简体   繁体   English

在 Java 中打印 2D ArrayList 导致空括号

[英]Printing a 2D ArrayList in Java results in empty brackets

I'm trying to read a file and put each 2 lines into an ArrayList of strings.我正在尝试读取文件并将每 2 行放入字符串的 ArrayList 中。 My program has a counter that starts at 1 and keeps track of the lines, and if it is on line 2 it will do the following:我的程序有一个从 1 开始并跟踪行的计数器,如果它在第 2 行,它将执行以下操作:

  • Add the ArrayList of strings named line into a 2D ArrayList named list2D将名为line的字符串的 ArrayList 添加到名为list2D 的二维 ArrayList
  • Clear line using .clear()使用.clear()清除
  • Reset the line counter to 1将行计数器重置为 1

So the idea is to store every 2 lines of text as a 2D ArrayList of strings.所以想法是将每两行文本存储为字符串的二维 ArrayList。 Here is an expected input file and the expected output:这是预期的输入文件和预期的 output:

INPUT FILE: cat
dog
mouse
duck (every word is on a different line idk why SO put it on the same line)
OUTPUT: [[cat, dog],[mouse, duck]]

However, when I print my 2D ArrayList using w.println(list2D) , the program outputs emptiness:但是,当我使用w.println(list2D)打印我的 2D ArrayList 时,程序输出空虚:

[ [ ], [ ] ]

Here is my code:这是我的代码:

ArrayList<ArrayList<String>> list2D = new ArrayList<ArrayList<String>>();
ArrayList<String> lines = new ArrayList<String>();

Integer lineNum = 1;

for (String line = r.readLine(); line != null; line = r.readLine()) {
    lines.add(line);
    if (lineNum == 2) { //line counter is at the end of block
        list2D.add(lines);
        lines.clear();
        lineNum = 1; //reset line counter
    } else {
        lineNum++;
    }
}
w.println(list2D);

I've checked other SO posts regarding how to create a 2D ArrayList, so I'm pretty sure I got everything from the for loop right.我已经检查了其他关于如何创建 2D ArrayList 的 SO 帖子,所以我很确定我从 for 循环中得到了一切。 When I test print the lines ArrayList before adding it to lines2D , it prints the desired result (eg [cat, dog]), so at least I'm correct up until that point.当我在将 ArrayList 添加到lines2D之前测试打印lines ,它会打印所需的结果(例如 [cat, dog]),所以至少在那之前我是正确的。

I'm also not sure if w.println(list2D) is the right method for printing, but again I've seen multiple other SO posts that use this method.我也不确定w.println(list2D)是否是正确的打印方法,但我再次看到使用此方法的多个其他 SO 帖子。

Any help/criticism is appreciated, thank you.感谢任何帮助/批评,谢谢。

the program outputs emptiness: [ [ ], [ ] ]程序输出空虚:[ [ ], [ ] ]

That is because you only have a single ArrayList.那是因为您只有一个 ArrayList。

You add a reference of that ArrayList to the 2D ArrayList multiple times, but there is no data in the ArrayList because you add two entries, then you clear the entries.您多次将 ArrayList 的引用添加到 2D ArrayList 中,但 ArrayList 中没有数据,因为您添加了两个条目,然后清除条目。

You need to create an new ArrayList for every two lines of text.您需要为每两行文本创建一个新的 ArrayList。

There is no need for the "lineNum" variable.不需要“lineNum”变量。

You just add a line of text to the ArrayList.您只需在 ArrayList 中添加一行文本。 When the size of the ArrayList is 2, you add it to the 2D ArrayList and create an new empty ArrayList for the next 2 lines of text.当 ArrayList 的大小为 2 时,将其添加到 2D ArrayList 并为接下来的 2 行文本创建一个新的空 ArrayList。

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

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