首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何如何访问数组:Java?

如何如何访问数组:Java?
EN

Stack Overflow用户
提问于 2018-09-17 00:42:55
回答 2查看 0关注 0票数 0

有一个包含数组列表的ArrayList,需要依次访问这些数组,怎么实现呢?

代码语言:txt
复制
List<String> fixedLenghtList = Arrays.asList(elements);
ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList);
EN

回答 2

Stack Overflow用户

发布于 2018-09-17 09:22:51

可以这样试试:

代码语言:javascript
复制
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);
    }
}

如果你不增加更多计划ListsArrayList,只需使用:

代码语言:javascript
复制
ArrayList<String> listOfString = new ArrayList<String>(Arrays.asList(elements));
票数 0
EN

Stack Overflow用户

发布于 2018-09-17 10:11:10

试试下面的代码

代码语言:txt
复制
// 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档