ArrayList

Hi,
I am aware of the 3 types of ArrayList we have, but is there a way to create an ArrayList myArrayList of  size n and initialize all myArrayList[i] with int 1.( of couse without using a loop. :) )

Thanks
komlaaaAsked:
Who is Participating?
 
objectsCommented:
For an ArrayList use:

ArrayList list = new ArrayList(1000);
Collections.fill(list, new Integer(1));

> I am aware of the 3 types of ArrayList we have

What 3 types are you referring to?
0
 
CEHJCommented:
myArrayList = new ArrayList(Arrays.asList(new int[] { 1, 1, 1, 1, 1, 1 }));
0
 
komlaaaAuthor Commented:
this approach i good but will be tedious if i size n=1000
0
 
CEHJCommented:
int[] ints = new int[1000];
Arrays.fill(ints, 1);

// (the rest as before)
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.