Array of ArrayList in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 23 Likes Like Report We often come across 2D arrays where most of the part in the array is empty. Since space is a huge problem, we try different things to reduce the space. One such solution is to use jagged array when we know the length of each row in the array, but the problem arises when we do not specifically know the length of each of the rows. Here we use ArrayList since the length is unknown. Following is a Java program to demonstrate the above concept. Java // Java code to demonstrate the concept of // array of ArrayList import java.util.*; public class Arraylist { public static void main(String[] args) { int n = 5; // Here al is an array of arraylist having // n number of rows.The number of columns on // each row depends on the user. // al[i].size() will give the size of the // i'th row ArrayList<Integer>[] al = new ArrayList[n]; // initializing for (int i = 0; i < n; i++) { al[i] = new ArrayList<Integer>(); } // We can add any number of columns to each // rows as per our wish al[0].add(1); al[0].add(2); al[1].add(5); al[2].add(10); al[2].add(20); al[2].add(30); al[3].add(56); al[4].add(34); al[4].add(67); al[4].add(89); al[4].add(12); for (int i = 0; i < n; i++) { for (int j = 0; j < al[i].size(); j++) { System.out.print(al[i].get(j) + " "); } System.out.println(); } } } Output : 1 2 5 10 20 30 56 34 67 89 12 The above code works fine, but shows below warning. prog.java:15: warning: [unchecked] unchecked conversion ArrayList[] al = new ArrayList[n]; ^ required: ArrayList[] found: ArrayList[] 1 warning The warning comes basically due to below line. Java ArrayList<Integer>[] al = new ArrayList[n]; How to fix above warning? We cannot use array of ArrayList without warning. We basically need to use ArrayList of ArrayList. Create Quiz Comment D debjitdbb Follow 23 Improve D debjitdbb Follow 23 Improve Article Tags : Misc Java Java-Collections Java - util package Java-Arrays Java-ArrayList Java-Array-Programs Java-List-Programs +4 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like