1
我正在創建一個ArrayList的Arraylist的ArrayList,並按照下面的方式填充它。它正確填充它。我通過調試和println
發現了這一點。但是,當我嘗試檢索這些整數時,它無法正常工作,並向我顯示文件的最後一個整數,我已經讀取了我的ArrayList變量的所有值。ArrayList的ArrayList的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
}
}
}