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 ArrayList
with 60 zeros you do:
ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, 0));
Read full article from collections - How can I initialize an ArrayList with all zeroes in Java? - Stack Overflow
No comments:
Post a Comment