Questions tagged [initialization]

Initialization deals with the task of initializing the contents of your data structure. It's a common practice in statically-typed languages.

Initialization deals with the task of initializing the contents of data structures. This is the initialization of an object, structure, blob of data, or any entity which contains state that must be prepared prior to usage of the entity.

11649 questions
3292
votes
34 answers

Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList places = new ArrayList(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then, I refactored the code as…
Macarse
  • 91,829
  • 44
  • 175
  • 230
2361
votes
31 answers

JavaScript check if variable exists (is defined/initialized)

Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.)) if (elem) { // or !elem or if (typeof elem !== 'undefined') { or if (elem != null) {
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
1574
votes
22 answers

How to directly initialize a HashMap (in a literal way)?

Is there some way of initializing a Java HashMap like this?: Map test = new HashMap{"test":"test","test":"test"}; What would be the correct syntax? I have not found anything regarding this. Is this possible? I am…
jens
  • 16,455
  • 4
  • 21
  • 20
1236
votes
43 answers

How can I initialise a static Map?

How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating the two methods: import…
dogbane
  • 266,786
  • 75
  • 396
  • 414
1157
votes
26 answers

How to initialize all members of an array to the same value?

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, but isn't there a way to do this that is built…
Matt
  • 84,419
  • 25
  • 57
  • 67
1108
votes
8 answers

Do the parentheses after the type name make a difference with new?

If 'Test' is an ordinary class, is there any difference between: Test* test = new Test; and Test* test = new Test();
David Read
  • 11,031
  • 3
  • 17
  • 5
905
votes
24 answers

How to initialize HashSet values by construction?

I need to create a Set with initial values. Set h = new HashSet(); h.add("a"); h.add("b"); Is there a way to do this in one line of code? For instance, it's useful for a final static field.
Serg
  • 13,470
  • 8
  • 36
  • 47
902
votes
15 answers

Efficiency of Java "Double Brace Initialization"?

In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax: Set flavors = new HashSet() {{ add("vanilla"); add("strawberry"); add("chocolate"); add("butter…
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
795
votes
30 answers

In C#, should I use string.Empty or String.Empty or "" to intitialize a string?

In C#, I want to initialize a string value with an empty string. How should I do this? What is the right way, and why? string willi = string.Empty; or string willi = String.Empty; or string willi = ""; or what?
Daniel Kreiseder
  • 12,135
  • 9
  • 38
  • 59
705
votes
29 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can create an array and initialize it like this: int a[] = {10, 20, 30}; How do I create a std::vector and initialize it similarly elegant? The best way I know is: std::vector
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
625
votes
18 answers

How to initialize private static members in C++?

What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors: class foo { private: static int i; }; int foo::i = 0; I'm guessing this is because I can't…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
609
votes
5 answers

What are the advantages of list initialization (using curly braces)?

MyClass a1 {a}; // clearer and less error-prone than the other three MyClass a2 = {a}; MyClass a3 = a; MyClass a4(a); Why?
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
606
votes
16 answers

How to initialize a struct in accordance with C programming language standards

I want to initialize a struct element, split in declaration and initialization. This is what I have: typedef struct MY_TYPE { bool flag; short int value; double stuff; } MY_TYPE; void function(void) { MY_TYPE a; ... a = { true, 15,…
cringe
  • 13,401
  • 15
  • 69
  • 102
492
votes
11 answers

How to initialize an array in Java?

I am initializing an array like this: public class Array { int data[] = new int[10]; /** Creates a new instance of Array */ public Array() { data[10] = {10,20,30,40,50,60,71,80,90,91}; } } NetBeans points to an error…
chatty
  • 5,001
  • 3
  • 19
  • 9
432
votes
15 answers

How do I initialize an empty array in C#?

Is it possible to create an empty array without specifying the size? For example, I created: String[] a = new String[5]; Can we create the above string array without the size?
yogesh
  • 4,455
  • 2
  • 14
  • 13
1
2 3
99 100