Array of Array Lists Java

Array of Array Lists Java


We have seen earlier adding "arrays to array list". It is also possible to create an array of array lists; that is, elements of array list are again array lists. This type of coding is not of regular use, but is of logical importance.

Two programs are given on this subject, the first one AOAL is very simple and easily understandable. The second program AOAL1 takes sometime to understand.

Following is the first program on Array of Array Lists


Array of Array Lists
Output screenshot on Array of Array Lists

ArrayList listArray[] = new ArrayList[2];

An array list array listArray is created capable to store two elements. As the type of the array is ArrayList, the elements must be ArrayList only.

ArrayList firstList = new ArrayList();
firstList.add(“hello”); firstList.add(“world”);

ArrayList secondList = new ArrayList();
secondList.add(10); secondList.add(20);

Two arrays firstList and secondList are created storing strings and integer values.

listArray[0] = firstList;
listArray[1] = secondList;

For the first element listArray[0], it is added with firstList and for the second element listArray[1], it is added with secondList.

System.out.println(listArray[0]);
System.out.println(listArray[1]);

Finally elements are displayed with System.out.println statement.

Following is the second program on Array of Array Lists


Array of Array Lists
Output screen of Array of Array Lists

ArrayList listArray[] = new ArrayList[5];

A five element array list listArray is created. Do you feel listArray[0] is an object of ArrayList? No, it is simply a reference variable of ArrayList. It is required to convert it into an object separately. For example see the following code used with Demo class.

Demo d[] = new Demo[5];

Here, d[0] is not an object; if used as an object like d[0].x (where x is an instance variable), it raises an error. It is just a reference variable of Demo. It must be converted into an object like this.

d[0] = new Demo();

And like this all the 5 reference variables should be converted into objects. This is exactly what is done in our listArray case also.

In a for loop, each reference variable of listArray is converted into an object. The for loop is taken to avoid writing 5 statements separately.

In a nested for loop, all the 5 array lists are added with 10 elements, from 0 to 9.

Again in another for loop, all the elements of each array list are printed.

2 thoughts on “Array of Array Lists Java

  1. mohan

    ArrayList(String) al=new ArrayList(String)();
    al.add(“mohan”);
    al.add(“Raju”);
    String names[] = { “S N Rao”, “Jyostna” };
    al.add(names);
    Error:
    No method found for add(String[ ])
    Method invocation not possible String[] to String.
    but when i write
    ArrayList(object) al=new ArrayList(Object)();
    al.add(“mohan”);
    al.add(“Raju”);
    String names[] = { “S N Rao”, “Jyostna” };
    al.add(names);

    It is not giving any error What’s the speciality with Object
    How method invocation is done here ?
    t hank u

  2. mohan

    ArrayList al=new ArrayList();
    al.add(“mohan”);
    al.add(“Raju”);
    String names[] = { “S N Rao”, “Jyostna” };
    al.add(names);
    Error:
    No method found for add(String[ ])
    Method invocation not possible String[] to String.
    but when i write
    ArrayList al=new ArrayList();
    al.add(“mohan”);
    al.add(“Raju”);
    String names[] = { “S N Rao”, “Jyostna” };
    al.add(names);

    It is not giving any error What’s the speciality with Object
    How method invocation is done here ?
    t hank u

Leave a Reply

Your email address will not be published. Required fields are marked *