0

Write a method

public static ArrayList merge(ArrayList a, ArrayList b)

that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elemts from the longer array list. For example, if a is

1 4 9 16

and b is

9 7 4 9 11

then merge returns the array list

1 9 4 7 9 4 16 9 11

Okay this is my code so far.

All I need to do is merged two inputs given by user in same fashion as here

I asked same question earlier but that was for Array and i got satisfied result.

I tried solutions from that link but I have no result If someone can please give me quick hint on how to merge two ArrayList, I will appreciate the help.

import java.util.ArrayList;
import java.util.Scanner;


public class HomeWork6_27_B{


public static void main(String arg[]) {

    Scanner scan = new Scanner(System.in);
    Scanner scan2 = new Scanner(System.in);
    ArrayList<Integer> a = new ArrayList<>();
    ArrayList<Integer> b = new ArrayList<>();

    System.out.print("Enter Array of numbers in First Set, type 0 when finished:");
    System.out.println();
    int in = scan.nextInt();
    a.add(in);
    in = scan.nextInt();
    while (in !=0){
        a.add(in);
        in = scan.nextInt();
    }
    System.out.println("End of First Set");

    System.out.print("Enter Array of numbers in Second Set, type 0 when finished:");
    System.out.println();
    int in2 = scan2.nextInt();
    b.add(in2);
    in2 = scan2.nextInt();
    while (in2 !=0){
        b.add(in2);
        in2 = scan2.nextInt();
    }
    System.out.println("End of Second Set");
}

}
Community
  • 1
  • 1
BabyC0d3eR
  • 27
  • 8

1 Answers1

1

I haven't used java for a while but I'm pretty sure this should work. Basically we loop to the end of the longer list, and add an element from each input list if we haven't reached the end of that input list.

ArrayList<Integer> merged = new ArrayList<Integer>();

// Merge the lists
for (int i = 0; i < Math.max(a.size(), b.size()); i++) {
    if (i < a.size()) merged.add(a.get(i));
    if (i < b.size()) merged.add(b.get(i));
}

for (int i = 0; i < merged.size(); i++) {
    System.out.print(merged.get(i).toString() + " ");
}
timleathart
  • 520
  • 1
  • 5
  • 20
  • so my out put would be system.out.println(merged)? – BabyC0d3eR Feb 26 '15 at 03:07
  • I appreciate help but I am really new to java. I am not and not sure how to implement that into my code. – BabyC0d3eR Feb 26 '15 at 03:12
  • Pretty perfect answer for someone who hasn't used Java in a while. +1. – Shashank Feb 26 '15 at 03:15
  • I have edited the original post. You need to loop through the new list and print each element separately. – timleathart Feb 26 '15 at 03:16
  • 1
    @TimLeathart make sure to print out the element at index `i`, not the int `i` itself. Also due to autoboxing and unboxing and some other stuff in Java, there's no need to use `.toString()`. It can even understand what to do with string concatenation. – Shashank Feb 26 '15 at 03:17
  • So does the code work after you run it. my eclipse shows error – BabyC0d3eR Feb 26 '15 at 03:24
  • Actually disgregard that autoboxing unboxing bit. I'm pretty sure it's just operator and method overloading that causes that functionality. – Shashank Feb 26 '15 at 03:27
  • This method must return result type of ArrayList – BabyC0d3eR Feb 26 '15 at 03:30
  • I'm guessing you have put the code I posted into a method `public static ArrayList merge(ArrayList a, ArrayList b)`, if this is the case then you need to add `return merged;` to the bottom of the method. – timleathart Feb 26 '15 at 03:32
  • It does not give me merged ArrayList. There are no error but no result either – BabyC0d3eR Feb 26 '15 at 03:42
  • @ Shashank does my code make sense to you guys? Can you please help me get it print(output) as intended. This is due in next 30 mins – BabyC0d3eR Feb 26 '15 at 03:46