Manually accessing an ArrayLists with a ArrayList

856 Views Asked by At

I'm working with Depth First Search program and I'm trying to create a Adjacency List Representation. I read through some articles stating that an creating ArrayLists within an ArrayList would be the best representation.

Let's say I initialized the arraylist within a arraylist like so:

List<List<Integer>> adjList = new ArrayList<List<Integer>>();

My question is how would you input data into the arraylist MANUALLY. I'm trying to understand the concept of arraylist with an arraylist before I begin my programming. If someone could possibly insert data into this arraylist so I could see the proper way of setting up.

Any additional input on anything I might need or take in consideration is recommended.

BTW: This is not a homework assignment just using personal time looking through my old textbooks.

4

There are 4 best solutions below

0
On BEST ANSWER

Let's say you want to add 2 lists, one with 1 and 2 and the other with 10 and 20. A very manual way of adding could be:

List<List<Integer>> adjList = new ArrayList<List<Integer>>();

adjList.add(new ArrayList<Integer>()); // initialise new ArrayList<Integer>
adjList.get(0).add(1); // add value one by one
adjList.get(0).add(2);

adjList.add(new ArrayList<Integer>());
adjList.get(1).add(10);
adjList.get(1).add(20);

You could also write it this way:

List<List<Integer>> adjList = new ArrayList<List<Integer>>();

ArrayList<Integer> a1 = new ArrayList<Integer>(); // initialise new ArrayList<Integer>
a1.add(1); // add value one by one
a1.add(2);
adjList.add(a1);

ArrayList<Integer> a2 = new ArrayList<Integer>(); // initialise new ArrayList<Integer>
a2.add(10); // add value one by one
a2.add(20);
adjList.add(a2);
0
On

The adjList can contain the elements of the type List<Integer>, so create one and add using add(E element) function as we would for adding an element:

ArrayList<Integer>aList = new ArrayList<>();
adjList.add(aList);

Then to add an element to the element(which has the type List<Integer>) of adjList: you can try getting it using get(index) and add your element:

adjList.get(0).add(10);
adjList.get(0).add(22);

Try adding a second list and get it's index using get(1) and add the Integer element to the list at index 1 as the above example suggest. There are other known function too. Please check the class ArrayList<E> documentation page.

0
On

Well, a list of a list of Integer objects could be done as such:

List<List<Integer>> adjList = new ArrayList<List<Integer>>();
List<Integer> li = new ArrayList<Integer>();

for (int i = 0; i < 10; i++) {
    li.add(i);
}

adjList.add(li);

Add to each sublist, and then add the sublist.

0
On

This will help

public static void main(String[] args){
        //creating a new ArrayList of List of Integers
        ArrayList<List<Integer>> integerListContainer = new ArrayList<List<Integer>>();
        //Creating the first child arraylist of Integers
        ArrayList<Integer> firstChildintegerList = new ArrayList<Integer>();
        //filling the values 1,2,3 in it
        firstChildintegerList.add(1);
        firstChildintegerList.add(2);
        firstChildintegerList.add(3);
        //adding this integer list to the parent list
        integerListContainer.add(firstChildintegerList);
         //Creating the second child arraylist of Integers
        ArrayList<Integer> secondChildintegerList = new ArrayList<Integer>();
        //filling the values 10,20,30 in it
        secondChildintegerList.add(10);
        secondChildintegerList.add(20);
        secondChildintegerList.add(30);
        //adding this integer list to the parent list
        integerListContainer.add(secondChildintegerList);
        System.out.println("Printing the parent list to see what it has: ");
        System.out.println(integerListContainer.toString());
    }

Hope it clearly explains what happens