Bizarre typing exception when using Arrays.copyOf

I have a class A, and two subclasses of A called B and C.

When I try and use the following method I created, with arguments (B[], C), I get an ArrayStoreException. This would suggest that types are clashing, but I don't see how that's possible.

Could anyone enlighten me?

publicstatic A[] arrayAppend(A[] detailsArray, A details){

A[] newDetailsArray = Arrays.copyOf(detailsArray, detailsArray.length + 1);

newDetailsArray[detailsArray.length] = details;

return newDetailsArray;

}

[684 byte] By [KomodoDavea] at [2007-11-26 15:14:11]
# 1
A[] newDetailsArray = (A[]) Arrays.copyOf(detailsArray, detailsArray.length + 1);?
quittea at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 2
I don't see an Arrays.copyOf() method in the Arrays class - am I missing something?
jbisha at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 3
Ok, so the arguments you are passing to the method are an array of B objects and a C object.This obviously will not work if B and C are both subclasses of A. You cannot store a C object into an array of B objects, because B is not a subclass of C.
jesperdja at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 4
Oh... I thought that because I'm specifying it to deal with A arrays, that would bypass the problem.If, instead of using Arrays.copyOf, I manually copy the elements over to a new A[], that should work then, shouldn't it?
KomodoDavea at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 5
> I don't see an Arrays.copyOf() method in the Arrays> class - am I missing something?You are using a Java version older than Java 6.
jesperdja at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 6

Awesome, I've just changed it to the code below, and it works great.

Thank you everyone :o)

public static A[] arrayAppend(A[] detailsArray, A details) {

A[] newDetailsArray = new A[detailsArray.length + 1];

int i = 0;

for (; i < detailsArray.length; i++)

newDetailsArray[i] = detailsArray[i];

newDetailsArray[i] = details;

return newDetailsArray;

}

KomodoDavea at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 7
> > I don't see an Arrays.copyOf() method in the> Arrays> > class - am I missing something?> > You are using a Java version older than Java 6.That makes sense - thanks.
jbisha at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 8

Your B[] array is really an array of B objects. Even if you create an array of B objects and assign it to a variable of type A[]:

A[] array = new B[10];

then the array is still an array of B objects, and you can't put a C object into it (because C is not a subclass of B). You will have to make your array an array of A objects:

A[] array = new A[10];

Ofcourse, you can then assign B objects to the elements, because B is a subclass of A:

array[0] = new B();

And also assigning C objects will work, because C is also a subclass of A:

array[1] = new C();

And your copy method will also work.

jesperdja at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...
# 9
I see, ok. Thanks for the explanation jesperdj ;o)
KomodoDavea at 2007-7-8 9:05:44 > top of Java-index,Java Essentials,Java Programming...