Java坐标数组错误

时间:2016-04-21 14:26:39

标签: java

enter image description here

线程中的异常" 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]]; 
}

1 个答案:

答案 0 :(得分: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中使用调试工具来了解问题的根源。 使用调试工具,您可以知道发生了什么和变量值