我正在创建一个 ArrayList of ArrayList of ArrayList 并按以下方式填充它。它正确填充它。我已经通过调试和println
. 但是,当我尝试检索这些整数时,它无法正常工作,并显示文件中的最后一个整数,我已经为 ArrayList 变量的所有值读取了该整数。
下面是我填充的代码,它运行良好。
Scanner sc = new Scanner(new File("e1.txt"));
Scanner lineSc;
String lineStr;
String lineRegEx = "(\\d+),(\\d+)";
Pattern linePattern = Pattern.compile(lineRegEx);
Matcher matcher;
Integer vertex = 0, edge = 0, length = 0;
String strE, strL ;
ArrayList<Integer> tmpLE = new ArrayList<Integer>() ;
ArrayList<ArrayList<Integer>> singleV = new ArrayList<ArrayList<Integer>>() ;
sc.useDelimiter("\\n");
int i = 0, j = 0;
while (sc.hasNextLine()) {
lineStr = sc.nextLine();
lineSc = new Scanner(lineStr);
lineSc.nextInt();
matcher = linePattern.matcher(lineStr);
while (matcher.find()) {
strE = matcher.group(1);
edge = Integer.parseInt(strE);
tmpLE.add(EI, edge);
strL = matcher.group(2);
length = Integer.parseInt(strL);
tmpLE.add(LI, length);
singleV.add(j ,tmpLE);
graph.add(i, singleV);
//System.out.println (graph.get(i).get(j).get(0));
//System.out.println (graph.get(i).get(j).get(1));
++j;
}
j = 0;
++i;
lineSc.close();
}
sc.close();
}
这是我用来检索效果不佳的代码
for (int i = 0; i < N; ++i) {
Integer minLen = graph.get(i).get(0).get(LI);
System.out.println (minLen);
Integer choosenEdge = 0;
for (int j = 0; j < graph.get(i).size(); ++j) {
ArrayList<Integer> tmpArr = graph.get(i).get(j);
System.out.print(tmpArr.get(LI) + " " + minLen); //Wrong Output
if (minLen > tmpArr.get(LI))
{
minLen = tmpArr.get(LI);
choosenEdge = graph.get(i).get(j).get(EI);
System.out.println (choosenEdge); // Wrong Output
}
}
}