Appending to arrays of arrays

i’m working on an art program and i’m trying to save the coordinates of the vectors in arrays one level in (for using layers) and i cant seem to get the append to work correctly. can someone help me?

i made some example code

float[][] x;

void setup() {
  for (int i = 0; i != 10; i = i + 1) {
    x[i] = append(x[i],-200);
  }
}

void draw() {
  
}

How about this instead?: :grinning:

static final int DIM = 10, INIT = -200;
final float[][] x = new float[DIM][DIM];

void setup() {
  for (final float[] row : x)  java.util.Arrays.fill(row, INIT);
  for (final float[] row : x)  println(join(str(row), ", "));
  exit();
}
1 Like

woah how does that work? @gotoloop

Have it even worked for you?! What exactly you haven’t understood? :question:
Some online references I’ve used in my sample sketch: :nerd_face:

  1. Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#fill(float[],float)
  2. Processing.org/reference/join_.html
  3. Processing.org/reference/for.html
1 Like

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

i dont know wether it s stupid but, trying to answer (how to use append with 2D arrays) i have found this workaround:

void setup() {
  
  int cols = 10;
int rows = 10;
int[][] myArr = new int[cols][rows];
int[] autre= new int[0];


for (int i = 0; i < cols; i++) {
  for (int j = 0; j < rows; j++) {
    autre = append(autre,-200);
    myArr[i][j] = autre[autre.length-1];
  }
}
int j=0;
  for(int k=0;k<myArr.length;k++){
    for (int l = 0; l < rows; l++) {
    println(myArr[k][l]);
    println(j);
    j++;
  }
}
exit();
}

it seems to work…but perhaps i am wrong…

@ducco

To focus in on two aspects of the previous answers –

  1. the original example has a float array with no length, then you try to append things into items 0-9. So the first problem is that float[][] x needs to have a top level dimension: x = new float[10][]. (Or it needs to be appended to first). You don’t need to declare the second dimension.

  2. Now there are 10 empty spots (or 1, if they are being appended one at a time, see below), but they aren’t each filled with a float array yet. So, assign a new float array to each one: x[i] = new float[]{-200, 0}.

So, with these applied to your original code:

float[][] x;
int DIM = 10;
void setup() {
  x = new float[DIM][];
  for (int i = 0; i < DIM; i++) {
    x[i] = new float[]{-200, 0};
  }
  for (int i = 0; i < DIM; i++) {
    println(x[i]);
  }
}

NOTE: This doesn’t use append. Repeatedly appending to a fixed array in a loop is usually a bad idea. Instead, consider declaring sizes at the beginning if you know them or use things like dynamically allocated array lists, e.g. use ArrayList and/or FloatList – including using ArrayList at the top level as well if your data layers are sufficiently dynamic.

1 Like

thank you, how would you do the same using dynamically allocated arrays, i’m not very experienced with data management in processing. btw i had the main script set up so it added a coordinate whenever you clicked, my test script was basically just for initialization so the actual drawing bit could have something to draw on startup @jeremydouglass

Well, here is one example. You have an unknown number of layers, and each layer is full of an unknown number of points, and each point contains 2 or 3 floats (2D or 3D). It should be easy to add layers and points, and get them.

ArrayList<ArrayList<PVector>> layers;

Okay, initialize your layers – a list of (lists of (points)).

layers = new ArrayList<ArrayList<PVector>>();

and add a layer – which is a list of points:

layers.add(new ArrayList<PVector>());

now get layer 0

ArrayList<PVector> layer0 = layers.get(0);

and add some points to layer 0

layer0.add(new PVector(0, 0));
layer0.add(new PVector(50, 50));
layer0.add(new PVector(random(0, width), random(0, height)));

now retrieve layer 0, point 1 from your layers object:

PVector pt = layers.get(0).get(1);

and draw its x, y:

ellipse(pt.x, pt.y, 20, 20);

The example:

ArrayList<ArrayList<PVector>> layers;
layers = new ArrayList<ArrayList<PVector>>();
layers.add(new ArrayList<PVector>());
ArrayList<PVector> layer0 = layers.get(0);
layer0.add(new PVector(0, 0));
layer0.add(new PVector(50, 50));
layer0.add(new PVector(random(0, width), random(0, height)));
PVector pt = layers.get(0).get(1);
ellipse(pt.x, pt.y, 20, 20);
1 Like