Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

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

// presizing 

ArrayList<Integer>() list = new ArrayList<Integer>(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?

1 Answer

0 votes
by (46k points)

Try:

// apparently this is broken. Whoops for me!

java.util.Collections.fill(list,new Integer(0));

// this is better

Integer[] data = new Integer[60];

Arrays.fill(data,new Integer(0));

List<Integer> list = Arrays.asList(data);

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 2, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...