Reputation: 31
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
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 positionssrcPos
throughsrcPos+length-1
in the source array are copied into positions destPos throughdestPos+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
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
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
Reputation: 12491
If you start copying from position 2 then you cannot copy 7 items, you can only copy 5
Upvotes: 4