# Processing to make 2 array in one

**URL:** <https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [April 9, 2020, 3:01pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587 "2020-04-09T15:01:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 9, 2020, 3:01pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/1 "2020-04-09T15:01:07Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Now I have 2 array

int a = { 1, 2, 25 };  
int b = { 9, 18 };

I want to make with two of them in one array

with a for loop.

Can someone show me a result for this thing.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 9, 2020, 3:37pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/2 "2020-04-09T15:37:43Z")

</div>

Hello,

Welcome to the forum.

> [@gurki](#):
>
> I want to make with two of them in one array

There is a discussion of arrays and two-dimensional arrays here:

> **[Arrays](https://processing.org/tutorials/arrays/)**
>
> How to store and access data in array structures.

> [@gurki](#):
>
> with a for loop.

> **[for / Reference](https://processing.org/reference/for.html)**
>
> Controls a sequence of repetitions. A basic for structure has three parts: init, test, and update. Each part must be separated by a semicolon (;). The loop continues until …

It is not clear what you are trying to do; I don’t want to guess.  
Write some code and post it and we can discuss further.

Additional resources:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

> **[The Coding Train](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)**
>
> All aboard! The Coding Train is on its way with creative coding video tutorials on subjects ranging from the basics of programming languages like JavaScript to algorithmic art, machine learning, simulation, generative poetry, and more. Choo choo!

[https://www.youtube.com/watch?v=h4ApLHe8tbk](https://www.youtube.com/watch?v=h4ApLHe8tbk) (searched coding train for)  
[https://www.youtube.com/watch?v=NptnmWvkbTw](https://www.youtube.com/watch?v=NptnmWvkbTw) (searched coding train array)

`:)`

---

<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:** [April 9, 2020, 3:45pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/3 "2020-04-09T15:45:57Z")

</div>

[Processing.org/reference/concat\_.html](http://Processing.org/reference/concat_.html)

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 9, 2020, 3:49pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/4 "2020-04-09T15:49:12Z")

</div>

Hello unfortunately the coding train doesent help me much for this question.

I have two arrays.

these two for example. Just simple two array.

int[] a = { 1, 2, 25 };  
int[] b = { 9, 18 };

I want to make an new 1 dimensional array like this:

int [] arraynew = { 1,2,25,9,18};

or in Processing

[0] 1  
[1] 2  
[2] 25  
[3] 9  
[4] 18

The problem is that I need to make this in an for loop. I am not allowed to use concat() or splice().  
This code should alos function when I want to add more numers in the arrays.

Can someone help me please???  
If someone can speak german. Here is the task:

* * *

#### Arrays zusammenfügen

Gegeben sind zwei Arrays a und b. Erzeugen Sie einen neuen Array c, der so lang ist wie a und b zusammengenommen und auch die Werte von a und b (in dieser Reihenfolge) enthält.

int[] a = { 1, 2, 25 }; int[] b = { 9, 18 }; // Hier Code schreiben println©;

Sie sollten das hier auf der Konsole sehen:

[0] 1 [1] 2 [2] 25 [3] 9 [4] 18

Verwenden Sie **nicht** die Processing-Funktionen `concat()` oder `splice()` . Ihr Code sollte auch funktionieren, wenn Sie bei a und/oder b Elemente hinzufügen oder entfernen.

**Tipp**

Zerlegen Sie das Problem in zwei Schritte. In Schritt 1 befüllen Sie den ersten Teil von `c` mit den Werten von `a` . In Schritt 2 füllen Sie den zweiten Teil von `c` mit den Werten von `b` .

Jeden Schritt lösen Sie mit einer eigenen For-Schleife. Schritt 1 ist leicht, denn die Indexzahlen von `a` und `c` sind gleich (c[0] bekommt Wert von a[0] usw.). Schritt 2 erfordert eine kleine Änderung. Wie lautet die Indexzahl in `c` für den ersten Wert von `b` ?

* * *

Thank You…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 9, 2020, 4:05pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/5 "2020-04-09T16:05:25Z")

</div>

Ja, bitte nutze die reference

Schau dir for an

Und schaue dir append an

Die Regeln für das Forum hier sind, dass wir keine Hausaufgaben für dich machen dürfen.

Zeige also bitte erstmal deinen Code soweit du kommst

LG,

Chrisir

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 9, 2020, 4:13pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/6 "2020-04-09T16:13:47Z")

</div>

Please show what you have done so far.  
You have to start somewhere.

`:)`

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 9, 2020, 4:20pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/7 "2020-04-09T16:20:44Z")

</div>

Sorry I dont know the rules for this forum.

## I tried like this.

int[] a = { 1, 2, 25 };  
int[] b = { 9, 18 };

for (int i=0; i \> a.length ; i++)  
{  
int [] arraynew = new int [a[i] + b[i]];  
println(arraynew[i]);  
}

* * *

Can someone tell me the solution.  
It is really important.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 9, 2020, 4:25pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/8 "2020-04-09T16:25:34Z")

</div>

> [@gurki](#):
>
> for (int i=0; i \> a.length

Dies ist falsch, du brauchst ein \< Zeichen statt des \>

(solange wie)

Dann brauchst du bitte eine zweite for-Schleife nach der ersten.

Das neue Array soll übrigens c heißen.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 9, 2020, 4:27pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/9 "2020-04-09T16:27:32Z")

</div>

> [@gurki](#):
>
> int arraynew = new int [a[i] + b[i]];

Dies muss VOR der for Schleife geschehen

int arraynew = new int [a.length + b.length];

Damit erstellst du das neue array mit der richtigen Länge

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 9, 2020, 5:15pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/10 "2020-04-09T17:15:44Z")

</div>

Es gibt ein tutorial zu arrays

Du brauchst etwas wie

c[i] = a[i]; im ersten for-loop

In der zweiten Schleife ist der Index also das i zu modifizieren für c

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 9, 2020, 5:26pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/11 "2020-04-09T17:26:09Z")

</div>

```auto
int[] a = { 1, 2, 25 };
int[] b = { 9, 18 };
int[] c;
c = concat(a, b);
printArray(c);

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 9, 2020, 8:59pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/12 "2020-04-09T20:59:21Z")

</div>

> [@gurki](#):
>
> int arraynew = new int [a[i] + b[i]];

The way to create the new array with the correct size is  
`int [] arraynew = new int [a.length + b.length];`

after that you need a for loop to populate it. 😄

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 9, 2020, 9:24pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/13 "2020-04-09T21:24:55Z")

</div>

Thank you all for the fast help.

Now i tried like this. (I am not good in IT)

* * *

int[] a = { 1, 2, 25 };  
int[] b = { 9, 18 };  
int [] arraynew = new int [a.length + b.length];

for (int i=0; i \< a.length ; i++)  
{  
for( int j=0; j \< b.length; j++)  
{  
arraynew[i] = a[i] + b[i];

println(arraynew[i]);

}  
}

* * *

Does someone have another hints?  
Thank you all for your feedback.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [April 9, 2020, 9:55pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/14 "2020-04-09T21:55:43Z")

</div>

As others have stated you might simply want to try  
`concat();`

> **[concat() / Reference](https://processing.org/reference/concat_.html)**
>
> Concatenates two arrays. For example, concatenating the array { 1, 2, 3 } and the array { 4, 5, 6 } yields { 1, 2, 3, 4, 5, 6 }. Both parameters must be arrays of the same datatype. When u…

however if the goal is to code the functionality yourself, the code you have provided is almost there.

Remember when coding break your task down.

You want to join two arrays, so the indecies of one array has to go into the other.

To access ONE array we use a for loop, this allows us to access ALL values in said array.

So if i

```auto
for (int i=0;i<array.length;i++){
  int k = array[i];
  
}

```

now this doesn’t do anything other than access the value.  
We also know how to create and add values to arrays.

```auto
int[] array = new Array[5];

for (int i=0;i<array.length;i++){
  array[i] = random(200);
}

```

its really the same thing because the size of the array is not amended by the for loop and is instanced before it.

However this tells us that if we want to add to any array we only need one for loop providing that the data is only coming from one array

If however the data comes from two arrays, you will need to use a for loop twice, this doesn’t mean a nested for loop, it means run a for loop once to add values of the first array, then again for the second array.

```auto
int[] a = { 1, 2, 25 };
int[] b = { 9, 18 };
int [] arraynew = new int [a.length + b.length];

for (int i=0; i < a.length ; i++)
{
for( int j=0; j < b.length; j++)
{
arraynew[i] = a[i] + b[i];

println(arraynew[i]);

}
}

```

note you are making use of nested loops, which wont have the intended effect here. Also all that aside you aren’t using j to locate any of the values. Finally because

```auto
arraynew[i] = a[i] + b[i];

```

is linked to the for loop this will cause issues if the arrays arent the same size;

Anyways I think the answer has already been answered much better by others, however if you have to use for loops then, you have to consider the steps you have to go through.

Dan Shiffman’s videos on the topic are more than suited for this task and providing you watch them should help you.

[![](https://img.youtube.com/vi/am6e1U2BHkA/hqdefault.jpg "9.4: Arrays and Loops - Processing Tutorial") ](https://www.youtube.com/watch?v=am6e1U2BHkA)

[![](https://img.youtube.com/vi/tA_ZgruFF9k/maxresdefault.jpg "7.5: Removing Objects from Arrays - p5.js Tutorial") ](https://www.youtube.com/watch?v=tA_ZgruFF9k)

[![](https://img.youtube.com/vi/RXWO3mFuW-I/maxresdefault.jpg "7.2: Arrays and Loops - p5.js Tutorial") ](https://www.youtube.com/watch?v=RXWO3mFuW-I)

---

<div class="post-metadata">

**Author:** ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)\
**Post date:** [April 9, 2020, 10:07pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/15 "2020-04-09T22:07:29Z")

</div>

Guys really,

the videos are not what I want as an answer. They I know this channel but what I just want to know is

How I can two arrays in Processing to one without using concat().

This forum is really bad if someone needs help. Sorry for that.  
I am middle in the exams and need help and not somebody who just says what I need to watch in the Internet.

Just say the right solution for my question or an hint how I can find the solution.

I have another exam questions where I dont know how I can find the solution.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 9, 2020, 10:22pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/16 "2020-04-09T22:22:30Z")

</div>

```auto
int[] a = { 1, 2, 25 };
int[] b = { 9, 18 };
int[] c = new int [a.length + b.length];

```

Is OK you now have three arrays  
a has 3 elements,  
b has 2 elements and  
c has 5 elements

If we look inside them we have

```auto
                  Array
Index a b c
 0 1 9 0
 1 2 18 0
 2 25 X 0
 3 X X 0
 4 X X 0

```

Note X means there is _ **no such array element** _  
Afterwards you want hem to look like this

```auto
                  Array
Index a b c
 0 1 9 1
 1 2 18 2
 2 25 X 25
 3 X X 9
 4 X X 18

```

To populate the `c` array you need two loops one to copy the `a` array into elements 0-2 and a second to copy the `b` array into elements 3-4

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 9, 2020, 10:32pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/17 "2020-04-09T22:32:34Z")

</div>

Here is the questions in English thanks to google translate

> Merge arrays
> 
> There are two arrays a and b. Create a new array c that is as long as a and b taken together and also contains the values ​​of a and b (in that order).
> 
> int a = {1, 2, 25}; int b = {9, 18}; // write code here println ©;
> 
> You should see this on the console here:
> 
> [0] 1 [1] 2 [2] 25 [3] 9 [4] 18
> 
> Do not use the concat () or splice () processing functions. Your code should also work if you add or remove elements from a and / or b.
> 
> tip
> 
> Break the problem down into two steps. In step 1, fill the first part of c with the values ​​of a. In step 2, fill the second part of c with the values ​​of b.
> 
> You solve each step with your own for loop. Step 1 is easy because the index numbers of a and c are equal (c [0] gets value of a [0] etc.). Step 2 requires a little change. What is the index number in c for the first value of b?

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [April 9, 2020, 10:33pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/18 "2020-04-09T22:33:44Z")

</div>

New rules means we cannot hand out answers for assignments, which this seems to indicate considering your refusal to use concat.

I’m pointing you to the video for this precise reason, the problem here is not too difficult providing that you think about the steps.

ie

```auto
//step 1------------------------- create new array
float[] array = new float[a.length.b.length];

//step 2------------------------- Read from array a
for(int i=0;i<a.length;i++){
?????
}
//step 2------------------------- Read from array b
for(int i=0;i<b.length;i++){
?????
}

// step 4-----------------------------????

```

this is really the entirety of the code, but I cannot help more than this.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 9, 2020, 10:36pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/19 "2020-04-09T22:36:05Z")

</div>

@paulgoux I think the first array should also be of type `int` to match the other arrays, just to be consistent and keep things simple

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [April 9, 2020, 10:40pm UTC](https://discourse.processing.org/t/processing-to-make-2-array-in-one/19587/20 "2020-04-09T22:40:25Z")

</div>

I’m writing for examples and not paying attention, but yes it is as you say, it all arrays should have the same type or you will have issues.
