Scanner input = new Scanner(System.in);
System.out.println("Number of Array lists");
int total_arraylists = input.nextInt();
ArrayList<Integer> lists[]=new ArrayList[total_arraylists];
for( int i = 0; i < total_arraylists; i++){
lists[i]=new ArrayList<Integer>(i);
System.out.println("Enter the values");
while(input.hasNextInt()){
lists[i].add(input.nextInt());
}
System.out.println(lists[i]);
}
Output of the above program is:
Number of Array lists
3
Enter the values
1
2
3
done
[1, 2, 3]
Enter the values
[]
Enter the values
[]
As we can see, when I enter any character or string (in this case,I entered "done"), the while loop exits and the other 2 Array lists remain empty. I want to add int values to those remaining Array lists too. How can I do it?
ArrayLists?