Array of ArrayList in Java
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.
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.
How to fix above warning?
We cannot use array of ArrayList without warning. We basically need to use ArrayList of ArrayList.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.