1

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

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

ya it's working thanks.... but can you tell me inside (copyFrom, 2, copyTo, 0, 4) what's the use of 0 in this??
o is the starting position in destination array
2

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

5 Comments

How come above code works? destination length is 5 and starting at 0 but source lenght is 7 ?
okay one thing can you tell me, when i use (copyFrom, 2, copyTo, 2, 5) using 2 instead 0 it throws again that ArrayIndexOutOfBounds exception... why?
@user3573302 2+5 = 7, but the last index of 7 sized array is 6.
but this outputs only cefgg not whole array as source array starts from 2
a -> 0 index, b-> 1 index then c-> 2 index where c starts from 2 then the method copies 5 following items in the source array whose are "cefgg" as expected. @KaribasappaGC
2

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

Comments

0
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.