For an array of ArrayList objects:
ArrayList obj[10];
Objects will be initialized by default, which is great for custom types, but may not be what you want for built-in types.
Consider also:
std::vector<ArrayList> obj(10, ArrayList());
This initializes the objects, copying everything you pass as the second parameter. So they are all the same, but not necessarily default. And, as the bulb indicates, โ10โ in a vector can be replaced with a non-constant expression, while โ10โ in an array declaration cannot.
This does not actually push ArrayList objects onto the stack; it pushes all 10 in a single selection from the heap. Thus, there can very rarely be performance problems if you really cannot afford the highlight. However, std :: vector is on the stack and removes any heap objects that it uses when it is destroyed. So, in order to make sure that your resources are freed, the vector behaves as if they were all on the stack.
Note that mixing an Object container with ArrayList values, as in your Java code example, is fraught with danger in C ++. Basically you cannot do this even if ArrayList extends Object, because the array will only contain storage for 10 objects, and ArrayList probably requires more bytes to store than Object. As a result, any ArrayList that you are trying to copy into an array will receive a โcutโ: only the initial part of its representation is placed in the array.
If you need a container of type saying that it contains objects, but which actually contains ArrayLists, then you need a container of pointers. To get good resource handling, this probably means you need a smart pointer container.
source share