2016-04-21 74 views
-4

enter image description hereJava的座標誤差與陣列

異常線程「main」 java.lang.ArrayIndexOutOfBoundsException

for (int i = 0; i < size; i++) { for (int j = i; j < size; j++) { 
    distances[i][j] = haversin(coords[i][0], coords[j][0], coords[i][1], 
    coords[j][1]); distances[j][i] = distances[i][j]; } } long start = 
    System.nanoTime(); int[] shortestPath = nearestNeighbour(distances); double 
    bestShort = 0; for (int i = 0; i < size; ++i) {bestShort += 
    distances[shortestPath[i] - 1][shortestPath[i + 1] - 1]; } bestShort += 
    distances[shortestPath[size - 1]][shortestPath[0]]; 
} 
+0

plz發佈代碼不是圖像,仍然與之前的問題相同。沒有改進 –

+0

for(int i = 0; i sha83

+0

不要將您的代碼置於評論中。把它放在問題上。 – khelwood

回答

0

PLZ編輯您的帖子,並把所有的代碼沒有圖像或一個方法。 那麼你必須隔離的操作(組織和檢測錯誤): 例如:

bestShort += distances[shortestPath[i] - 1][shortestPath[i+1]-1];

int shortestPath,shortestPath_1; 
for (int i = 0; i < size; ++i) { 
shortestPath=shortestPath[i] - 1; 
shortestPath_1=shortestPath[i + 1] - 1; 
bestShort += distances[shortestPath][shortestPath_1]; 
} 

你應該在Eclipse中使用調試工具知道源的問題。你可以知道發生了什麼,並且可以得到變量值