有一个包含数组列表的ArrayList,需要依次访问这些数组,怎么实现呢?
List<String> fixedLenghtList = Arrays.asList(elements);
ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList);
发布于 2018-09-17 09:22:51
可以这样试试:
List<String> fixedLenghtList = Arrays.asList(elements);
ArrayList<List<String>> listOfString = new ArrayList<List<String>>(fixedLenghtList);
for (List<String> stringList : listOfString) {
for (String e : stringList) {
System.out.println(e);
}
}
如果你不增加更多计划Lists
到ArrayList
,只需使用:
ArrayList<String> listOfString = new ArrayList<String>(Arrays.asList(elements));
发布于 2018-09-17 10:11:10
试试下面的代码
// inputs objects
String[] elements = {"elem 1","elem 2","elem 3"};
List<String> fixedLenghtList = Arrays.asList(elements);
String[] otherElements = {"elem 4", "elem 5", "elem 6", "elem 7", "elem 8"};
List<String> otherFixedLenghtList = Arrays.asList(otherElements);
// a new instance of arraylist of empty list of strings
ArrayList<List<String>> listOfString = new ArrayList<List<String>>();
// add two list of string -> fixedLengthList, otherFixed...
listOfString.add(fixedLenghtList);
listOfString.add(otherFixedLenghtList);
// java 8 foreach, now it's easy and similar to C#
listOfString.forEach(itemListOfString -> itemListOfString.forEach(System.out::println));
// outpout on stdout :
// elem 1
// elem 2
// elem 3
// elem 4
// elem 5
// elem 6
// elem 7
// elem 8
https://stackoverflow.com/questions/-100002638
复制相似问题