由於某種原因,我收到此錯誤。獲取數組索引超出界限錯誤
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Assignment25.main(Assignment25.java:80)
public static void main (String[] args){
long[] array = new long[7];
for (int i = 0; i < 6; i++){
int thefib = 39;
array[i] = fib(thefib);
thefib++;
}
int counter = 0;
int counter1 = 1;
for(int i = 0; i < 6; i++){
long start = System.currentTimeMillis();
gcd(array[counter], array[counter1]);
long end = System.currentTimeMillis();
long time = end - start;
System.out.println("GCD time for " + (counter + 39) + " and " + (counter1 +
39) + " is " + time);
counter++;
counter1++;
}
counter = 0;
counter = 1;
for(int i = 0; i < 6; i++){
long start1 = System.currentTimeMillis();
gcd2(array[counter], array[counter1]);
long end1 = System.currentTimeMillis();
long time1 = end1 - start1;
System.out.println("GCD2 time for " + (counter + 39) + " and " + (counter1 +
39) + " is " + time1);
counter++;
counter1++;
}
}
}
的原因是,你正在使用的陣列外的索引。 – m0skit0