2014-10-01 61 views
0

,我越来越如何解决数组索引超出界限的错误?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 610 
at Fib.sorted(Fib.java:67) 
at Fib.main(Fib.java:17) 

我的代码

public class Fib 
{ 
    public static void main(String args[]) 
    { 
     System.out.println(Arrays.toString(fiblist)); 
     System.out.println(Fib.add()); 
     System.out.println(Fib.square()); 
     System.out.println(Fib.reversal()); 
     System.out.println(Fib.sorted()); 
    } 

    public static int fiblist[] = {1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765}; 
    public static int fiblen = fiblist.length; 

    public Fib() 
    { 
     // Do nothing 
    } 

    public static ArrayList<Integer> sorted() 
    { 
     ArrayList sorted = new ArrayList(); 

     for(int counter = 0; counter < fiblist[4]; counter++) 
     { 
      int temp1 = fiblist[counter]; 
      System.out.println("Elements stored " + temp1); 
     } 
     for(int counter = fiblist[14]; counter < fiblist[19]; counter++) 
     { 
      int temp2 = fiblist[counter]; 
      System.out.println("Last Elements stored " + temp2); 
     } 
     return sorted; 
    } 
} 

我想我的数组的最后5个元素存放在温度2 然后我会切换他们的错误。 有没有更简单的方法来做到这一点? 用最后五个数组切换数组的前五个元素? 你将如何使用for循环切换它们?

+1

你'counter'应该是在数组中的位置,而不是价值那个位置。将counter 2014-10-01 19:39:32

+0

这看起来像麻烦'int counter = fiblist [14];计数器 gtgaxiola 2014-10-01 19:39:47

+0

而且'counter 2014-10-01 19:40:08

回答

0

这工作

for(int i=0;i<fiblist.length;i++){ 
    System.out.print(fiblist[i]+","); 
} 
System.out.println(); 

for (int i=0;i<5;i++){ 
    temp=fiblist[i]; 
    fiblist[i]=fiblist[fiblist.length-i-1]; 
    //the first ellement= the last 
    //the second=second from last... 
    fiblist[fiblist.length-1-i]=temp; 
} 

for(int i=0;i<fiblist.length;i++){ 
    System.out.print(fiblist[i]+","); 
} 

输出:

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765, 
6765,4181,2584,1597,987,8,13,21,34,55,89,144,233,377,610,5,3,2,1,1, 
+0

临时只显示'运动'? – 2014-10-02 20:51:40

+0

检查更正的代码 – 2014-10-03 08:52:11

1

您正在混淆数组索引和值。 fiblist [19]是6765.你希望你的计数器从0到4和14到19,而不是fiblist [19]。

for(int counter = 0; counter < 4; counter++) 
{ 
    int temp1 = fiblist[counter]; 
    System.out.println("Elements stored " + temp1); 
} 

for(int counter = 14; counter < 19; counter++) 
{ 
    int temp2 = fiblist[counter]; 
    System.out.println("Last Elements stored " + temp2); 
} 
+1

不要只复制和粘贴OP的格式,并用代码纠正问题,修复格式,以便人们不必编辑问题和答案。 – bcsb1001 2014-10-01 19:41:40

+0

@ bcsb1001完成。 – 2014-10-01 20:03:31

+0

这部分运行。但是如何将temp1移动到数组的末尾?或temp2到数组的开头? – 2014-10-02 20:47:06

0

试试这个。这是一个排序算法(A一个比较差的,虽然)

public static void sort(int[] a) { 
    int iMin; 
    int n = a.length; 
    for (int j = 0; j < n-1; j++) { 
     iMin = j; 
     for (int i = j+1; i < n; i++) { 
      if (a[i] < a[iMin]) { 
       iMin = i; 
      } 
     } 
     if(iMin != j) { 
      swap(j, iMin, a); 
     } 
    } 
} 

public static void swap(int i, int j, int[] arr){ 
    int temp = arr[i]; 
    arr[i] = arr[j]; 
    arr[j] = temp; 
}