I am trying to make program that create an ArrayList of initial size of 1 and populate it with random numbers. Next, the ArrayList is cleared and the size of ArrayList is increased by 1 and again filled with random numbers. I want my program to repeat those steps until the ArrayList size will be equal to number specified by user.
So the output will be for example like:
1
3,8
6,3,7
...
n
I have produced so far:
public static void main (String args[]){
ArrayList<Integer> al = new ArrayList<Integer>();
Random rand = new Random();
int val=1;
int ii =1;
while(val<400){
for (int j = 0; j<ii;)
{
ii++;
val++;
pick = rand.nextInt(100);
al.add(pick);
}
System.out.println("Contents of al: " + al);
al.clear();
System.out.print("Array has been cleared: " + al);
}
Any ideas how to make it work?
forloop (j is always =0 and ii is always >=0) hence your list grows out of bounds. You should rethink your loops.