Vijay Kumar Prajapat Vijay Kumar Prajapat - 1 year ago 112
Java Question

How to print an array of ArrayList in Java?

I have created a array of

but now I want to access its elements and sort them and print them, please help me to find out a way

ArrayList<Integer>[] lists = (ArrayList<Integer>[])new ArrayList[20] ;
for(int i=0;i<20;i++){
lists[i]=new ArrayList<Integer>();



Answer Source

You need to use Arrays.toString(Object[]) to print the content of your array as next:


Arrays.toString(Object[]): Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.

Using Arrays.toString(Object[]) to print the content of your array is enough in your case as an ArrayList is a subclass of AbstractCollection which overrides toString() to give a String representation of the collection in square brackets using a comma as value separator and here values are Integers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download