Lonely Twinky Lonely Twinky - 1 year ago 64
Java Question

Converting several Arraylists into an array

I need to put the values of an arraylist into a array, so that I can complete a for-loop project. I have an arraylist with 10 arraylists and each one of the arrays has one or more integer values:

Arraylist<ArrayList<Integer>> lists = new ArrayList<>();

I used a for-loop to create the 10 other arrays and now I need to put the 10 arraylists into one array that is

Integer [] second;

I need to put the arraylists into the [] array in the order that they are placed in, I have to do this to complete my project. But for some reason my for-loop used to put each single arraylist into the array will not print them. Any suggestions?
Here is my for-loop used to print the arraylists into the array:

for(int i=0; i<lists.siz();i++)
second = lists.get(i).toArray(second);

Answer Source
// Create temp list
List<Integer> secondList = new ArrayList<Integer>();
// add all sublist to temp list
for(ArrayList<Integer> subList : lists)
// convert temp list to array
Integer[] second = secondList.toArray(new Integer[0]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download