java 如何修复数组索引越界错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26149182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:22:23  来源:igfitidea点击:

How to fix array index out of bounds error?

javaarraysindexoutofboundsexception

提问by Brian Dela Cruz

The error that I am getting

我得到的错误

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

My code

我的代码

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

I'm trying to store the last 5 elements of my array in temp 2. Then I will switch them. Is there an easier way to do this? Switch the first five elements of an array with the last five? How would you switch them with a for loop?

我正在尝试将数组的最后 5 个元素存储在 temp 2 中。然后我将切换它们。有没有更简单的方法来做到这一点?将数组的前五个元素与后五个元素交换?你将如何用 for 循环切换它们?

回答by Guillaume CR

You are confusing array index and value. fiblist[19] is 6765. You want your counters to go from 0 to 4 and 14 to 19, not fiblist[19].

您混淆了数组索引和值。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);
}

回答by Skaros Ilias

This works

这有效

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]+",");
}

Output:

输出:

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,

回答by cholewa1992

Try this. It's a sorting algorithm (A fairly poor one though)

试试这个。这是一种排序算法(虽然相当糟糕)

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