It looks like arraylist is not doing its job for presizing:
// presizing
ArrayList() list = new ArrayList(60);
Afterwards when I try to access it:
list.get(5)
It shows null instead of 0.
Is there a way to initialize all elements to 0 of an exact size like what C++ does?
解决方案
The integer passed to the constructor represents its initial capacity, i.e., the number of elements it can hold before it needs to resize its internal array (and has nothing to do with the initial number of elements in the list).
To initialize an list with 60 zeros you do:
List list = new ArrayList(Collections.nCopies(60, 0));
If you want to create a list with 60 different objects, you could use the Stream API with a Supplier as follows:
List persons = Stream.generate(Person::new)
.limit(60)
.collect(Collectors.toList());