如何 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
how to java.lang.ArrayIndexOutOfBoundsException?
提问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 roundArray
has length 64, but inside the methods roundKeyGenerator
, parityDrop
and compressionPBox
you have arrays of length 56.
您的阵列roundArray
具有长度64,但是这些方法内roundKeyGenerator
,parityDrop
并且compressionPBox
你有长度56的阵列。
You are calling the method parityDrop
with k
and then loop over it. In parityDrop
you call compressionPBox
with the variable i
, which will be between 0 and 63 (the length of key
). But in compressionPBox
you're returning cell[i]
, which will ofcourse fail when i
is >= 56.
您正在调用该方法parityDrop
,k
然后循环遍历它。在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
. 这是它如何到达那里:
main
callsroundKeyGenerator
passing inroundArray
(anint[64]
) for thek
argument.roundKeyGenerator
callsparityDrop
, passing ink
for thekey
argument.parityDrop
loops from0
(inclusive) tokey.length
(exclusive), and so will loop from0
..63
(inclusive), callingcompressionPBox
with each of those values for thei
argument.compressionPBox
usesi
to index intocell
, which is anint[56]
.
main
调用roundKeyGenerator
传入roundArray
(anint[64]
) 作为k
参数。roundKeyGenerator
调用parityDrop
,传递k
的key
参数。parityDrop
从0
(包含)到key.length
(不包含)循环,因此将从0
..63
(包含)循环,调用参数的compressionPBox
每个值i
。compressionPBox
用于i
索引到cell
,这是一个int[56]
.
...and so that's why your ArrayIndexOutOfBoundsException
is reporting an error on index 56. Once i
reaches 56, you can't index into cell
with that value anymore.
...这就是为什么您ArrayIndexOutOfBoundsException
在索引 56 上报告错误的原因。一旦i
达到 56,您将无法cell
再使用该值进行索引。