2016-04-21 60 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中使用调试工具知道源的问题。你可以知道发生了什么,并且可以得到变量值