We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › using array append() with subclasses
Page Index Toggle Pages: 1
using array append() with subclasses (Read 1058 times)
using array append() with subclasses
Nov 9th, 2009, 2:02pm
 
In the following code both RectTarget and CircleTarget are child classes of BaseTarget.  I'm declaring them as BaseTarget in the first instance as I was hoping that would fix a problem I'm having:  I want to be able to join any created targets into a single array to pass as a parameter to a class constructor.  I'm trying to do this with append() but it's throwing an error:

IllegalArgumentException: array element type mismatch.

Either I'm not understanding the syntax or it doesn't care that the objects are derived from the same parent class.

Presumably I wouldn't have the same problem with an ArrayList?  That would mean rewriting the classes to use ArrayLists instead of normal arrays and I'm feeling lazy... but I'll do it if it will fix the problem.

Any suggestions?

Code:
BaseTarget bar;
int numTargets = 3;
BaseTarget[] targets = new RectTarget[numTargets];
BaseTarget[] allTargets;

void setup() {
 size(400,400);
 
 bar = new CircleTarget(67,340,50);
 
 for (int i=0; i<numTargets; i++) {
   targets[i] = new RectTarget(30, 10 + i*100, 75, 75);
 }
 
 // This throws an error:
 allTargets = (BaseTarget[])append(targets, bar);
 
}
Re: using array append() with subclasses
Reply #1 - Nov 9th, 2009, 11:54pm
 
I don't recognize the (BaseTarget[]) bit.  what happens if you remove that?

allTargets = append(targets. bar);
Re: using array append() with subclasses
Reply #2 - Nov 10th, 2009, 12:52am
 
If you remove it you get:

"cannot convert from Object to dragDrop05.BaseTarget[]"

But that's to be expected:

from append()

Quote:
When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) append(originalArray, element).


... but that's what I'm trying and it doesn't work.  I've actually implemented the ArrayList alternative - not as much work as I expected - but I'm still curious to know if this is just me misunderstanding or a bug/limitation of append.
Re: using array append() with subclasses
Reply #3 - Nov 10th, 2009, 4:59am
 
blindfish wrote on Nov 9th, 2009, 2:02pm:
Any suggestions

Don't be lazy!  Grin
Seriously, ArrayList is conceptually better when you start using classes.
Re: using array append() with subclasses
Reply #4 - Nov 10th, 2009, 6:59am
 
As I said - I already built the ArrayList version and I agree it's definitely better Smiley

I just wondered whether this error had been caused by a misunderstanding on my part or if it is in fact a limitation or bug in append().  If I get a minute I'll write some code to post here to demonstrate the problem...
Re: using array append() with subclasses
Reply #5 - Nov 10th, 2009, 2:13pm
 
OK - append() works fine with child classes passed to an array of the type Parent class...  I'd just declared one of my arrays of the parent class but instantiated an array of the type child class:

Code:
BaseTarget[] targets = new RectTarget[numTargets];
// <snip>
for (int i=0; i<numTargets; i++) {
   targets[i] = new RectTarget(30, 10 + i*100, 75, 75);
 }


Changing it to:

Code:
BaseTarget[] targets = new BaseTarget[numTargets];
// <snip>
for (int i=0; i<numTargets; i++) {
   targets[i] = new RectTarget(30, 10 + i*100, 75, 75);
 }


...fixes the problem.  So in theory I could have used arrays, but I think I'm going to stick to ArrayLists now Smiley
Page Index Toggle Pages: 1