Array of ArrayList in Java Last Updated : 11 Dec, 2018 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article Array of ArrayList in Java D debjitdbb Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Arrays Java-ArrayList Java-Array-Programs Java-List-Programs +4 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Array vs ArrayList in Java In Java, an Array is a fixed-sized, homogenous data structure that stores elements of the same type whereas, ArrayList is a dynamic-size, part of the Java Collections Framework and is used for storing objects with built-in methods for manipulation. The main difference between array and ArrayList is: 4 min read Java ArrayList of Arrays ArrayList of arrays can be created just like any other objects using ArrayList constructor. In 2D arrays, it might happen that most of the part in the array is empty. For optimizing the space complexity, Arraylist of arrays can be used. ArrayList<String[ ] > geeks = new ArrayList<String[ ] 2 min read ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac 9 min read Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th 3 min read Conversion of Array To ArrayList in Java Following methods can be used for converting Array To ArrayList: Method 1: Using Arrays.asList() method Syntax: public static List asList(T... a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing 5 min read Like