# Processing to make 2 array in one

please format code with </> button * homework policy * asking questions

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.

Hello,

Welcome to the forum.

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

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.

6.3: For Loop - Processing Tutorial - YouTube (searched coding train for)
9.1: What is an Array? - Processing Tutorial - YouTube (searched coding train array)

`:)`

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

 1
 2
 25
 9
 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.

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:

 1  2  25  9  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 bekommt Wert von a usw.). Schritt 2 erfordert eine kleine Änderung. Wie lautet die Indexzahl in `c` für den ersten Wert von `b` ?

Thank You…

1 Like

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

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

`:)`

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.

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.

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

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

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

``````

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. 1 Like

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.

As others have stated you might simply want to try
`concat();`
https://processing.org/reference/concat_.html

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

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

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

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

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.

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

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

4 Likes

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.

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

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

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

2 Likes

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:

 1  2  25  9  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  gets value of a  etc.). Step 2 requires a little change. What is the index number in c for the first value of b?

2 Likes

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

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

2 Likes

@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

1 Like

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.