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 › append() on my class
Page Index Toggle Pages: 1
append() on my class (Read 404 times)
append() on my class
Aug 13th, 2008, 7:01pm
 
Hi,

i have a problem with the append function.

int[] pp = new int[0];
MyClass[] cc = new MyClass[0];

when i do that it return me an error, the append() with the int variable works, with my class no.

Why? How can i use an array(with dinamic size) of a general class?

pp = append(pp,element); // it works!!!
cc = append(cc,element); // doesn't works
Re: append() on my class
Reply #1 - Aug 13th, 2008, 8:07pm
 
Hi

Seems strange, is there something fishy with your Class declaration?
This is something copied from a project of mine:

Worm[] wormArray = new Worm[maxWorms];

My worm class is just:

class Worm{
 float xoff, yoff, roff;
 float posX, posY;
 float radMax = 0;
 float radius = 0;

 Worm(float _x, float _y, float _rad){
   radMax = _rad;
   xoff = _x;
   yoff = _y;
   roff = 0;
 }


I sometimes go with the ArrayList instead:

Quote:


class PlasmaSys{

 ArrayList plasma;
 float radMax = 50;

 int numOfPlasma;

 PlasmaSys(int plasmaCount, float _offX, float _offY ){
   numOfPlasma = plasmaCount;
   plasma = new ArrayList();
   for( int i = 1; i <= plasmaCount; i++ ){
     plasma.add(new Plasma( width/2, height/2, 150, i, _offX, _offY));
   }  
 }

 void run(){
   for( int i = plasma.size()-1; i >= 0; i-- ) {
     Plasma p = (Plasma) plasma.get(i);
     p.run();
   }
 }
}





I can't spot the problem, but try to check your real code or post it (Processing -> Tools -> Copy for Discourse)

Ricki
Re: append() on my class
Reply #2 - Aug 14th, 2008, 1:53am
 
Quote:
the append() with the int variable works, with my class no.

Why?

See append() reference :
When using an array of objects, the data returned from the function must be cast to the object array's data type.
http://processing.org/reference/append_.html

Code:
cc = (MyClass[])append(cc, element); 

Re: append() on my class
Reply #3 - Aug 15th, 2008, 8:13am
 
This post may help explain why a bit more:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1218148740;start=4#4
Re: append() on my class
Reply #4 - Aug 15th, 2008, 3:40pm
 
yep, i just forgot to cast it into my class type, thanks a lot :]
Page Index Toggle Pages: 1