Appending to arrays of arrays

I’m just going to play with some arrays here, and I hope that you get something out of it. I believe example float[][] c will be the most relevant to “appending”.


float[][] a;
float[][] b;
float[][] c;
void setup(){
  // you need the word "new" tell the system to allocate memory for the array
  // a's value is now a memory address for the array. it's type is float[][]
  a = new float[70][60];
  for(int i = 0; i < a.length; i++)
  {                   //.length's value is 70
    for(int ii = 0; ii < a[i].length; ii++)
    {                    //[i].length's value is 60
       a[i][ii] = random(-1000,1000);
    }
  }
  b = new float[70][]; // you don't have to provide the second dimension
  for(int i = 0; i < b.length; i++)
  {      
    //b[i]'s value is also a memory address. It's type is float[]
    b[i] = new float[(int)random(0,100)];
    for(int ii = 0; ii < b[i].length; ii++)
    {                   
       b[i][ii] = random(-1000,1000);
    }
  }
  c = new float[70][]; // this example is more like "appending"
  for(int i = 0; i < c.length; i++)
  {      
    float[] temp = new float[(int)random(0,100)];
    for(int ii = 0; ii < temp.length; ii++)
    {                   
       temp[ii] = random(-1000,1000);
    }
    c[i] = temp;
  }
}
2 Likes