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 & HelpPrograms › array problems
Page Index Toggle Pages: 1
array problems? (Read 987 times)
array problems?
Aug 23rd, 2005, 3:08am
 
I've got a program that's supposed to draw a grid of squares on the screen, using a 2 dimensional array to identify the squares and store their color values.  When I try to execute the program, I get this error from the compiler (or debugger or what ever you call it): java.lang.ArrayIndexOutOfBoundsException: 0
I don't think that my code could have generated a number larger than the size of the array, so I'm confused as to what is going wrong.  Here's my program:

int ssize = 15;
color z, whi, bla;
int ncount, w, h;
color[][] current = new color[w][h];
color[][] next = new color [w][h];

void setup(){
 size(900, 600);
 background(0);
 framerate (60);
 stroke(122);
 w = width/ssize;
 h = height/ssize;
 bla = color(0,0,0);
 whi = color(255,255,255);
 
 for(int i=0; i<w; i++){
   for(int j=0; j<h; j++){
     current[i][j] = bla;
   }
 }
}

void draw(){  
 for(int i=0; i<w; i++){
   for(int j=0; j<h; j++){
     fill(current[i][j]);
     rect(14*i,14*j,15,15);
   }
 }
 
 for(int i=0; i<width; i++){
   for(int j=0; j<height; j++){
     neighbors (i, j);      
     if (ncount == 3){
       next[i][j] = whi;}
     else if(ncount != 2){
       next[i][j] = bla;}
   }
 }
 
 for(int i=0; i<w; i++){
   for(int j=0; j<h; j++){
     current[i][j] = next[i][j];
   }
 }
}

void neighbors (int wi, int wj){
 ncount = 0;
 if (current[wi-1][wj-1]==whi){
   ncount ++;}    
 if (current[wi][wj-1]==whi){
   ncount ++;}    
 if (current[wi+1][wj-1]==whi){
   ncount ++;}    
 if (current[wi-1][wj]==whi){
   ncount ++;}    
 if (current[wi+1][wj]==whi){
   ncount++;}    
 if (current[wi-1][wj+1]==whi){
   ncount++;}
 if (current[wi][wj+1]==whi){
   ncount++;}    
 if (current[wi+1][wj+1]==whi){
   ncount++;}
}

Also, this code seems pretty cumbersome to me.  Especially the function neighbors at the bottom.  It's supposed to check the eight squares around the current one to see what their colors are, but there is a whole lot of duplicated work the way I have it here.  Does anyone have any suggestions about how to make this a bit more streamlined?
Re: array problems?
Reply #1 - Aug 23rd, 2005, 3:20am
 
int ncount, w = 900, h = 600;
color[][] current = new color[w][h];
color[][] next = new color [w][h];

that fixed the error Smiley

-seltar
Re: array problems?
Reply #2 - Aug 23rd, 2005, 4:03am
 
I see now.  Thanks!
Re: array problems?
Reply #3 - Aug 23rd, 2005, 10:44am
 
ok, new problem...  the method I use to test adjacent squares (the neighbor function) looks at all eight squares around the one being tested.  But when that square is on an edge or in a corner it tries to test squares that are off the screen, and references terms in the arrays that don't exist.  I can see one way to get around this, but it would involve a whole lot of if statements to handle each situation where it might test offscreen.  Anyone know how I might fix this problem in a more readable/efficient way?
Re: array problems?
Reply #4 - Aug 23rd, 2005, 12:47pm
 
Before you try and check an index, you would need to add a single set of if statements to see if the values were out of bounds. You wouldn't need to duplicate this - just find the right place in the code to peform the operation. Perhaps make your code a bit more modular, or put the code below in a method that returns true, or false.

E.g. maybe:
Code:

int x = 0, y = 0;
int w = 100, h = 50;
int[][] ary = int[w][h];

// select next index to check
x = 54;
y = 120;

if(x < 0 || y < 0 || x >= w || y >= h) {
println("Index "+x+", "+y+" out of bounds!");
}
Page Index Toggle Pages: 1