1

I have created a array of ArrayList 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>();
    lists[i].add(i+i*2);
    lists[i].add(i+i*3);
    lists[i].add(i+i*4);
    lists[i].add(i+i*5);
  }  
   System.out.println(lists);

output:-

[Ljava.util.ArrayList;@15db9742 
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122

1 Answers1

1

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

System.out.println(Arrays.toString(lists));

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.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122