# Appending to two dimensional array

**URL:** <https://discourse.processing.org/t/appending-to-two-dimensional-array/26736>\
**Category:** Beginners\
**Created:** [January 3, 2021, 7:19pm UTC](https://discourse.processing.org/t/appending-to-two-dimensional-array/26736 "2021-01-03T19:19:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave307](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dave307](https://discourse.processing.org/u/dave307)\
**Post date:** [January 3, 2021, 7:19pm UTC](https://discourse.processing.org/t/appending-to-two-dimensional-array/26736/1 "2021-01-03T19:19:20Z")

</div>

How would you append to a two array and append to a certain rows.  
like  
a = [1,2,3]  
b = [5,6]  
then you append a to the first column and b to the second column of another array so it would be  
c =  
[1,2,3]  
[5,6]  
hope this makes sense and thanks in advance

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 3, 2021, 7:21pm UTC](https://discourse.processing.org/t/appending-to-two-dimensional-array/26736/2 "2021-01-03T19:21:47Z")

</div>

> **Processing**
>
> ```auto
> <datatype> <name>[] = new <datatype>[length (integer)];
> int w = 10, h = 10;
> int grid[][] = new int[w][h];
> grid[0][3] = 3;
> 
> ```

you need to determin the size of the array

```auto
let a = []; //i don't really know the syntax
let w = 10, h = 10; //set length of a[] to w*h
function access(int input1, int input2) {
   return( a[input1 + input2*w]);
}

```

---

<div class="post-metadata">

**Author:** ![Sierra\_Alpha\_Romeo.P](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sierra_alpha_romeo.p/32/11571_2.png) [@Sierra\_Alpha\_Romeo.P](https://discourse.processing.org/u/Sierra_Alpha_Romeo.P)\
**Post date:** [January 3, 2021, 7:32pm UTC](https://discourse.processing.org/t/appending-to-two-dimensional-array/26736/3 "2021-01-03T19:32:34Z")

</div>

You’ve added the p5.js tag to this question.  
In JavaScript, you can directly use the _push_ method to append any data to an existing array.  
For example,

```auto
let a = [1, 2, 3];
let b = [4,5];
let c = [];
c.push(a); //c = [[1,2,3]] now
c.push(b); //c = [[1,2,3], [4,5]] now

```

---

<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:** [January 3, 2021, 7:37pm UTC](https://discourse.processing.org/t/appending-to-two-dimensional-array/26736/4 "2021-01-03T19:37:09Z")

</div>

Using the `[]` syntax for creating arrays is common in JS.

And there are many other ways to do that for 2D arrays.

Here’s 1 you can copy & paste on any browser console or even Node.js:

```auto
var a = Uint8Array.of(1, 2, 3), b = Uint8Array.of(5, 6);
console.info(a, b);

var c = [a, b];
console.table(c);

```

And the corresponding Processing’s Java Mode version for 2D arrays:

```auto
byte[] a = { 1, 2, 3 }, b = { 5, 6 };

println(a);
println();
println(b);
println();

byte[][] c = { a, b };

for (byte[] row : c) println(str(row));

exit();

```

And Python Mode version as well:

```auto
a = 1, 2, 3
b = 5, 6
print a, b

c = a, b
print c

exit()

```

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [January 3, 2021, 7:45pm UTC](https://discourse.processing.org/t/appending-to-two-dimensional-array/26736/5 "2021-01-03T19:45:43Z")

</div>

You can use the concat() method to merge the 2 arrays.

```auto
let a = [1, 2, 3];
let b = [5, 6];
a.concat(a, b);

```
