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 › make arrays with a for loop in a class
Page Index Toggle Pages: 1
make arrays with a for loop in a class (Read 530 times)
make arrays with a for loop in a class
Dec 31st, 2009, 6:15am
 
How can i create arrays in a for loop?

Code:
    for(int i=0; i<20; i++){
int[][] ["test"+i] = new int[20][20];
}


and if that's possible how can i create them in a class.
Now it says testInt cannot be fined, that's cause it has to be created by this line:
//for loop can't be here

only there can't be a for loop, so does this mean a for loop aint possible or what?


Code:
Grid grid1;

void setup(){
size(200,200);

grid1 = new Grid();

}

void draw(){
grid1.test();
}

class Grid {

//for loop can't be here

Grid(){//side1, side2
int[] testInt = new int[10];

}

void test(){
println(testInt);
}
}


Re: make arrays with a for loop in a class
Reply #1 - Dec 31st, 2009, 7:03am
 
Yes it is possible because in Java a "D array is a 1D aray of arrays. Might try this:
Code:


Grid grid;

void setup(){
size(200,200);
grid = new Grid(10,25);
println("Side 1 = "+grid.getSide1());
println("Side 2 = "+grid.getSide2());

}

class Grid {

int[][]g;

Grid(int side1, int side2){
g = new int[side1][];
for(int i = 0; i < side1; i++){
g[i] = new int[side2];
}
}

public int getSide1(){
return g.length;
}

public int getSide2(){
return g[0].length;
}
}

Smiley
Page Index Toggle Pages: 1