简体   繁体   English

如何迭代arrayLists的arraylist

[英]How to iterate arraylist of arrayLists

I Have a list which is in 我有一个清单在

[
    [SAM, 12/01/2015, 9A-6P], 
    [JAM, 12/02/2015, 9A-6P]
]

I need to iterate it.I tried the below code 我需要迭代它。我尝试了以下代码

for (int i = 0; i < list4.size(); i++) {
            System.out.println("List" + list4.get(i).toString());
}
//this is giving me [SAM, 12/01/2015, 9A-6P]

but I want to iterate the above one also [SAM, 12/01/2015, 9A-6P] . 但我也想对以上代码进行迭代[SAM, 12/01/2015, 9A-6P]

Can anybody have idea? 有人有主意吗?

You can and should use the fact that every List is also an Iterable . 您可以并且应该使用每个List也是Iterable的事实。 So you can use this: 因此,您可以使用以下代码:

// Idk what you list actually contains
// So I just use Object
List<List<Object>> listOfLists; 
for(List<Object> aList : listOfLists) {
    for(Object object : aList) {
        // Do whatever you want with the object, e.g.
        System.out.println(object);
    }
}

Tried your case with below example. 使用以下示例对您的案例进行了尝试。 Hope it helps 希望能帮助到你

import java.util.ArrayList;
import java.util.List;

public class IterateList {
  public static void main(String[] args) {
    List<List<String>> myList = new ArrayList<List<String>>();

    List<String> list1 = new ArrayList<String>();
    list1.add("SAM");
    list1.add("12/01/2015");
    list1.add("9A-6P");

    List<String> list2 = new ArrayList<String>();
    list2.add("JAM");
    list2.add("12/01/2015");
    list2.add("9A-6P");

    myList.add(list1);
    myList.add(list2);

    for (List list : myList) {
      for(int i=0; i<list.size();i++){
        System.out.println(list.get(i));
      }

    }
  }
}

Output: 输出:
SAM SAM
12/01/2015 2015年12月1日
9A-6P 9A-6P
JAM 果酱
12/01/2015 2015年12月1日
9A-6P 9A-6P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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