# Switch the array places in a loop

**URL:** <https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 20, 2020, 11:56am UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084 "2020-05-20T11:56:50Z")\
**Posts on this page:** 13\
**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:** [May 20, 2020, 11:56am UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/1 "2020-05-20T11:56:50Z")

</div>

Hello Together,

I have an important question.

I have a task where I need your help.

this is the question.

```auto
Given! Be! Two! Equal! Long! Arrays:!
int[] a = {1,2,3,4};
int[] b = {10, 20, 30, 40};
Create a new array c that alternately contains all the values of a and b, that is, the value of the array.
then the first value of a, then the first value of b, then the second value of a, etc.
Your output in the example should look like this
[0]!1!
[1]!10!
[2]!2!
[3]!20!
[4]!3!
[5]!30!
[6]!4!
[7]!40!
Your function should work for any value (and any number of values) for any number of values for a and b.
as long as the length of both a and b is the same.

```

Now I have made two for loops.  
This is my solution but its not correct:

```auto
int[] a = {1, 2, 3, 4};
int[] b = {10, 20, 30, 40};

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

for(int i =0; i<a.length; i++)
{
  c[i] = a[i];
}

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

println(c);

```

How I can switch the array places where the c array starts at first with a[0] then with b[0] and so on?

Could someone give me a hint.  
Then I will try it again.

Thank you.  
Best wishes.

gurki

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 20, 2020, 12:50pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/2 "2020-05-20T12:50:44Z")

</div>

Hi gurki,

I like your attitude. You ask for a hint, not a solution.

With for loops you can loop through c[] and with each step either pick value from a[] or b[]. Second choice is a bit similar what you have started with. Loop through length a[]. With each value of i you first add a value from a[] to c[] and then a value from b[] to c[]. Trick is in how you construct index for c[].

If this was too cryptic, ask and I’ll help you a bit more.

---

<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:** [May 20, 2020, 1:00pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/3 "2020-05-20T13:00:26Z")

</div>

- I guess that’s called array zipping or something like that.
- All values in array _a[]_ will go into _c[]_'s even indices.
- All values in array _b[]_ will go into _c[]_'s odd indices.
- You can make 1 loop to iterate over _c[]_ by 2 increment steps:  
`for (int i < 0; i < c.length; i += 2) {`
- Take advantage that in Java when we divide 2 int values, we get it truncated to an integer too:  
`c[i] = a[i / 2];`  
`c[i + 1] = b[i / 2];`

---

<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:** [May 20, 2020, 1:52pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/4 "2020-05-20T13:52:45Z")

</div>

Unfortunately. I could not find the solution.  
Could you please give me a further hint.

I tried it like this

```auto
int[] a = {1, 2, 3, 4};
int[] b = {10, 20, 30, 40};

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

for(int i =0; i<a.length; i+=2)
{
  c[i] = a[i/2];
  for(int j=0; j <c.length; j+=2)
  {
    c[j+1] = b[j/2];
  }
}

println(c);

```

The 4. array place and the 6. place are 0.

---

<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:** [May 20, 2020, 1:58pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/5 "2020-05-20T13:58:09Z")

</div>

Hallo

die Formatierung der Aufgabe gelingt dir nicht. Bitte nicht als Code sondern als Zitat formatieren.

LG

Chrisir

---

<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:** [May 20, 2020, 2:01pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/6 "2020-05-20T14:01:25Z")

</div>

You only need one for loop

The trick is to read properly from both arrays

Handle the index correctly

---

<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:** [May 20, 2020, 2:17pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/7 "2020-05-20T14:17:07Z")

</div>

I suggest that you use the approach presented by @SomeOne perhaps this will make it clearer

```
Loop through length `a[]`. With each value of `i` you 
    add a value from `a[]` to the *next available* element in `c[]` 
    add a value from `b[]` to the *next available* element in `c[]`
```

All you need is a variable to remember the _next available_ element. Think about it, at the beginning the first available element is at [0], the net avialble is [1] and the one after that [2] and so on.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 20, 2020, 2:21pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/8 "2020-05-20T14:21:40Z")

</div>

You don’t actually need to remember the next available element if you are smart with indexing. From a[] index is `i*2` and from b[] index is `i*2+1`. That’s actually how even and odd numbers are defined in mathematics.

---

<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:** [May 20, 2020, 2:28pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/9 "2020-05-20T14:28:16Z")

</div>

> [@SomeOne](#):
>
> You don’t actually need to remember the next available element if you are smart with indexing.

True, I was just trying to avoid the maths, to me it seemed simpler to have a variable that you increment by 1 each time you store a value in `c[]`. In fact using post increment ++ can make the code even less verbose and more efficient.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 20, 2020, 3:31pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/10 "2020-05-20T15:31:12Z")

</div>

> [@quark](#):
>
> to me it seemed simpler to have a variable that you increment by 1 each time you store a value in `c[]` .

You are probably right. I was thinking problem as an experienced programmer. I’d do believe that the most elegant solution is the best one, but it’s not one that you come by easily and thus not suitable for beginners.

---

<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:** [May 20, 2020, 3:35pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/11 "2020-05-20T15:35:31Z")

</div>

> [@SomeOne](#):
>
> I was thinking problem as an experienced programmer.

So was I … 😀

---

<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:** [May 20, 2020, 3:36pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/12 "2020-05-20T15:36:10Z")

</div>

> [@SomeOne](#):
>
> From a index is `i*2` and from b index is `i*2+1` .

These are the Indexes for c not for a and b. Probably that’s confusing too that the three arrays use different indexes.

---

<div class="post-metadata">

**Author:** ![GSA\_IxD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gsa_ixd/32/7554_2.png) [@GSA\_IxD](https://discourse.processing.org/u/GSA_IxD)\
**Post date:** [May 25, 2020, 11:09pm UTC](https://discourse.processing.org/t/switch-the-array-places-in-a-loop/21084/13 "2020-05-25T23:09:23Z")

</div>

Hi. It’s simpler than you think!  
You need to step through c in increments of 2. This means a single for loop:  
`for (int i = 0; i < c.length; i+=2)`  
then in this loop assign a[i/2] to c[i] and then b[i/2] to c[i+1]  
Done!
