Create a list with initial capacity in Python

Warning: This answer is contested. See comments. def doAppend( size=10000 ): result = [] for i in range(size): message= “some unique object %d” % ( i, ) result.append(message) return result def doAllocate( size=10000 ): result=size*[None] for i in range(size): message= “some unique object %d” % ( i, ) result[i]= message return result Results. (evaluate each … Read more

Error “initializer element is not constant” when trying to initialize variable with const

In C language, objects with static storage duration have to be initialized with constant expressions, or with aggregate initializers containing constant expressions. A “large” object is never a constant expression in C, even if the object is declared as const. Moreover, in C language, the term “constant” refers to literal constants (like 1, ‘a’, 0xFF … Read more

How can I make the memberwise initialiser public, by default, for structs in Swift?

Quoting the manual: “Default Memberwise Initializers for Structure Types The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Otherwise, the initializer has an access level of internal. As with the default initializer above, if you want a public structure type to be initializable with … Read more

How to declare an ArrayList with values? [duplicate]

In Java 9+ you can do: var x = List.of(“xyz”, “abc”); // ‘var’ works only for local variables Java 8 using Stream: Stream.of(“xyz”, “abc”).collect(Collectors.toList()); And of course, you can create a new object using the constructor that accepts a Collection: List<String> x = new ArrayList<>(Arrays.asList(“xyz”, “abc”)); Tip: The docs contains very useful information that usually … Read more

How to initialize array to 0 in C?

Global variables and static variables are automatically initialized to zero. If you have simply char ZEROARRAY[1024]; at global scope it will be all zeros at runtime. But actually there is a shorthand syntax if you had a local array. If an array is partially initialized, elements that are not initialized receive the value 0 of … Read more

How to set default values in Go structs

One possible idea is to write separate constructor function //Something is the structure we work with type Something struct { Text string DefaultText string } // NewSomething create new instance of Something func NewSomething(text string) Something { something := Something{} something.Text = text something.DefaultText = “default text” return something }

Initializing multiple variables to the same value in Java

String one, two, three; one = two = three = “”; This should work with immutable objects. It doesn’t make any sense for mutable objects for example: Person firstPerson, secondPerson, thirdPerson; firstPerson = secondPerson = thirdPerson = new Person(); All the variables would be pointing to the same instance. Probably what you would need in … Read more

Java: how to initialize String[]?

You need to initialize errorSoon, as indicated by the error message, you have only declared it. String[] errorSoon; // <–declared statement String[] errorSoon = new String[100]; // <–initialized statement You need to initialize the array so it can allocate the correct memory storage for the String elements before you can start setting the index. If … Read more

How to call a method after bean initialization is complete?

To expand on the @PostConstruct suggestion in other answers, this really is the best solution, in my opinion. It keeps your code decoupled from the Spring API (@PostConstruct is in javax.*) It explicitly annotates your init method as something that needs to be called to initialize the bean You don’t need to remember to add … Read more

Is there a difference between copy initialization and direct initialization?

C++17 Update In C++17, the meaning of A_factory_func() changed from creating a temporary object (C++<=14) to just specifying the initialization of whatever object this expression is initialized to (loosely speaking) in C++17. These objects (called “result objects”) are the variables created by a declaration (like a1), artificial objects created when the initialization ends up being … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)