[英]I want to know how to add set of string type arraylists into an arraylist
I want to add a set of string type arraylist objects into a another arraylist to store that string type arraylist objects. 我想将一组字符串类型的arraylist对象添加到另一个arraylist中,以存储该字符串类型的arraylist对象。
ex: [[<, int, char], [12, &&, text], [<<, ||], [., .]]
例如:
[[<, int, char], [12, &&, text], [<<, ||], [., .]]
I have created a string type arraylist to store string values. 我创建了一个字符串类型arraylist来存储字符串值。 And another ArrayList type arraylist to store string type arraylists.
另一个ArrayList类型的arraylist用于存储字符串类型的arraylist。
I give file path as input. 我将文件路径作为输入。 That file has content like this
该文件具有这样的内容
while (j < 6 || j > 90) {
i = 1;
while (i < 6) {
System.out.print("* ");
i++;
}
System.out.println();
j++;
}
switch ( rt ) {
case t1:
case 12:
case e3:
case 43e:
}
}
csCalculator sizeObject = new csCalculator();
List<Integer> csMarkList = new ArrayList<Integer>();
ArrayList<String> singleLineIdentiedTokens = new ArrayList<>();
Collection<ArrayList<String>> allLineIdentiedTokens = new ArrayList<>();
int returnCsMark = 0;
for(String obj:list) {
singleLineIdentiedTokens = (ArrayList<String>) sizeObject.calcCsOfStatement(obj);
csMarkList.add(singleLineIdentiedTokens.size());
allLineIdentiedTokens.add(singleLineIdentiedTokens);
System.out.println(singleLineIdentiedTokens);
System.out.println(allLineIdentiedTokens);
if (!(singleLineIdentiedTokens.isEmpty())) {
singleLineIdentiedTokens.clear();
}
}
System.out.println("CS MARK OF STATEMENT");
System.out.println(singleLineIdentiedTokens);
System.out.println(csMarkList);
I want expected an array like this. 我希望期望这样的数组。 [[<, int, char], [12, &&, text], [<<, ||], [., .]]
[[<,int,char],[12,&&,文本],[<<,||],[。,。]]
But it gives an array with last string arraylist replacing all the previous arraylist objects like this. 但是它提供了一个具有最后一个字符串arraylist的数组,该数组替换了所有以前的arraylist对象,就像这样。
[[., .], [., .], [., .], [., .]]
I want correct that replacing defect. 我想纠正那个替换缺陷。
You're repeatedly adding singleLineIdentiedTokens
to allLineIdentiedTokens
. 您反复将
singleLineIdentiedTokens
添加到allLineIdentiedTokens
。 This means that all these adds refer to the same ArrayList. 这意味着所有这些添加都引用相同的ArrayList。 Your
println(allLineIdentiedTokens)
will print something like this: 您的
println(allLineIdentiedTokens)
将打印如下内容:
[singleLineIdentiedTokens.toString(), singleLineIdentiedTokens.toString(), ... ]
That is exactly what you're seeing, as you get: [[., .], [., .], [., .], [., .]]
. 这就是您所看到的,即:
[[., .], [., .], [., .], [., .]]
。 As you can see, the only value being kept is the last value assigned to singleLineIdentiedTokens
. 如您所见,唯一保留的值是分配给
singleLineIdentiedTokens
的最后一个值。 You can remedy this by altering the code as so: 您可以通过更改代码来纠正此问题:
... //logic
for(String obj:list) {
singleLineIdentiedTokens = (ArrayList<String>) sizeObject.calcCsOfStatement(obj);
csMarkList.add(singleLineIdentiedTokens.size());
allLineIdentiedTokens.add(new ArrayList<>(singleLineIdentiedTokens));// <- change this line
System.out.println(singleLineIdentiedTokens);
System.out.println(allLineIdentiedTokens);
if (!(singleLineIdentiedTokens.isEmpty())) {
singleLineIdentiedTokens.clear();
}
}
... //more logic
What this does is keep a copy of singleLineIdentiedTokens
at the current point in time, instead of repeatedly adding one reference that could change in value later. 这样做是在当前时间点保留
singleLineIdentiedTokens
的副本,而不是重复添加一个引用,该引用以后可能会改变值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.