How can I initialize an ArrayList with all zeroes in Java?

后端 未结 5 794
小蘑菇
小蘑菇 2020-11-27 10:37

It looks like arraylist is not doing its job for presizing:

// presizing 

ArrayList list = new ArrayList(60);
         


        
5条回答
  •  Happy的楠姐
    2020-11-27 11:15

    The 60 you're passing is just the initial capacity for internal storage. It's a hint on how big you think it might be, yet of course it's not limited by that. If you need to preset values you'll have to set them yourself, e.g.:

    for (int i = 0; i < 60; i++) {
        list.add(0);
    }
    

提交回复
热议问题