Hi!
I've made a simple code, which uses
System.arraycopy, but it constantly throws
ArrayStoreException, even though I can't see any reason for this.
(I've even run through it and watched the variables at breakpoints.) It seems as if it cannot convert Integer to Integer, but why?
My code is here:
import java.lang.reflect.Array;
public class ArrayHandler {
public static void main(String[] args) {
try {
Integer[] startArray=new Integer[]{new Integer(1)};
Integer[] endArray=new Integer[]{new Integer(2)};
Object[] result=new ArrayHandler().appendArray(startArray, endArray);
}catch(Throwable exception){
exception.printStackTrace();
}
}
/**
*
http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29
...
Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:
* The src argument refers to an object that is not an array.
* The dst argument refers to an object that is not an array.
* The src argument and dst argument refer to arrays whose component types are different primitive types.
* The src argument refers to an array with a primitive component type and the dst argument refers to an array with a reference component type.
* The src argument refers to an array with a reference component type and the dst argument refers to an array with a primitive component type.
...
Otherwise, if any actual component of the source array
from position srcOffset through srcOffset+length-1
cannot be converted to the component type of the destination array by assignment conversion,
an ArrayStoreException is thrown.
...
In this case, let k be the smallest nonnegative integer less than length
such that src[srcOffset+k] cannot be converted to the component type of the destination array;
when the exception is thrown,
source array components from positions srcOffset through srcOffset+k-1
will already have been copied to destination array positions dstOffset through dstOffset+k-1
and no other positions of the destination array will have been modified.
(Because of the restrictions already itemized,
this paragraph effectively applies only to the situation
where both arrays have component types that are reference types.)
*/
public Object[] appendArray(Object[] startArray,Object[] endArray) {
Object[] outputArray=startArray;
Class startArrayClass=startArray.getClass();
System.out.println(startArrayClass);
Class endArrayClass=startArray.getClass();
System.out.println(endArrayClass);
if(startArrayClass !=endArrayClass){
//throw new Exception("startArrayClass !=endArrayClass");
}else{
try{
outputArray=(Object[])Array.newInstance(startArrayClass, startArray.length+endArray.length);
System.arraycopy(startArray,0,outputArray,0,startArray.length);
System.arraycopy(endArray,0,outputArray,startArray.length,endArray.length);
return outputArray;
}catch(Throwable exception){
exception.printStackTrace();
}
}
return outputArray;
}//appendArray
}
Could anybody share his experience, please.
My best regards.