# Switch the array places in a loop

Hello Together,

I have an important question.

I have a task where I need your help.

this is the question.

``````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
!1!
!10!
!2!
!20!
!3!
!30!
!4!
!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:

``````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 then with b and so on?

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

Thank you.
Best wishes.

gurki

2 Likes

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.

2 Likes
• 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];`
2 Likes

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

I tried it like this

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

1 Like

Hallo

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

LG

Chrisir

You only need one for loop

The trick is to read properly from both arrays

Handle the index correctly

3 Likes

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 , the net avialble is  and the one after that  and so on.

2 Likes

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.

2 Likes

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.

3 Likes

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.

So was I … 1 Like

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

2 Likes

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!

1 Like