snake plissken snake plissken - 2 months ago 20
Java Question

Merge 3 arraylist to one

I want to merge down 3 arraylist in one in java. Does anyone know which is the best way to do such a thing?

Answer

Use ArrayList.addAll(). Something like this should work (assuming lists contain String objects; you should change accordingly).

List<String> combined = new ArrayList<String>();
combined.addAll(firstArrayList);
combined.addAll(secondArrayList);
combined.addAll(thirdArrayList);

Update

I can see by your comments that you may actually be trying to create a 2D list. If so, code such as the following should work:

List<List<String>> combined2d = new ArrayList<List<String>>();
combined2d.add(firstArrayList);
combined2d.add(secondArrayList);
combined2d.add(thirdArrayList);
Comments