user3573302
user3573302

Reputation: 31

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method)

i'm doing some practice in Arrays using Intellij idea first time, But it's giving me this errors:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at ArrayDemo.main(ArrayDemo.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Why i'm getting this, will anyone please tell me the reason?

here is my program:

public class ArrayDemo{
    public static void main(String[] args){
        char[] copyFrom={'a', 'b', 'c', 'e', 'f', 'g', 'g'};
        char[] copyTo=new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        System.out.println(new String(copyTo));
    }
}

Upvotes: 1

Views: 4790

Answers (4)

user1134181
user1134181

Reputation:

As said in documentation:

public static void arraycopy(Object src,
             int srcPos,
             Object dest,
             int destPos,
             int length)

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.

Thus this code:

char[] copyFrom = {'a', 'b', 'c', 'e', 'f', 'g', 'g'};
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);

Can not be used to copy 7 elements. If you need to copy 7 elements, you can use the following example:

public class ArrayDemo{
    public static void main(String[] args) {
        char[] copyFrom = {'a', 'b', 'c', 'e', 'f', 'g', 'g'};
        char[] copyTo = new char[7];

        System.arraycopy(copyFrom, 0, copyTo, 0, 7);
        System.out.println(new String(copyTo));
    }
}

Output:

abcefgg

Upvotes: 2

erencan
erencan

Reputation: 3763

This code sample tries to copy 7 of copyFrom items starting by the index 2. However there is no item indexed 2+7 = 9 in the copyFrom array that's why you get ArrayIndexOutOfBoundsException.

Look at the parameters.

Parameters:
src the source array.
srcPos starting position in the source array.
dest the destination array.
destPos starting position in the destination data.
length the number of array elements to be copied.

Try this code:

public class ArrayDemo{
    public static void main(String[] args){
        char[] copyFrom={'a', 'b', 'c', 'e', 'f', 'g', 'g'};
        char[] copyTo=new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0,5);
        System.out.println(new String(copyTo));
    }
}

Upvotes: 2

Karibasappa G C
Karibasappa G C

Reputation: 2732

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

where

  • src -- This is the source array.
  • srcPos -- This is the starting position in the source array.
  • dest -- This is the destination array.
  • destPos -- This is the starting position in the destination data.
  • length -- This is the number of array elements to be copied.

So either change destination length to 5 like below

char[] copyFrom={'a', 'b', 'c', 'e', 'f', 'g', 'g'};
        char[] copyTo=new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 5);
        System.out.println(new String(copyTo));

or start source from 0 instead of 2 like below

char[] copyFrom={'a', 'b', 'c', 'e', 'f', 'g', 'g'};
        char[] copyTo=new char[7];

        System.arraycopy(copyFrom, 0, copyTo, 0, 5);
        System.out.println(new String(copyTo));

Upvotes: 0

Leon
Leon

Reputation: 12491

If you start copying from position 2 then you cannot copy 7 items, you can only copy 5

Upvotes: 4

Related Questions