简体   繁体   中英

Java how to iterate through an ArrayList of an ArrayList?

I am trying to grasp the concept. I have never worked with ArrayList s 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 ).

无需循环 - 您可以通过索引访问ArrayList ,然后使用addAll方法将该位置的ArrayList所有元素添加到结果中:

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

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

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 :)

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);
    });
});

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:

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM