如何 java.lang.ArrayIndexOutOfBoundsException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4451799/
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-10-30 06:26:00  来源:igfitidea点击:

how to java.lang.ArrayIndexOutOfBoundsException?

javaarrays

提问by Gain

i tried to fix the problem multiple time but i didn't get the proble that produces the exception which is "--------------------Configuration: -------------------- E

我试图多次解决这个问题,但我没有得到产生异常的问题,即“--------------------配置:----- --------------- E

xception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 56
    at testing.compressionPBox(testing.java:61)
    at testing.parityDrop(testing.java:39)
    at testing.roundKeyGenerator(testing.java:23)
    at testing.main(testing.java:14)"

simply my program creates an array of 64 length ,then fill it by value from 1 to 64 then use the method roundKeyGenerator(roundArray) ; to executes the method ,when the method executes it creates an array of 56 length then use parityDrop method to return an array of length 56 form the initial array which has length of 64 ,but how to creates and drop some indexes !! simply i creates a predefined table which when i call the table method by index from initially start from 0 the method takes this index and sees what is the value of table_cell[index]=value_X then return value_x to the caller metod and put vaule_X as an index of the intailArray which has the size 64 and takes its value ,then stores it in the returned array depending where did its pointer stop and i think the pointers is the problem i think the second array of 56 length exceed the limit then the exception produced.

只是我的程序创建了一个长度为 64 的数组,然后按从 1 到 64 的值填充它,然后使用方法 roundKeyGenerator(roundArray) ;要执行该方法,当该方法执行时,它会创建一个长度为 56 的数组,然后使用 parityDrop 方法从长度为 64 的初始数组中返回一个长度为 56 的数组,但是如何创建和删除一些索引!!只是我创建了一个预定义的表,当我从最初从 0 开始按索引调用 table 方法时,该方法采用该索引并查看 table_cell[index]=value_X 的值是什么,然后将 value_x 返回给调用者方法并将 vaule_X 作为intailArray 的索引,其大小为 64 并取其值,

   static int [] roundArray=new int [64];    
    public static void main(String [] arg) 
         { 
          for(int i=0;i<roundArray.length;i++)
           roundArray[i]=i+1;
          roundKeyGenerator(roundArray) ;      
         } 



     public static int [] roundKeyGenerator(int [] k) //takes an int array of length 64 repesents the key in binary representation
        {
         int [] key=new int [56];
         key=parityDrop(k); 
         return key;
        }
        public static int [] parityDrop(int [] key)//takes an int array of length 64 repesents the key and return the key in an arrayof length 56 
        {
         int index;
         int [] result=new int[56];
          for(int i=0;i<key.length;i++)
          { 
           index=compressionPBox(i);//return cell value from straightPBoxTable
           result [i]= key[index-1];
          }

         return result;

        }
        public static int compressionPBox(int i){

          int cell[] =new int [56];
          cell[0] =57 ;cell[1] =49 ;cell[2]=41  ;cell[3] =33 ;cell[4] =25 ;cell[5] =17 ;cell[6] =9 ;cell[7] =1;  
          cell[8] =58 ;cell[9] =50 ;cell[10]=42 ;cell[11]=34 ;cell[12]=26 ;cell[13]=18 ;cell[14]=10 ;cell[15]=2;
          cell[16]=59 ;cell[17]=51 ;cell[18]=43 ;cell[19]=35 ;cell[20]=27 ;cell[21]=19 ;cell[22]=11 ;cell[23]=3;
          cell[24]=60 ;cell[25]=52 ;cell[26]=44 ;cell[27]=36 ;cell[28]=63 ;cell[29]=55 ;cell[30]=47 ;cell[31]=39;
          cell[32]=31 ;cell[33]=23 ;cell[34]=15 ;cell[35]=7 ;cell[36]=62 ;cell[37]=54 ;cell[38]=46 ;cell[39]=38;
          cell[40]=30 ;cell[41]=22 ;cell[42]=14 ;cell[43]=6 ;cell[44]=61 ;cell[45]=53 ;cell[46]=45 ;cell[47]=37;
          cell[48]=29 ;cell[49]=21 ;cell[50]=13 ;cell[51]=5 ;cell[52]=28 ;cell[53]=20 ;cell[54]=12 ;cell[55]=4;
         return cell[i];
         }// predefined permutation rule

