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.
Page Index Toggle Pages: 1
Array Help (Read 1544 times)
Array Help
May 28th, 2010, 7:46am
 
Hi,

Yet another question.

I have a two dimensional array declared as:

int coords[][] = {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18},{0,1,2,3,4,5,6,7,8,9,10,11,12,
13,14,15,16,17,18}};

Problem is that when I declare:

coords[1][1] = "spec1";               - this works fine

but...
coords[2][2] = "spec2";               - doesn't work

...the second one gives me the error:

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 2
     at assignment3.load_level(assignment3.java:98)
     at assignment3.select_pic(assignment3.java:235)
     at assignment3.mouseClicked(assignment3.java:64)
     at processing.core.PApplet.handleMouseEvent(PApplet.java:1614)
     at processing.core.PApplet.dequeueMouseEvents(PApplet.java:1544)
     at processing.core.PApplet.handleDraw(PApplet.java:1436)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)


I want to go up to declaring that....
coords[18][18] = "spec18";           - and everything in between.

Any help would be appreciated. I'm not good with arrays, let alone double dimension arrays. Did I declare it wrong?

IceKat.
Re: Array Help
Reply #1 - May 28th, 2010, 7:56am
 
IceKat wrote on May 28th, 2010, 7:46am:
I have a two dimensional array declared as:

int coords[][] = {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18},{0,1,2,3,4,5,6,7,8,9,10,11,12,
13,14,15,16,17,18}};

Problem is that when I declare:

coords[1][1] = "spec1";               - this works fine

but...
coords[2][2] = "spec2";               - doesn't work

...the second one gives me the error:

<snip>


Array indices start at 0.  Your 2d array currently contains only two sub-arrays with 19 elements each.

coords[2][2] references the third item in a non-existent third sub-array...
Re: Array Help
Reply #2 - May 28th, 2010, 8:02am
 
So how should I declare it so I can have....

coords[2][2] = "spec2";

??? And whilst I'm on here I may as well ask...how do I pull "spec2" out again?

I appreciate the help, like I said I'm new to arrays - particularly in processing.  Smiley

IceKat.
Re: Array Help
Reply #3 - May 28th, 2010, 8:28am
 
IceKat wrote on May 28th, 2010, 7:46am:
coords[1][1] = "spec1";               - this works fine

I doubt it... Wink
You assign a string to an array of ints!

There is a simpler way to create an array:
Code:
// Declare and create a double dimension array
int[][] coords = new int[20][20];
// Now, you can populate your array
for (int i = 0; i < coords.length; i++)
{
 coords[i][i] = i;
}
Re: Array Help
Reply #4 - May 28th, 2010, 8:55am
 
IceKat wrote on May 28th, 2010, 8:02am:
So how should I declare it so I can have....

coords[2][2] = "spec2";

? And whilst I'm on here I may as well ask...how do I pull "spec2" out again

I appreciate the help, like I said I'm new to arrays - particularly in processing.  Smiley

IceKat.


Hehe and what you meant by coords[1][1] is in fact coords[0][0].

But I guess these "spec" strings are meant to be variables to make the data easier to understand

do it like this:

Code:
int spec1 = coords[0][0]; 



I often do something similar:

Code:
for(int j = 0; j < points_3d.length; j++) {

 rotateY(j*TWO_PI/float(points_3d.length));

 for(int i = 0; i < points_3d[j].length; i++) {

   float mx = modelX(points_3d[j][i].x, points_3d[j][i].y, 0);
   float my = modelY(points_3d[j][i].x, points_3d[j][i].y, 0);
   float mz = modelZ(points_3d[j][i].x, points_3d[j][i].y, 0);

   println("("+mx+", "+my+", "+mz+")");

 }

}
Page Index Toggle Pages: 1