简体   繁体   English

我有一个异常“ java.lang.ArrayIndexOutOfBoundsException”的问题

[英]i have a problem with exception “java.lang.ArrayIndexOutOfBoundsException”

i have a problem with exception "java.lang.ArrayIndexOutOfBoundsException" i wrote a program which have src array of 48 length then processes it to take each 6 indexes to another array using method arrayCopy and print each dst array for me it works fine it prints each 6 indexes from the initial array but at the end i get an exception help please . 我有一个异常“ java.lang.ArrayIndexOutOfBoundsException”的问题,我编写了一个程序,该程序具有48个长度的src数组,然后使用方法arrayCopy处理它以将每个6个索引带到另一个数组,并为我打印每个dst数组,它可以正常打印初始数组中的每个6个索引,但最后我得到异常帮助。 the algorithm is just a test because i want to use the arrayCopy in another algorithm so i don't need suggestion to change the algorithm . 该算法只是一个测试,因为我想在另一种算法中使用arrayCopy,因此我不需要建议改变算法。 i hope it is clear fair enough 我希望这很公平

  public static void main(String [] arg) 
        {   
            int[] src = new int[48];
            for(int j=0;j<src.length;j++)
            {
                src[j]=j+1;
                System.out.print(src[j]+" ");
            }   
            System.out.println();
            int[] dst = new int[6]; 
            int from=0;
            for(int i=0;i<src.length;i++)
            {
                System.arraycopy(src, from, dst, 0, 6); // Copies 6 indexes from src starting at from into dst
                from=from+6;
                print(dst); 
                System.out.println();
            }



            } 

        public static void print(int [] dst)
        {
            for(int i=0;i<dst.length;i++)
                System.out.print(dst[i]+" ");   
        }

Try this: 尝试这个:

for(int i=0;i<src.length;i+=6)  // increment i by value 6

Or use from in the for expression: 或在中使用from进行表达:

for(int from=0; from<src.length; from+=6) {
    System.arraycopy(src, from, dst, 0, 6); 
    print(dst); 
    System.out.println();
}

在循环的最后一次迭代中,编写方式为+ 5 = 53(大于47)(因此超出了源范围)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有java.lang.ArrayIndexOutOfBoundsException - i have java.lang.ArrayIndexOutOfBoundsException 异常:java.lang.ArrayIndexOutOfBoundsException - Exception : java.lang.ArrayIndexOutOfBoundsException 不知道为什么我在线程“ main”中有此错误Exception java.lang.ArrayIndexOutOfBoundsException:0 - Dont know why I have this error Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 致命异常:主java.lang.ArrayIndexOutOfBoundsException: - FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 异常:java.lang.ArrayIndexOutOfBoundsException OraclePreparedStatement - Exception : java.lang.ArrayIndexOutOfBoundsException OraclePreparedStatement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM