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 › Populate a 2D array
Page Index Toggle Pages: 1
Populate a 2D array (Read 1044 times)
Populate a 2D array
Jun 22nd, 2006, 10:59am
 
Hi there,

I am trying to figure out how to specify values in a 2D array. So far I have this 1D array that stores a set of Vector coordinates:

Vector3D[]target1 = {new Vector3D(20,20), new Vector3D(50,20), new Vector3D(100,20)};

However, I'd really like to have e.g. 3 sets of coordinates in the array. In other words, an array with 3 indexes, each of which stores its own array.

My guess is that the syntax would start something like this:

Vector3D[][]targets = ???

Any help would be very much appreciated. I can't quite get my head around this multi-dimensional array stuff Sad

cheers,
Matt
Re: Populate a 2D array
Reply #1 - Jun 22nd, 2006, 11:04am
 
If you really want to populate your array in that way, then the syntax is (I think)
Code:
type[][] foo={{type(0,0),type(0,1),type(0,2)}, {type(1,0),type(1,1),type(1,2)}, {type(2,0),type(2,1),type(2,2)}}; 



However that's really ick.
It's probabyl betyter to do something like:

Code:
type[][] foo=new type[3][3];
type[0][0]=new type(0,0);
type[0][1]=new type(0,1);
type[0][2]=new type(0,2);
type[1][0]=new type(1,0);
type[1][1]=new type(1,1);
type[1][2]=new type(1,2);
type[2][0]=new type(2,0);
type[2][1]=new type(2,1);
type[2][1]=new type(2,2);

Re: Populate a 2D array
Reply #2 - Jun 22nd, 2006, 11:27am
 
That helps a lot. Many thanks.
Re: Populate a 2D array
Reply #3 - Jun 22nd, 2006, 11:55am
 
I was just wondering, does anyone happen to have any idea of how I could then randonly select from the 2D array? I need to take a random set of coordinates.

The original code for my 1D array was like this:

// store values in array
Vector3D[]target = {new Vector3D(20,20), new Vector3D(50,20), new Vector3D(100,20)}

// a function that returns a random index from that array:
Vector3D someTarget() {
 return target[int(random(target.length))];
}

//use that function to store a target position (that my Boid object moves to)
Vector3D goodTarget = someTarget();

Now that I have a 2D array, how do I change the function above to select an array of Vector objects rather than a single object as before?

My head hurts!
Re: Populate a 2D array
Reply #4 - Jun 22nd, 2006, 2:41pm
 
Code:
// a function that returns an array from a random index 
// from that array:
Vector3D[] someTarget() {
return target[int(random(target.length))];
}

Vector3D[] goodTarget = someTarget();


Or to chose a single thing from a 2D array
Code:
Vector3D someTarget() {
int i=int(random(target.length));
int j=int(random(target[i].length));
return target[i][j];
}

Re: Populate a 2D array
Reply #5 - Jun 22nd, 2006, 4:37pm
 
That seems to work perfectly. Thanks again - you're a star!

Matt Wink
Re: Populate a 2D array
Reply #6 - Jun 22nd, 2006, 5:39pm
 
OK so that all seems to work well. I've figured out that if I use the following code inside the function, I can refer to a specific set of my co-ordinates:

Vector3D[] someTarget() {
 return target[4];
}

So I have a one final question (I promise!):

What would be ideal would be for the function to return a specific array depending on which key is pressed. The problem is, as soon as I put any keypress or if/else statements inside the function, it returns an error. Here is my poor attempt at the code:

Vector3D[] someTarget() {

 if(keyPressed) {
   if (key == 'a' || key == 'A') {
     return target[0];
   }

   if (key == 'b' || key == 'B') {
     return target[1];
   }



 }
}

It tells me that the function "must return an expression compatible with type Vector3D[]"

As you must have guessed I'm a complete novice when it comes to programming. Is my syntax completely wrong?
Re: Populate a 2D array
Reply #7 - Jun 22nd, 2006, 6:43pm
 
When a function has a return type other than void, the function needs to return that type. Nesting return statements within other blocks (your conditionals) doesn't ensure that (from the compiler's perspective.)

Here's a more structured verson of what I think you're trying to do.
Code:

Vector3D[]target = { new Vector3D(1,2,3), new Vector3D(4,5,6) };
Vector3D w = new Vector3D();

void draw(){
}

Vector3D getTarget() {
println("target = ("+w.x+", "+w.y+", "+w.z+")");
return w;
}

void setTarget(Vector3D w) {
this.w = w;
}

class Vector3D{
float x, y, z;
Vector3D(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}

Vector3D(){
}
}

void keyPressed() {
if (key == 'a' || key == 'A') {
setTarget(target[0]);
}
else if (key == 'b' || key == 'B') {
setTarget(target[1]);
}
getTarget();
}

Re: Populate a 2D array
Reply #8 - Jun 22nd, 2006, 6:58pm
 
Great thanks. I'll take a look at your code and see if I can get my head around it.

cheers,
Matt
Re: Populate a 2D array
Reply #9 - Jun 22nd, 2006, 7:32pm
 
Matt,
One other clarification, which may not be obvious in my code, you wouldn't really want to call getTarget() from within the keyPressed() function. I just did that so you could see the output. You'd likely want to create another function (which called getTarget()) that actually used or assigned the returned Vector3D obj.

ira
Re: Populate a 2D array
Reply #10 - Jun 22nd, 2006, 7:57pm
 
Thanks Ira. I must say that I'm finding it quite hard to adapt what you've done to my code but I will persevere! Wink
Re: Populate a 2D array
Reply #11 - Jun 22nd, 2006, 9:01pm
 
Sorry for the trouble;-)
In the long run it's beneficial to organize code as smallish, concise and hopefully reusable structures; it's also good practice as you delve deeper into OOP.

Learning this stuff is all about perseverance – a point I struggle to instill in my students.
Re: Populate a 2D array
Reply #12 - Jun 23rd, 2006, 3:53pm
 
Finally got it to work! What a relief. Thanks for the help everyone. Very much appreciated Smiley
Page Index Toggle Pages: 1