public class Testarraycopy
{
public static void main(String[] args)
{
int[] smallPrimes = {1,2,3,4,5,6,7,8,9};
int[] luckNumbers = {0,0,0,0,0,0};
System.arraycopy(smallPrimes,2,luckNumbers,3,4);
for(int i=0;i<luckNumbers.length;i++)
System.out.println(i+";"+luckNumbers[i]);
}
}
javac Testarraycopy.java 没问题java Testarraycopy
时提示
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at Testarraycopy.main(Testarraycopy.java:7)这是为什么啊!?
初学java 还望个位大侠多多指教!!谢谢!!!
{
public static void main(String[] args)
{
int[] smallPrimes = {1,2,3,4,5,6,7,8,9};
int[] luckNumbers = {0,0,0,0,0,0};
System.arraycopy(smallPrimes,2,luckNumbers,3,4);
for(int i=0;i<luckNumbers.length;i++)
System.out.println(i+";"+luckNumbers[i]);
}
}
javac Testarraycopy.java 没问题java Testarraycopy
时提示
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at Testarraycopy.main(Testarraycopy.java:7)这是为什么啊!?
初学java 还望个位大侠多多指教!!谢谢!!!
* @param srcPos starting position in the source array.
* @param dest the destination array.
* @param destPos starting position in the destination data.
* @param length the number of array elements to be copied.
* @exception IndexOutOfBoundsException if copying would cause
* access of data outside array bounds.
* @exception ArrayStoreException if an element in the <code>src</code>
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
* @exception NullPointerException if either <code>src</code> or
* <code>dest</code> is <code>null</code>.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);源数组,起始位置,目标数组,起始位置,拷贝长度以上是arraycopy的五个参数你的luckNumbers长度是6,从第3个开始,只能拷贝length=3,你的代码却拷贝了4个,所以出现了运行期错误