arnold leki
arnold leki

Reputation: 267

2D ArrayList in java

I have a 2D ArrayList and I want to take one specific ArrayList from those that are inside the 2D ArrayList.

Is this possible? If yes how can I do this? How exactly does the 2D arrayList work, I have read a lot about it but I still can't understand. My 2D ArrayList has this form:

ArrayList<ArrayList<Items>> arrayList = new ArrayList<ArrayList<Items>>();

for (int i = 0; i < 10; i++) {
    ArrayList<Items> row = new ArrayList<Items>();

    for (int j = 0; j < 500; j++)
    {
        // create the items...
    }
}

Upvotes: 1

Views: 23979

Answers (4)

SWETA KESUR
SWETA KESUR

Reputation: 321

Yes, of course, you can pick ArrayList from an ArrayList. In order to do this you can follow these 2 steps:

  1. Initialize and declare 2D ArrayList

As per your question, you have 10 rows (i = 10) and 500 columns (j=500).

ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();

    for (int i = 0; i < 10; i++) {
        ArrayList<Integer> row = new ArrayList<Integer>();

        for (int j = 0; j < 500; j++)
        {
            //add any integers into "row" ArrayList
            row.add(2);
        }
        //add this above 1d "row" ArrayList to our 2D "arraylist" 
        arrayList.add(row);
    }
  1. Fetch elements from an ArrayList
    for (int i = 0; i < arrayList.size(); i++) { 
        for (int j = 0; j < arrayList.get(i).size(); j++) { 
            System.out.print(arrayList.get(i).get(j) + " "); 
        } 
        System.out.println(); 
    }

Upvotes: 1

Jesan Fafon
Jesan Fafon

Reputation: 2304

Yes this is possible.

Since you have an ArrayList< ArrayList<Items> > when you call arrayList.get() this will return an ArrayList. Then you can go ahead and do whatever you need.

For instance:

for(int i = 0; i < arrayList.length(); i++){
   ArrayList<Items> innerList = arrayList.get(i);
   for(int j =0; j < 10; j++){
       innerList.add(new Items());
   }
 }

This will take your ten ArrayLists you made above and fill each of them with ten items.

Upvotes: 7

Synesso
Synesso

Reputation: 39018

EDIT: this answer is for the original version of the question (which was a little difficult to understand).

If I understand you correctly, you want to flatten many lists into one.

List<List<String>> listOfLists = new ArrayList<List<String>>();
for (int i = 0; i < 5; i++) {
    List<String> thisList = Arrays.asList("a", "b", "c");
    listOfLists.add(thisList);
}

// flatten
List<String> flattenedListOfStrings = new ArrayList<String>();
for (List<String> listOfString : listOfLists) {
    flattenedListOfStrings.addAll(listOfString);
}

// test
for (String string : flattenedListOfStrings) {
    System.out.print(string);
}

Outputs:

abcabcabcabcabc

Upvotes: 1

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

Starting from the top:

Is this possible?

Yes, quite simple actually:

ArrayList innerList = arrayList.get(listIndex);

How does the 2D ArrayList work?

Basically, it functions as an array of arrays, so that when you access one element of the 2D list, returns an ArrayList, which you must further call get() on to get the element you want.

Example:

ArrayList innerList = (ArrayList) arrayList.get(listIndex); // returns an arraylist
Item item = (Item) innerList.get(innerListIndex); // returns your item

Upvotes: 5

Related Questions