回答by Falmarri

  for(int i=0;i<key.length;i++)
  { 
   index=compressionPBox(i);//return cell value from straightPBoxTable
   result [i]= key[index-1];
  }

You can't index a negative array. index = 0 on the first loop. key[-1] is an error.

你不能索引一个负数组。在第一个循环中索引 = 0。key[-1] 是一个错误。

回答by Bozho

Your arrays are of different sizes - one with 56 and one with 64. There comes the problem.

你的数组有不同的大小——一个有 56 个,一个有 64 个。问题来了。

回答by Ferenc Szaszdi

package org.ferencszaszdi.sistemaNit;
public class SistemaNit {
     public static void main(String args[]) {
        String nit = "3894140-6";
    String valor[]={};        
    int num= nit.length();
        int num2 = num - 2;
        int numero = num;
        int in = 0;
        int va = 0;
        for(int inicio = 0;inicio<=num;inicio++) {
            valor[in] = nit.substring(in);
            in++;
    }
    int in2 = 0;
    for(int inicio2 = 0;inicio2<=num2;inicio2++) {
        int valor2 = Integer.parseInt(valor[in2]);
            int val = numero * valor2;
            va = va + val;
            numero--;
            in2++;
        }
        String residuo = nit.substring(num);
        int residuo2 = Integer.parseInt(residuo);
        int nume = va%11;
        if (nume == residuo2){
           System.out.println ("El nit es correcto");
        }
        else
        {
           System.out.println ("El nit es incorrecto");
         }
    }
}

回答by Jesper

Your array roundArrayhas length 64, but inside the methods roundKeyGenerator, parityDropand compressionPBoxyou have arrays of length 56.

您的阵列roundArray具有长度64,但是这些方法内roundKeyGeneratorparityDrop并且compressionPBox你有长度56的阵列。

You are calling the method parityDropwith kand then loop over it. In parityDropyou call compressionPBoxwith the variable i, which will be between 0 and 63 (the length of key). But in compressionPBoxyou're returning cell[i], which will ofcourse fail when iis >= 56.

您正在调用该方法parityDropk然后循环遍历它。在parityDrop您调用compressionPBox变量 时i,该变量将介于 0 和 63( 的长度key)之间。但是在compressionPBox您返回时cell[i],当i>= 56时,这当然会失败。

Note: To solve problems like this, carefully look at the stack trace of the exception. It tells you exactly at what line in what source file the error happens. Go to that spot in your source code and trace back what went wrong. You can also use a debugger to step through the code to follow what happens. IDEs such as Eclipse and NetBeans have very good built-in debuggers.

注意:要解决这样的问题,请仔细查看异常的堆栈跟踪。它准确地告诉您错误发生在哪个源文件的哪一行。转到源代码中的那个位置并追溯出错的地方。您还可以使用调试器单步调试代码以了解发生的情况。Eclipse 和 NetBeans 等 IDE 具有非常好的内置调试器。

回答by T.J. Crowder

Your error is in compressionPBox. Here's how it gets there:

你的错误在compressionPBox. 这是它如何到达那里:

  • maincalls roundKeyGeneratorpassing in roundArray(an int[64]) for the kargument.
  • roundKeyGeneratorcalls parityDrop, passing in kfor the keyargument.
  • parityDroploops from 0(inclusive) to key.length(exclusive), and so will loop from 0..63(inclusive), calling compressionPBoxwith each of those values for the iargument.
  • compressionPBoxuses ito index into cell, which is an int[56].
  • main调用roundKeyGenerator传入roundArray(an int[64]) 作为k参数。
  • roundKeyGenerator调用parityDrop,传递kkey参数。
  • parityDrop0(包含)到key.length(不包含)循环,因此将从0.. 63(包含)循环,调用参数的compressionPBox每个值i
  • compressionPBox用于i索引到cell,这是一个int[56].

...and so that's why your ArrayIndexOutOfBoundsExceptionis reporting an error on index 56. Once ireaches 56, you can't index into cellwith that value anymore.

...这就是为什么您ArrayIndexOutOfBoundsException在索引 56 上报告错误的原因。一旦i达到 56,您将无法cell再使用该值进行索引。