ArrayList内部的ArrayList,如何清除最后一个元素

huangapple go评论101阅读模式
英文:

Arraylist inside arraylist, how do I clear the last

问题

ArrayList<ArrayList<String>> mainAList = new ArrayList<ArrayList<String>>();
ArrayList<String> parse = new ArrayList<>();

try (Scanner filereader = new Scanner(Paths.get("name_of_file.txt"))) {
    while (filereader.hasNextLine()) {
        parse.add(filereader.nextLine());
        if (parse.get(parse.size() - 1).length() == 0 || !filereader.hasNextLine()) {
            mainAList.add(new ArrayList<>(parse));
        }
        parse.clear();
    }
}

文件读取的内容如下:
a
b
c
d

f
g

h
i
j
k
l

等等...

英文:

I use while loop to read a text file and create new arraylists out of portions of the text file and then place those arraylists inside another arraylist. My problem is that I don't know how to add my temporary arraylist<String> parse into the mainAList and then clear it for the next loop without affecting all the parse arraylists inside the arraylist<arraylist<string>> mainAList. More info inside the comments below in the code.

This is a sample code, it is from larger file. It is shortened but has all the moving parts

ArrayList&lt;ArrayList&lt;String&gt;&gt; mainAList = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
ArrayList&lt;String&gt; parse = new ArrayList&lt;&gt;();

try (Scanner filereader = new Scanner(Paths.get(&quot;name_of_file.txt&quot;))) {
    while (filereader.hasNextLine()) {
        parse.add(filereader.nextLine());
        if (parse.get(parse.size()-1).length() == 0 || !filereader.hasNextLine()) {
            // if the file reader detects empty line or has no more lines
            //  the arraylist called parse gets added to the mainAList
            mainAList.add(parse);
        }
        parse.clear();
        // here is my problem. How do I clear parse so for the next patch of lines
         //  so I don&#39;t add the same things + more into the next slot of mainAList
         // every time the loop runs?
         // I understand my issue is not how to correctly clear the parse but 
         // how to add the arraylists correctly into the mainAList so I can alter them individually
   }
}

The file that is read is like this:
a
b
c
d

f
g

h
i
j
k
l

etc..

答案1

得分: 1

你目前的方法存在两个问题:

  1. parse 会对所有的内部 ArrayList 使用相同的引用,因此当你调用 parse.clear() 时,会清空 mainAList 中所有内部列表的内容。可以通过在添加时创建 parse 列表的副本来解决这个问题(即 mainAList.add(new ArrayList<> (parse));)。
  2. 在 if 语句内部添加副本到 mainAList 后,你还希望清空该列表。此外,我假设你不希望空行添加到单独的内部列表中,因此你可以首先读取行,然后要么将整个列表添加到 mainAList 并在 if 语句内部清空 parse,要么将行添加到 parse 列表中。

稍微更好的替代方案是直接创建一个新的列表,这样你就不需要使用 clear 和创建列表的副本。

总之:

ArrayList<ArrayList<String>> mainAList = new ArrayList<ArrayList<String>>();
ArrayList<String> parse = new ArrayList<>();

try (Scanner filereader = new Scanner(System.in)) {
  while (filereader.hasNextLine()) {
    String line = filereader.nextLine();
    if (line.length() == 0 || !filereader.hasNextLine()) {
        // 如果文件阅读器检测到空行或没有更多行时,
        // 将名为 parse 的 ArrayList 添加到 mainAList 中,
        // 然后为下一组输入创建一个新的 parse 列表:
        mainAList.add(parse);
        parse = new ArrayList<>();
    } else{
        parse.add(line);
    }
  }
}
英文:

There are two problems with your current approach:

  1. parse will use the same reference for all inner ArrayLists, so when you call parse.clear() it will empty all inner lists of your mainAList. This can be solved by creating a copy of the parse list when you add it (i.e. mainAList.add(new ArrayList&lt;&gt;(parse));)
  2. You want to clear the list inside the if as well, after you've added the copy to your mainAList.

In addition, I assume you don't want the empty lines added to the individual inner Lists, so you could read the line first, and either add the entire list to mainAList and clear parse inside the if-statement, or add the line to the parse-list.

A slightly better alternative, is to just create a new list instead, so you won't need the clear and creating a copy of the list.

In total:

ArrayList&lt;ArrayList&lt;String&gt;&gt; mainAList = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
ArrayList&lt;String&gt; parse = new ArrayList&lt;&gt;();

try (Scanner filereader = new Scanner(System.in)) {
  while (filereader.hasNextLine()) {
    String line = filereader.nextLine();
    if (line.length() == 0 || !filereader.hasNextLine()) {
        // if the file reader detects empty line or has no more lines
        // the arraylist called parse gets added to the mainAList
        mainAList.add(parse);
        // and then a new parse-list is created for the next group of inputs:
        parse = new ArrayList&lt;&gt;();
    } else{
        parse.add(line);
    }
  }
}

Try it online.

答案2

得分: 0

尝试这个,而不是每次都清空列表。

ArrayList<ArrayList<String>> mainAList = new ArrayList<ArrayList<String>>();

try (Scanner filereader = new Scanner(Paths.get("name_of_file.txt"))) {
    while (filereader.hasNextLine()) {
        ArrayList<String> parse = new ArrayList<>();
        parse.add(filereader.nextLine());
        if (parse.get(parse.size()-1).length() == 0 || !filereader.hasNextLine()) {
            mainAList.add(parse);
        }
    }
}
英文:

Try this instead of clearing the list everytime.

ArrayList&lt;ArrayList&lt;String&gt;&gt; mainAList = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();

try (Scanner filereader = new Scanner(Paths.get(&quot;name_of_file.txt&quot;))) {
    while (filereader.hasNextLine()) {
    	ArrayList&lt;String&gt; parse = new ArrayList&lt;&gt;();
        parse.add(filereader.nextLine());
        if (parse.get(parse.size()-1).length() == 0 || !filereader.hasNextLine()) {
         mainAList.add(parse);
        }
   }
}

答案3

得分: 0

你的问题在于最终mainAList会变为空,因为对象属于引用类型。为了解决这个问题,你需要向mainAList中添加parse的副本,而不是直接添加parse数组列表本身。

将这行代码:

mainAList.add(parse);

替换为这行:

mainAList.add(new ArrayList<>(parse));
英文:

Your problem is that your mainAList will be empty in the end because objects are reference types. In order to fix that problem, you have to add a copy of parse to the mainAList and not the parse arraylist itself

replace this line

mainAList.add(parse);

with this

mainAList.add(new ArrayList&lt;&gt;(parse));

huangapple
  • 本文由 发表于 2020年9月17日 21:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63938580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定