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 › expand()ing multi-dimensional arrays
Page Index Toggle Pages: 1
expand()ing multi-dimensional arrays (Read 1298 times)
expand()ing multi-dimensional arrays
Apr 5th, 2007, 4:42am
 
I've been trying to figure out how to expand two- and three- dimensional arrays, but I've been unable to figure out how to do certain things.

here is my test code:

int[][] two=new int[1][3];
int x, y;
void setup(){
 size(400,400);
 colorMode(RGB,100);
 noStroke();
 for(x=0;x<two.length;x++){
 for(y=0;y<two[x].length;y++){
   fill(two[x][y]);
   rect(x*5,y*5,5,5);
 }
 }
}
void draw(){}

void mouseClicked(){
 two[0]=(int[])expand(two[0]);
 
 for(x=0;x<two.length;x++){
 for(y=0;y<two[x].length;y++){
   two[x][y]=(int)random(40);
 }
 }
 for(x=0;x<two.length;x++){
 for(y=0;y<two[x].length;y++){
   fill(two[x][y]);
   rect(x*5,y*5,5,5);
 }
 }
}


this statement expands the second array,
two[0]=(int[])expand(two[0]);
But only for two[0], how would I go about expanding two[0...n] all at once?

Also, when I try to expand the first array ( two=(int[][])expand(two); )I get a null pointer exception at
"for(y=0;y<two[x].length;y++){" and if I switch the "two[x]" to "two[0]" the error just shifts down a line.

what am I doing wrong?

Lastly, and sort of unrelated, if I have an array of some type of object, and want to shrink it, how do I?  the processing reference says that contract has been removed, and that I should use subset instead, but subset says it only works on base arrays, as does shorten, what do I do?

preemptive thanks.
Re: expand()ing multi-dimensional arrays
Reply #1 - Apr 5th, 2007, 12:36pm
 
Code:


int[][] two=new int[1][3];
int x, y;
void setup(){
size(400,400);
colorMode(RGB,100);
noStroke();
for(x=0;x<two.length;x++){
for(y=0;y<two[x].length;y++){
fill(two[x][y]);
rect(x*5,y*5,5,5);
}
}
}
void draw(){
}

void mouseClicked(){

// set this to "true" for other example
if ( false ) {
// since you don't use the old values anyway
two = new int[two.length+1][3];

for(x=0;x<two.length;x++){
for(y=0;y<two[x].length;y++){
two[x][y]=(int)random(40);
}
}
}
else {

// if you would like to keep the old values do:
int[][] tmp = new int[1][3];

for(x=0;x<tmp.length;x++){
for(y=0;y<tmp[x].length;y++){
tmp[x][y]=(int)random(40);
}
}
two = (int[][])concat( two, tmp );
}

for(x=0;x<two.length;x++){
for(y=0;y<two[x].length;y++){
fill(two[x][y]);
rect(x*5,y*5,5,5);
}
}
}


and

Code:


void setup ()
{
Foo sa1[] = {
new Foo(), new Foo(), new Foo(), new Foo(), new Foo(), new Foo(), new Foo() };
Foo sa2[] = (Foo[])subset(sa1, 1);
print(sa2); // Prints "NY CA VA CO IL "
println();
Foo sa3[] = (Foo[])subset(sa1, 2, 3);
print(sa3); // Prints "CA VA CO "
}

int fooCount = 0;
class Foo
{
int fooId;
Foo ()
{
fooId = ++fooCount;
}
public String toString ()
{
return "Foo: "+fooId+"\n";
}
}


F
Re: expand()ing multi-dimensional arrays
Reply #2 - Apr 5th, 2007, 9:54pm
 
Thanks, but isn't there a better way of expanding the array and keeping the data?  That'll slow down the program once I'm using bigger arrays (as in [100][983040]) and have to load in 99 temp[]s.

Edit:
wait, nevermind. that works fine.
Page Index Toggle Pages: 1