为什么我会在返回的数据中得到差异.它工作正常,直到3,然后繁荣,它混乱到最后.我在做什么很简单.我从带有id,name的txt文件中获取值,并将id与另一个具有name,name的txt文件匹配,使其看起来像id,id,如下所示.但是,它并不像您期望的那样有效.匹配正在进行,直到它搞砸了.
0,1
1,3
0,2
0
3,0,4
2
3,0
4,2
4,1
2,
while ((output2 = br2.readLine()) != null) {
String[] vv = output2.split(",");
String value1 = vv[0];
String value2 = vv[1];
for (Map.Entry<Integer, String> entry : map.entrySet()) {
int key = entry.getKey();
String value = entry.getValue();
//System.out.println(key+","+value);
if ((value1.equals(value))) {
System.out.print(key + ",");
}
if ((value2.equals(value))) {
System.out.print(key + "\n");
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据
ids.txt
0,Triple H
1,John Cena
2,Megan Fox
3,The Undertaker
4,Pamela Anderson
5,The Rock
Run Code Online (Sandbox Code Playgroud)
的text.txt
Triple H,John Cena
John Cena,The Undertaker,
Triple H,Megan Fox
The Undertaker,Triple H
Run Code Online (Sandbox Code Playgroud)
在你的第四次入场
String value2 = vv[1];
Run Code Online (Sandbox Code Playgroud)
vv[1] 将为null,因为在第一个逗号后没有值.
编辑:
在你的第四个条目
The Undertaker,Triple H
Run Code Online (Sandbox Code Playgroud)
value2 (i.e. Triple H)first iteration在循环中匹配zeroth position,所以它用a打印\n,然后entry 3 is matched在4rd iteration打印中打印next line with a comma
这就是你得到输出的原因
0
3,0,4
Run Code Online (Sandbox Code Playgroud)