• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to mix two arrays in Java?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys im starting to learn Java by myself and stumbled upon this exercise.

The method behaves similarly to , except that it has n different arrays as input. The signature is therefore . Again, the numbers from the input arrays are to be inserted into the result arrays in turn. Likewise, arrays that have already been completely processed are to be skipped until all values of the input arrays have been used up. If the input array is empty, an empty array is returned.

Example: with the result is .

Can anyone help me with the code?

I have tried using the answer from https://stackoverflow.com/questions/2682053/how-to-mix-two-arrays-in-java but my exercise needs me to use and i have no idea how to go about it.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's move you to a more appropriate forum.

Please supply more information. For example, are you guaranteed that the component arrays will all have the same length?
The answer in SO is good, but you need to revise the behaviour of i++ to understand it. The expression i++ has the same value as the old value of i. The new value of i is hidden until the next time you use it. If you go through the suggestions on SO, use a pencil to write down the values of the two arrays, remembering that an int[] starts off full of 0s, and a T[] where T is any type of object, starts off full of nulls. Then please tell us what you have learnt.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Johnanthan Grobmueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys im starting to learn Java by myself and stumbled upon this exercise.

The method behaves similarly to , except that it has n different arrays as input. The signature is therefore . Again, the numbers from the input arrays are to be inserted into the result arrays in turn. Likewise, arrays that have already been completely processed are to be skipped until all values of the input arrays have been used up. If the input array is empty, an empty array is returned.

Example: with the result is .

Can anyone help me with the code?

I have tried using the answer from https://stackoverflow.com/questions/2682053/how-to-mix-two-arrays-in-java but my exercise needs me to use and i have no idea how to go about it.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Johnanthan,

welcome to the Ranch and enjoy the stay!

At StackOverflow they gave this method:

But this is exactly the same as this:

Now, you talk about int[][] instead of String[][], but the method will be exactly the same. Can you give it a try?
 
Johnanthan Grobmueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Johnanthan,

welcome to the Ranch and enjoy the stay!

At StackOverflow they gave this method:

But this is exactly the same as this:

Now, you talk about int[][] instead of String[][], but the method will be exactly the same. Can you give it a try?



I have tried that but it doesnt work since you have to thru the colomuns first and then the raws. if we got 5,2,3 and 1,6,7 the array should look like this :
5,1,2,6,3,7
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear to have asked the same question twice, so I have merged the two threads; you may find the replies out of chronological order.

The ellipsis/varargs operator ... takes multiple inputs and creates an array from them, which means the two following method signatures are equivalent to each other.The ... operator is more versatile than [] because it will accept one array or several individual elements as its arguments. There is however a slight performance overhead at runtime.
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the method from StackOverflow:

Now, replace every String[] by int[], and replace String[]... by int[][], so you get:
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Useful quote, Piet; thank you
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johnanthan Grobmueller wrote:Hi guys im starting to learn Java by myself . . . I have tried using the answer from . . . stackoverflow . . . and i have no idea how to go about it.



You can use a loop to iterate through each array in the input array, and add the elements to a new array. Use a counter to ensure all values are added in the correct order.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please don't quote the whole of a preceding post; that adds nothing new and is liable to removal.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic