# How to append to a 2 dimensional array?

**URL:** <https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936>\
**Category:** Coding Questions\
**Created:** [November 29, 2019, 2:56am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936 "2019-11-29T02:56:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoeyPeluka](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JoeyPeluka](https://discourse.processing.org/u/JoeyPeluka)\
**Post date:** [November 29, 2019, 2:56am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936/1 "2019-11-29T02:56:04Z")

</div>

Hello, I’m having trouble finding an answer to this problem.

1. How would you append an array to an array.

One of the ways I have tried doing this:

```auto
int[][] array;

  for (int i=1; i<=20; i++) { 
      append(array,{0,0,0});
    }
 }

```

1. How would you append to an array that was within that array?

How I’ve tried this:

```auto
int[][] array;

for (int i=1; i<=20; i++) { 
    for (int j=1; j<=20; i++) {
      append(array[i][j],0);
    }
 }

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 29, 2019, 3:35am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936/2 "2019-11-29T03:35:23Z")

</div>

i play long ago, and sure there are better ways

> **array copy expand**
>
> ```auto
> // array expand test
> // https://processing.org/discourse/beta/num_1175740933.html
> int[][] test ={{11, 12, 13}, {21, 22, 23}, {31, 32, 33}, {41, 42, 43}};
> int[][] tmp ;
> int cols1,rows1,cols2,rows2;
> 
> cols1=test[0].length;
> println(" test[0].length == columns : "+cols1);
> rows1=test.length;
> println(" test.length == rows : "+rows1);
> 
> for (int i = 0; i<4; i++) {
> for (int k = 0; k<3; k++) {
> println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
> }
> }
> 
> // backup
> tmp = new int[rows1][cols1];
> println(" copy to tmp ");
> tmp = test;
> for (int i = 0; i<4; i++) {
> for (int k = 0; k<3; k++) {
> //println("row ["+i+"] "+" col ["+k+"] : "+tmp[i][k]);
> }
> }
> 
> // expand:
> println(" make one longer ");
> test = new int[rows1+1][cols1];
> 
> cols2=test[0].length;
> println(" test[0].length == columns : "+cols2);
> rows2=test.length;
> println(" test.length == rows : "+rows2);
> for (int i = 0; i<rows2; i++) {
> for (int k = 0; k<cols2; k++) {
> //println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
> }
> }
> println(" copy back ");
> for (int i = 0; i<rows1; i++) {
> for (int k = 0; k<cols1; k++) {
> test[i][k] = tmp[i][k];
> //println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
> }
> }
> println(" show all ");
> for (int i = 0; i<rows2; i++) {
> for (int k = 0; k<cols2; k++) {
> println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
> }
> }
> 
> ```

that might be a good reason to make a class and a arrayList of it?

```auto
class Row {
  int x,y;
  Row(int _x, int _y ) {
    x=_x;
    y=_y;
  }
}

ArrayList<Row> my_array = new ArrayList<Row>();

void setup(){
  my_array.add( new Row(1,2) );
  my_array.add( new Row(3,4) );
  for ( int i = 0;i < my_array.size();i++) println(my_array.get(i).x , my_array.get(i).y );
}

```

but a much better reason for it is that you can make mixed record types with it,  
just add string, float, boolean variables to the class…

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [November 29, 2019, 3:48am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936/3 "2019-11-29T03:48:09Z")

</div>

Its a lot more typing but [ArrayList](https://processing.org/reference/ArrayList.html) is better for performance if you plan on doing this a lot. The disadvantage is you can’t use primitive data types only the class wrappers which are slower. The advantage of using ArrayList instead of append is it doesn’t have to create a new array every time you add something to it.

```auto
// initialization
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

// append to array list that is already in the 2d list
list.get(/* index */).add(/* value */);

// append a new arraylist to the 2d list
ArrayList<Integer> otherList = new ArrayList<Integer>();
otherList.add(5);
list.add(otherList); // you can either create an otherList object
list.add(new ArrayList<Integer>()); // or add an empty one

// add the contents of one 2d list to the end of another
ArrayList<ArrayList<Integer>> other2d = new ArrayList<ArrayList<Interger>>();
list.addAll(other2d);

```

---

<div class="post-metadata">

**Author:** ![JoeyPeluka](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JoeyPeluka](https://discourse.processing.org/u/JoeyPeluka)\
**Post date:** [November 29, 2019, 4:57am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936/4 "2019-11-29T04:57:33Z")

</div>

I am doing this just in setup(), but need to refer back to variables from these arrays/ArrayLists in draw(). Would it still be faster to use ArrayList or just an array?

If an array is faster, how would you code that?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 29, 2019, 7:47am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936/5 "2019-11-29T07:47:35Z")

</div>

> [@JoeyPeluka](#):
>
> ARRAY … how would you code that

sorry that is little bit hidden under the above

> array copy expand

link you might have missed

array list use array in a smart way, so on some scale it might be slower…  
tell me when you notice it.

the thinking/using array .vs. arraylist  
has to do with the available functions  
you might get a idea here from the intList

> **[IntList / Reference](https://processing.org/reference/IntList.html)**
>
> Helper class for a list of int values. Lists are designed to have some features of ArrayList, but to maintain the simplicity and efficiency of working with arrays. Functions such as s…

* * *

using array and make some better code ( like put / use it as a own function )  
might have same result, still it not easy beats this line:  
` my_array.add( new Row(a,b) );`

* * *

as i define the arrayList “my\_array” global, you can use it in setup ( and draw )  
if you move that line down into setup it will be NOT usable further.  
now i not see further considerations for that but there can be.

---

<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:** [November 29, 2019, 9:28am UTC](https://discourse.processing.org/t/how-to-append-to-a-2-dimensional-array/15936/6 "2019-11-29T09:28:05Z")

</div>

> [@JoeyPeluka](#):
>
> I am doing this just in **setup()**,

If that’s so, it means you already know how many rows & columns your 2D array is gonna need. 😳

Apparently, according to your posted double loop example, that 2D array is 20 x 20. 🤓

So you’re just gonna need this, and you’re done: 💡

```java
static final int ROWS = 20, COLS = 20;
final int[][] grid = new int[ROWS][COLS];

void setup() {
  for (final int[] row : grid) println(str(row));
  exit();
}

```
