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
   Syntax
(Moderators: fry, REAS)
   append array multi dimension
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: append array multi dimension  (Read 394 times)
st33d

WWW Email
append array multi dimension
« on: Jul 29th, 2004, 2:34am »

Code:

int []x=new int [1];
void setup(){
x[0]=1;
}
void draw(){
newLength(x[x.length-1]);
println(x[0]);
println(x[1]);
println(x.length);
}
void newLength(int newx) {  
  int[] tempx = new int[x.length + 1];  
  System.arraycopy(x, 0, tempx, 0, x.length);  
  tempx[x.length] = newx;    
  x = tempx;
}

This code is based on a previous post I looked up to find out how to append an array. It has served me well but I must prevail upon the wisdom of the forum in another matter. How do I append a multi-dimensional array? Lets say our array is x[][], what is the correct syntax for extending the length of either of the dimensions?
 

I could murder a pint.
narain


Re: append array multi dimension
« Reply #1 on: Jul 29th, 2004, 6:06am »

That's tough, actually, because of the way multi-dimensional array work...
 
When you say int x[m][n], what Java does is actually to create m independent arrays of n ints each, and then make an array of those.
 
So to extend the array in the first dimension, you can just treat x as a simple array containing int[]s, like this (untested):
Code:
void extend1 (int[] newx) {
    // newx is what to put in the new locations
  int[] tempx = new int[x.length+1][];
  System.arraycopy(x, 0, tempx, 0, x.length);
  tempx[x.length] = newx;
  x = tempx;
}

 
Extending in the second dimension is harder, since x has m arrays of n elements, so to increase n you'll have to extend each one of the m arrays.
(also untested):
Code:
void extend2 () {
  for (int i = 0; i < x.length; x++) {
    int[] xi = x[i];
    int[] tempxi = new int[xi.length + 1];
    System.arraycopy(xi, 0, tempxi, 0, xi.length);
    x[i] = tempxi;
  }
}

 
This one leaves the new locations unset for clarity. (If you want you can pass another int[] containing what to set in each location).
 
st33d

WWW Email
Re: append array multi dimension
« Reply #2 on: Jul 29th, 2004, 10:21pm »

Thanks a bundle. This knowledge comes in especially handy at the conceptual stage.
 
Code is tested. Try for yourself.
 
Code:

int [][] x=new int [1][1];
void setup(){
x[0][0]=1;
}
void draw(){
println(x[0][0]);
println(x[0][0]);
extend1(x[x.length-1]);
println(x[1][0]);
extend2();
println(x[1][1]);
println(x.length);
}
void extend1 (int[] newx) {  
    // newx is what to put in the new locations  
  int[][] tempx = new int[x.length+1][];  
  System.arraycopy(x, 0, tempx, 0, x.length);  
  tempx[x.length] = newx;  
  x = tempx;  
}
void extend2 () {  
  for (int i = 0; i < x.length; i++) {  
    int[] xi = x[i];  
    int[] tempxi = new int[xi.length + 1];  
    System.arraycopy(xi, 0, tempxi, 0, xi.length);  
    x[i] = tempxi;  
  }  
}
 

I could murder a pint.
narain


Re: append array multi dimension
« Reply #3 on: Jul 30th, 2004, 3:42pm »

Good for you Glad to help.
 
Pages: 1 

« Previous topic | Next topic »