• Post Reply Bookmark Topic Watch Topic
  • New Topic

Joining two array lists, alternate elements  RSS feed

 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm given the task of creating a method that merges two array lists while alternating their elements.

Example:
List 1: 4, 2, 8, 9, 3
List 2: 1, 8, 3, 0, 3

Result: 4, 1, 2, 8, 8, 3, 9, 0, 3, 3

I just created a method to merge the two arrays, but I have no idea where to start to alternate their elements.
I would appreciate any help that puts me in the right direction.

Thanks!
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Carmine.

1. Please show us what have you got so far.

2. I'm unclear a bit what you mean by "alternating their elements". Please explain a bit more.
3. Please clarify are you talking about Arrays or ArrayList class? Those two are different in Java.
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch Carmine.

1. Please show us what have you got so far.

2. I'm unclear a bit what you mean by "alternating their elements". Please explain a bit more.
3. Please clarify are you talking about Arrays or ArrayList class? Those two are different in Java.

Thanks for your reply.
I gave an example of what I mean by alternating their elements in my original post but here's another one:
ArrayList1 = {3, 9, 1, 4, 2, 1, 8}
ArrayList2 = {b, c, a}

The result after running it through the method should be: {3, b, 9, c, 1, a, 4, 2, 1, 8}
I'm talking about ArrayList

Here is what I have so far:
I have managed to merge the two arrays into one, I just don't know how to start on getting it to alternate the elements.


Any input would be greatly appreciated!
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try describing the process in plain English. What would you do if you did it yourself manually? Imagine you were working with lists and items that you could physically hold and manipulate.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example:

If I had a list and was given another list to merge into, this is what I would do:

1. Referring to the list I have as "this list" and the list I've been given to merge as "the other list"
2. Take the first element from the other list and add it to this list.
3. Repeat step 2 until I've gone through all the elements of the other list
4. I would then end up with a list that had my original list merged with the other list.

This is how you might describe the code you have in your append() method in plain English.  If you can describe the alternating merge process in a similar manner, then you're a step closer to translating into valid Java instructions.
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple for loop is enough to solve the problem.



Hope, this is helpful. Please let me know if you can't get it.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Gautham, thank you for your suggestion. However, we encourage our community members to give hints and guidance rather than outright solutions before the original poster (OP) has had a chance to work out a solution for themselves. This maximizes the OP's learning experience and minimizes our culpability in aiding and abetting plagiarism if OP is inquiring about something that's related to school work.

The solution you offered is incomplete because it doesn't account for arrays/lists that have different lengths. Please allow the OP to work out the rest of the solution for himself. Thanks.
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Try describing the process in plain English. What would you do if you did it yourself manually? Imagine you were working with lists and items that you could physically hold and manipulate.


1. Gather both lists.
2. Get the first element from the first list and put it in a new list.
3. Get the first element from the second list and put it in the new list created in step 2.
4. Continue process 2-3 for each element until there is no elements left in either the first or second list.

I appreciate the help into guiding me to figure out the solution.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that's a step forward. You have used an enhanced for loop before but you'll need more control with this process. Use a regular for-loop. Look up List.iterator() to get an idea of how you're going use it to do steps 2 and 3. Keep in mind that you want to walk through two lists. Step 4 will be handled in the for-loop but you'll have to tweak that statement a little bit and say it a different way in Java.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4. Continue process 2-3 for each element until there is no elements left in either the first or second list.

The 4th step needs to be broken down into several steps that describe what is needed to be done to continue after the first items have been copied from each list.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
4. Continue process 2-3 for each element until there is no elements left in either the first or second list.

The 4th step needs to be broken down into several steps that describe what is needed to be done to continue after the first items have been copied from each list.

Actually, if OP translates to Java, step 4 can be a Boolean expression that determines whether or not to keep iterating. In Steps 2 and 3, you also need to check something before proceeding to take action. Think of what will happen when the lists are not the same size and you just blindly proceed.
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Ok, that's a step forward. You have used an enhanced for loop before but you'll need more control with this process. Use a regular for-loop. Look up List.iterator() to get an idea of how you're going use it to do steps 2 and 3. Keep in mind that you want to walk through two lists. Step 4 will be handled in the for-loop but you'll have to tweak that statement a little bit and say it a different way in Java.


Okay, correct me if I'm wrong:
- Get the size of both lists, compare them, and repeat for as many times as the size of the largest list.
- using hasNext() check if there is a next element in the first list
- if true, get the next element in the first list and add it to the new list
- using hasNext() check if there is a next element in the second list
- if true, get the next element in the second list and add it to the new list

I feel like I'm missing stuff in between
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't really need to worry about the size of either list. Like I said, look up List.iterator() and see what else it can do. The rest of what you have looks about right.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, make sure you understand what hasNext() does. Does it have any side effects or not? If you call it multiple times, does it change the state of the Iterator?
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You don't really need to worry about the size of either list. Like I said, look up List.iterator() and see what else it can do. The rest of what you have looks about right.

I'm looking at the List.iterator() in the Java API now but I don't see anything else there that I would use in this scenario.

Junilu Lacar wrote:Also, make sure you understand what hasNext() does. Does it have any side effects or not? If you call it multiple times, does it change the state of the Iterator?

Returns true if the list iterator has has more elements in the forward direction. Nothing of any side effects for it being called multiple times.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me put it this way: if you're using an Iterator, do you really need the size? Look for examples of using an Iterator with a normal for-loop. Also, in a normal for-loop, you can initialize multiple loop variables.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you understand why you don't need to worry about size, you already have everything you need. You're really close. Just write down what you think will work and give it a try. Nothing teaches like mistakes so don't be afraid to make them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!