英文:
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<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()) {
// 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'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
你目前的方法存在两个问题:
parse
会对所有的内部 ArrayList 使用相同的引用,因此当你调用parse.clear()
时,会清空mainAList
中所有内部列表的内容。可以通过在添加时创建parse
列表的副本来解决这个问题(即mainAList.add(new ArrayList<> (parse));
)。- 在 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:
parse
will use the same reference for all inner ArrayLists, so when you callparse.clear()
it will empty all inner lists of yourmainAList
. This can be solved by creating a copy of theparse
list when you add it (i.e.mainAList.add(new ArrayList<>(parse));
)- 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<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()) {
// 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<>();
} else{
parse.add(line);
}
}
}
答案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<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);
}
}
}
答案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<>(parse));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论