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 › Dynamically resizing arrays
Page Index Toggle Pages: 1
Dynamically resizing arrays? (Read 1840 times)
Dynamically resizing arrays?
Jul 19th, 2005, 10:14pm
 
Is it possible to dynamically resize arrays in Processing? Or must one create a new array and copy all the elements into it?
Re: Dynamically resizing arrays?
Reply #1 - Jul 19th, 2005, 10:37pm
 
Have you tried Java's Vector class?

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

That'll let you extend an array by using addElement etc.

I believe List (something like that) is a bit more restricted on functionality but will give you something closer to what you need and be slightly faster for it.

Here's the link to Sun's List doc
http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html
Re: Dynamically resizing arrays?
Reply #2 - Jul 19th, 2005, 11:11pm
 
That does appear to be the one I'm looking for. My next problem follows, then:

I am creating a class within processing (bone -- it's used for skeletal animation). Previously, I used a static Processing array with a maximum of some number of elements. Now, when I include and use Java's Vector class, it seems adding new elements to the list works fine -- my code goes right through that stage. However, it dies when I try to retrieve an element from the list.

The method in question is:
Code:
bone link (int which) { return children.elementAt(which); } 


The error seems to be as simple as: Vector.elementAt() returns a java.lang.Object, however, my method is supposed to return a bone. Is there a way to change the typing of this, so that it returns as it ought?

You'll have to forgive me, I've spent the last few years shying away from statically typed languages (and Java especially) for this very reason. Smiley

Code:
class bone {
// constants
color[] colors = { color(0, 0, 0), color(0, 0, 255), color(191, 0, 255), color(255, 0, 0) };

// variables
float x, y, z, a, b ,c;
Vector children;

// methods
bone (float x, float y, float z) { this.x = x; this.y = y; this.z = z; a = b = c; children = new Vector(); }
bone rotate (float a, float b, float c) { this.a = a; this.b = b; this.c = c; return this; }
bone link (bone child) { children.addElement(child); return this; }
bone link (int which) { return children.elementAt(which); }

void draw () {
pushMatrix();

rotateX(a); rotateY(b); rotateZ(c); translate(x, y, z);

stroke(0); line(0, 0, 0, -x, -y, -z);
fill(colors[len]); noStroke(); sphere(4);

for(int i = 0; i < len; ++i) children[i].draw();

popMatrix();
}
}


You can see the previous iteration in all it's glory at: http://www.rpi.edu/~laporj2/art/dynamic/processing/walkie/index.html
Re: Dynamically resizing arrays?
Reply #3 - Jul 20th, 2005, 6:07am
 
you just need to cast it back to being a bone object, which just means hinting to the language that you know what you're doing and that the object coming back is in fact of type "bone":

Code:
bone link(int which) { 
return (bone) children.elementAt(which);
}


once you get used to the typing, you may grow to like it. for scripting (usually perl) i personally prefer to go without the typing, but in p5/java it often saves me..
Re: Dynamically resizing arrays?
Reply #4 - Jul 20th, 2005, 1:48pm
 
Now that I see it, it makes sense. I always tend to typecast things as, for example, float(width) / 2; however that likely calls some sort of constructor overloaded for handling ints. This other way is a completely different beast that is able to provide the same effect, but does so a different way.

Thanks!
Re: Dynamically resizing arrays?
Reply #5 - Jul 26th, 2005, 7:24pm
 
For a set of classes, working with Arrays become a problem. I have three class, and I would like to simplify the arrays manipulations. Here's an example:
Code:
class MyObjectA{
MyObjectA{}
//...
}
class MyObjectB{
MyObjectB{}
//...
}
class MyObjectC{
MyObject{}
//...
}



MyObjectA[] a = new MyObjectA[100];
MyObjectB[] b = new MyObjectB[100];
MyObjectC[] c = new MyObjectC[100];



//////////////////////
MyObjectA[] addMyObjectA(MyObjectA[] myArrayA, MyObjectA aNewObject){
//Create the new List
MyObjectA[] newArrayA= new MyObjectA[myArrayA.length+1];
//Copy all the old objects
System.arraycopy(myArrayA, 0, newArrayA, 0, myArrayA.length);
//Add the new Object
newArrayA[myArrayA.length] = aNewObject;
//Replace the List with the NewList
return newArrayA;
}

MyObjectA[] removeMyObjectA(MyObjectA[] myArrayA, int objectA_id){
MyObjectA[] newArrayA = new MyObjectA[myArrayA.length-1];
System.arraycopy(myArrayA, 0, newArrayA, 0, objectA_id);
System.arraycopy(myArrayA, objectA_id+1, newArrayA,
objectA_id,(myArrayA.length-(objectA_id+1)) );
return newArrayA;
}

MyObjectA[] putThisInTheMiddle(...){}
MyObjectA[] putThisAtTheBeginning(...){}
MyObjectA[] removeTheFirst(...){}
MyObjectA[] removeTheLast(...){}
MyObjectA[] reverseOrder(...){}
...
////////////////////////



//DoTheSame for each class
/////////////////////
MyObjectB[] addMyObjectB(MyObjectB[] myArrayB, MyObjectB bNewObject){ ... }
MyObjectC[] addMyObjectC(MyObjectC[] myArrayC, MyObjectC cNewObject){ ... }

MyObjectB[] removeMyObjectB(MyObjectB[] myArrayB, int objectB_id){ ... }
MyObjectC[] removeMyObjectC(MyObjectC[] myArrayC, int objectC_id){ ... }

I should say that I'm not familiar with Vectors and Abstract classes. I could not see if Vector strategy could simplify this structure?
Did someone have an Example of implementation?
Re: Dynamically resizing arrays?
Reply #6 - Jul 27th, 2005, 2:27pm
 
Vector certainly would. Instead of having to make a new array and copy all of the old contents, all you would have to put is myArray.addElement(foo); so it handles all the backend sort of stuff for you.

And not to worry, Vector isn't an abstract class. Smiley Just a normal one. You can declare a Vector by going:

Vector myVec = new Vector();

And then you can retrieve elements by:

MyObject foo = (MyObject) myVec.elementAt(i);
Re: Dynamically resizing arrays?
Reply #7 - Jul 27th, 2005, 9:42pm
 
Why is it called Vector? It's far different geometry vectors (2D&3D) vectors.
Re: Dynamically resizing arrays?
Reply #8 - Jul 28th, 2005, 12:51am
 
gll wrote on Jul 27th, 2005, 9:42pm:
Why is it called Vector It's far different geometry vectors (2D&3D) vectors.

"Vector" is the name of the class from Java, not Processing, where it simply refers to a list of things. since Java's primary role is not graphics, Vector isn't as objectionable.
Page Index Toggle Pages: 1