FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Array problem
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Array problem  (Read 660 times)
gaza


Array problem
« on: Jan 15th, 2003, 4:07pm »

Based on this Array Example here http://proce55ing.net/learning/examples/data05.html  ... I was trying to work this thing:
 
float xM = mouseX;
float yM = mouseY;
 
//Ball ball_1 = new Ball(150,150,10,10);
Ball[] balls;
int num;
 
void setup(){
  size(300,300);
  background(255);
  num=30;
  balls = new Ball[num];
   
   
  for (int i=0; i<width; i+=10){
    for(int j=0; j<height; i+=10){
 //int index=i+1;
 balls[i]=new Ball(i,j,10,10);
 }
 }
  }
   
void loop(){
for (int i=0; i<width; i+=10){
balls[i].moveTo();
balls[i].drawBall();
  }
}
 
class Ball{
  float xB;
  float yB;
  int w;//ancho
  int h;//alto
  public Ball(float xpos, float ypos,int W,int H){
  w = W;
  h = H;
  xB = xpos;
  yB = ypos;
  }
  void moveTo(){
  if(mousePressed){
    xB+=(mouseX-xB)*.2000;
    yB+=(mouseY-yB)*.2000;
    }
    else{
    this.xB=xB;
    this.yB=yB;
    }
   }
  void drawBall(){
    ellipseMode(CENTER_DIAMETER);
    noFill();
    stroke(255,0,0);
    ellipse(xB,yB,w,h);
    }
}
 
 
But I'm getting this ERROR - ArrayIndexOutOfBoundsExeption: 30.
 
I'm really lost here : )
 
Thanks for any help.
 
 
fry


WWW
Re: Array problem
« Reply #1 on: Jan 15th, 2003, 7:45pm »

the ArrayIndexOutOfBounds means that you're trying to use an array as if it's larger than it really is.
 
in your code, you made an array called balls[] but since num = 30, balls[] only works from balls[0] up to balls[29].  
 
later, you have balls[i] = new Ball (etc)
 
and i is going from 0 to 300, in steps of ten. so when i is 30, you're getting the error, where balls[30] doesn't exist (nor does balls[40], balls[50] etc which would come next).
 
to fix it, i think your for loops should go from 0 to 30, instead of 0 to width or 0 to height.  
 
also, you need a two dimensional array for what you're doing. so "balls = new Ball[num]" should be "balls = new Ball[num][num]". then inside the loop, it would be balls[i][j] = new Ball(i, j, 10, 10);
 
good luck!
 
gaza


Re: Array problem
« Reply #2 on: Jan 15th, 2003, 10:28pm »

Thanks Fry...
 
  why is it needed to have a two dimensional array?
 
 Thanks for the explanation on top.
 
benelek

35160983516098 WWW Email
Re: Array problem
« Reply #3 on: Jan 16th, 2003, 5:03am »

a two-dimentional array simplifies the task of organization. for instance, if u wanted an easy way to refer to a certain pixel in the global pixel array by its (x,y) coords, you could create a two-dimentional array as follows:
 
Code:

 
int[][] theList;
 
 
void setup() {
  size(300,300);
  noBackground();
  int counter=0;
  theList = new int[height][width];
  for(int i=0; i<height; i++) {
    for(int j=0; j<width; j++) {
 theList[i][j]=counter;
 counter++;
    }
  }
}

 
then you could call, say, the pixel at (42,29) by:
 
Code:

 
pixels[theList[29][42]];
 

 
remember, though, that essentially what you're doing is the same task - a 2D array is just an array of arrays, just as a normal array is the same as a list of variables. an array with 4 objects is just the same as 4 variables holding those objects, except that it may be easier to think of them as in an array.
 
now this particular example isnt hugely relevant to ur program, but you can see how much easier it is to relate objects in 2D, if they're in a 2D array.
 
-jacob
« Last Edit: Jan 16th, 2003, 5:16am by benelek »  
gaza


Re: Array problem
« Reply #4 on: Jan 16th, 2003, 4:12pm »

Thanks for explaining jacob....
 
  : )
 
Pages: 1 

« Previous topic | Next topic »