We have discussed that an array of ArrayList is not possible without warning. A better idea is to use ArrayList of ArrayList.
// Java code to demonstrate the concept of // array of ArrayList import java.util.*; public class Arraylist { public static void main(String[] args) { int n = 3; // Here aList is an ArrayList of ArrayLists ArrayList<ArrayList<Integer> > aList = new ArrayList<ArrayList<Integer> >(n); // Create n lists one by one and append to the // master list (ArrayList of ArrayList) ArrayList<Integer> a1 = new ArrayList<Integer>(); a1.add(1); a1.add(2); aList.add(a1); ArrayList<Integer> a2 = new ArrayList<Integer>(); a2.add(5); aList.add(a2); ArrayList<Integer> a3 = new ArrayList<Integer>(); a3.add(10); a3.add(20); a3.add(30); aList.add(a3); for (int i = 0; i < aList.size(); i++) { for (int j = 0; j < aList.get(i).size(); j++) { System.out.print(aList.get(i).get(j) + " "); } System.out.println(); } } } |
chevron_right
filter_none
Output:
1 2 5 10 20 30
Recommended Posts:
- Arraylist.contains() in Java
- ArrayList in Java
- ArrayList vs LinkedList in Java
- Array vs ArrayList in Java
- Synchronization of ArrayList in Java
- ArrayList isEmpty() in Java with example
- Vector vs ArrayList in Java
- Initialize an ArrayList in Java
- Reverse an ArrayList in Java
- Arraylist lastIndexOf() in Java with example
- ArrayList trimToSize() in Java with example
- How to get ArrayList from Stream in Java 8
- ArrayList vs. HashMap in Java
- Get first and last elements from ArrayList in Java
- Custom ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



