-4

enter image description here

Exception in thread "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]]; 
}
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
sha83
  • 1
  • 3
  • plz post the codes not images .still same as previous question .no improvement – Madhawa Priyashantha Apr 21 '16 at 14:29
  • 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]]; – sha83 Apr 21 '16 at 14:33
  • Don't put your code in a comment. Put it in the question. – khelwood Apr 21 '16 at 14:35
  • I have an edit in to add it in the question – Lexi Apr 21 '16 at 14:36

1 Answers1

0

plz edit your post and put all your codes not images or one method. then you have to isolate your operations (to organize and to detect errors): ex :

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

to

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];
 }

And you should use a debug tool in eclipse to know the source of the problem. with debug tools you can know what happen and variable values

h.zak
  • 1,407
  • 5
  • 21
  • 40