简体   繁体   English

如何遍历字符串数组列表的数组列表?

[英]How to iterate through an arraylist of an arraylist of strings?

I'm creating a program that allows users to interact with a file thats input. 我正在创建一个程序,该程序允许用户与输入的文件进行交互。 One of the options is to display the file. 选项之一是显示文件。 Below, I store the file into an arraylist first by creating arraylists consisting of lines, and then an inner arraylist separated of strings, separated by spaces. 在下面,我首先通过创建由行组成的arraylist,然后将内部的arraylist(由字符串分隔,由空格分隔)将文件存储到arraylist中。

Scanner sc = new Scanner(fileName);

    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        String[] words = {};
        words = line.split(" ");
        ArrayList<String> lineArray = new ArrayList<String>();
        for (int i = 0; i < words.length; i++) {
            lineArray.add(words[i]);
        }
        fileByLine.add(lineArray);
    }
    sc.close();

I'm trying to print the contents of the arraylist, called fileByLine, as they appear in the file, but I'm not sure how to. 我正在尝试打印出现在文件中的arraylist的内容,称为fileByLine,但是我不确定如何打印。 Any suggestion? 有什么建议吗?

case '1':
for(int i = 0; i < fileByLine.size(); i++){
for(int j = 0; j < fileByLine.[i].size(); j++){
System.out.print(fileByLine[i][j]);
  } System.out.println("");
} break;

You are using bracket notation which is for arrays, you need to use get() for arraylists 您正在使用括号表示数组,需要将get()用于数组列表

for(int i = 0; i < fileByLine.size(); i++){
    for(int j = 0; j < fileByLine.get(i).size(); j++){
        System.out.print(fileByLine.get(i).get(j));
    }
    System.out.println("");
}

Since your container consist of String type you can just print it as follow: 由于您的容器包含String type您可以按照以下方式进行打印:

System.out.println(fileByLine);

No need to loop through your collection. 无需遍历您的收藏。 For example, let's say you have following list: 例如,假设您有以下列表:

List<String> oneList = new ArrayList<>(Arrays.asList("1 2 3 4 5 6 7".split(" ")));

and you want to add it into another list: 并将其添加到另一个列表中:

List<List<String>> anotherList = new ArrayList<>();
anotherList.add(oneList);

After adding you would like to print it and here is how it looks: 添加后,您要打印它,这是它的外观:

System.out.println(anotherList);

Output: [[1, 2, 3, 4, 5, 6, 7]] 输出:[[1、2、3、4、5、6、7]

It prints because of String type if you keep any other custom type in your container and try to print it that will not work until you override toString() method. 如果您在容器中保留任何其他custom type ,并尝试打印它,直到您override toString()方法,否则它将由于String type而打印。

If you need to iterate over two nested lists you can use this approach too: 如果您需要遍历两个嵌套列表,也可以使用这种方法:

Iterator<List<String>> outerIterator = anotherList.listIterator();

while (outerIterator.hasNext()) {
    Iterator<String> innerIterator = outerIterator.next().listIterator();

    while (innerIterator.hasNext()) {
        String item = innerIterator.next();
        System.out.print(item + " ");
    }

    System.out.println();
} 

Or something like this as well... 或类似的东西...

     for (List list : fileByLine) {
        for (Object line : list) {
            System.out.print(line);
        }
        System.out.println("");
     }

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

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