LLgood
LLgood

Reputation: 37

Java how to iterate through an ArrayList of an ArrayList?

I am trying to grasp the concept. I have never worked with ArrayLists before (just arrays).

What I have is:

ArrayList<ArrayList<String>> movies = new ArrayList<ArrayList<String>>();

What this will look like or the way I picture it is:

[[Ratatouille, A Bug's Life], [Tangled, Zootopia, Finding Dory], [Harry Potter]]

And say the userInput = 2; then I would subtract 1 from the user input (because Array's and ArrayList's index at 0 that much I know) so userInput= 1; (based on their multiple choice selection, not very important).

Then what I want to do is take the index 1 so [Tangled, Zooptopia, Finding Dory] and loop through that index and add it to an ArrayList (not ArrayList of an ArrayList).

Upvotes: 1

Views: 8670

Answers (6)

user9680608
user9680608

Reputation:

Declare your variable as following

private ArrayList<String> arrayList = new ArrayList<>();

Then in a method add following

for(int j=0; j < arrayList.size() ; j++){
arrayList.get(i);
 // Your code goes here
}

This should work

Upvotes: 0

Abhishek Attri
Abhishek Attri

Reputation: 114

You can use either for-each loop or simple for loop to print elements in ArrayList of ArrayList. For example,

ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);list1.add(2);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(3);list2.add(4);
ArrayList<Integer> list3 = new ArrayList<>();
list3.add(5);list3.add(6);

ArrayList<ArrayList<Integer>> listOfList = new ArrayList<>();
listOfList.add(list1);
listOfList.add(list2);
listOfList.add(list3);

// Printing elements using for-each loop
for(ArrayList<Integer> eachList : listOfList){
  for(Integer elementInList : eachList){
    System.out.print(elementInList + "\t");
  }
  System.out.println();
}

// Printing elements using for loop
for(int i = 0;i < listOfList.size();i++){
  ArrayList<Integer> eachList = listOfList.get(i);
  for(int j = 0;j < eachList.size();j++){
    System.out.print(eachList.get(j) + "\t");
  }
  System.out.println();
}

Output:

enter image description here

Upvotes: -1

Sam Bombo
Sam Bombo

Reputation: 25

You should learn how to use Java Lambdas. you can use a lambda to iterate through an ArrayList since ArrayLists are Iterable.

ArrayList<ArrayList<String>>arrayListofarrayList  = new ArrayList<ArrayList<String>>();
arrayListofarrayList.forEach((arrayList) -> {System.out.println(arrayList.get(1));});

or

ArrayList<ArrayList<String>>arrayListofarrayList  = new ArrayList<ArrayList<String>>();
arrayListofarrayList.forEach((arrayList) -> {\*code here!*\});

if you're trying nest iterations you can:

ArrayList<ArrayList<String>>arrayListofarrayList  = new ArrayList<ArrayList<String>>();
arrayListofarrayList.forEach((arrayList) -> {
arrayList.forEach((item) -> {
    System.out.println(item);
    });
});

Upvotes: 0

Sanshrew
Sanshrew

Reputation: 1

The ArrayList.get(int index) method can help you out.

ArrayList myList = fullList.get(1); would give you your desired list and you can iterate over it like:

for (String currString: myList){ //Do things with currString }

I hope I explained it well :)

Upvotes: 0

hyperCoder
hyperCoder

Reputation: 271

following with following code you can iterate through an arrayList

private ArrayList<String> myArrayList = new ArrayList<>();
for(int i=0;i<myArrayList.size();i++){
myArrayList.get(i);
// Perform whatever operation here
}

Let me know if it doesn't work. And also what's the error given

Upvotes: 1

Mureinik
Mureinik

Reputation: 311073

No need to loop - You can access an ArrayList by an index, and then use the addAll method to add all the elements of the ArrayList in that position to your result:

result.addAll(movies.get(userInput - 1));

Upvotes: 1

Related Questions