# Appending to arrays of arrays

**URL:** <https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945>\
**Category:** Coding Questions\
**Created:** [September 29, 2018, 12:15am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945 "2018-09-29T00:15:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ducco](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ducco/32/1749_2.png) [@ducco](https://discourse.processing.org/u/ducco)\
**Post date:** [September 29, 2018, 12:15am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/1 "2018-09-29T00:15:55Z")

</div>

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

```auto
float[][] x;

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

void draw() {
  
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 29, 2018, 12:50am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/2 "2018-09-29T00:50:32Z")

</div>

How about this instead?: 😀

```java
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();
}

```

---

<div class="post-metadata">

**Author:** ![ducco](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ducco/32/1749_2.png) [@ducco](https://discourse.processing.org/u/ducco)\
**Post date:** [September 29, 2018, 1:28am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/3 "2018-09-29T01:28:30Z")

</div>

woah how does that work? @gotoloop

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 29, 2018, 5:20am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/4 "2018-09-29T05:20:41Z")

</div>

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

1. [Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#fill(float[],float)](http://Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#fill(float%5B%5D,float))
2. [Processing.org/reference/join\_.html](http://Processing.org/reference/join_.html)
3. [Processing.org/reference/for.html](http://Processing.org/reference/for.html)

---

<div class="post-metadata">

**Author:** ![desk](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@desk](https://discourse.processing.org/u/desk)\
**Post date:** [September 29, 2018, 6:42am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/5 "2018-09-29T06:42:11Z")

</div>

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”.

```auto

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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 1, 2018, 11:55am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/6 "2018-10-01T11:55:08Z")

</div>

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

```auto
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…

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 1, 2018, 6:44pm UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/7 "2018-10-01T18:44:38Z")

</div>

@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:

```auto
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.

- [https://processing.org/reference/ArrayList.html](https://processing.org/reference/ArrayList.html)
- [https://processing.org/reference/FloatList.html](https://processing.org/reference/FloatList.html)

---

<div class="post-metadata">

**Author:** ![ducco](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ducco/32/1749_2.png) [@ducco](https://discourse.processing.org/u/ducco)\
**Post date:** [October 2, 2018, 1:48am UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/8 "2018-10-02T01:48:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 2, 2018, 11:40pm UTC](https://discourse.processing.org/t/appending-to-arrays-of-arrays/3945/9 "2018-10-02T23:40:51Z")

</div>

> [@ducco](#):
>
> how would you do the same using dynamically allocated arrays

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.

```auto
ArrayList<ArrayList<PVector>> layers;

```

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

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

```

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

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

```

now get layer 0

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

```

and add some points to layer 0

```auto
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:

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

```

The example:

```auto
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);

```
