0

I have tried to solve this exercise for almost three hours now and I still don't understand what I am doing wrong. I am supposed to take two ArrayLists with numbers and merge them into one,but here's the catch, they must sorted like this:

If arraylist "A" has the numbers [1, 2, 3] and arraylist "B" has [9, 8, 7, 6, 5] then ArrayList "merge" should be [1, 9, 2, 8, 3, 7, 6, 5].

I.e it should alternate the numbers from the inputted arraylists A and B. If one arraylist is longer it should just keep on filling with numbers (like what happened to [7,6,5] in this case. Also, we don't know the length of any of the arrayLists.

Here's one solution that I think should work well, but I can't get it to work. All help is extremely appreciated!!

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

public class test19 {
    public static void main(String[] args) {
        ArrayList<Integer> arrayListA = new ArrayList<>();
        ArrayList<Integer> arrayListB = new ArrayList<>();
        Scanner sc = new Scanner(System.in);


        while(true) {
            System.out.println("Write a number to place in ArrayList A, quit with '-1'");
            int Local = sc.nextInt();
            if(Local > 0) {
                arrayListA.add(Local);
            } else {
                break;
            }

        }
        System.out.println();
        System.out.println();

        while(true) {
            System.out.println("Write a number to place in ArrayList B, quit with '-1'");
            int Local = sc.nextInt();
            if(Local > 0) {
                arrayListB.add(Local);
            } else {
                break;
            }

        }
        System.out.println(merge(arrayListB, arrayListA));

    }

    public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) {
        ArrayList<Integer> merge = new ArrayList<>();

        if(a.size() < b.size()) {
            //here we check which list is the smallest and use that one (so we don't try to add blankspaces from the longer list to the merge list)
            for(int i = 0; i <= a.size(); i++) {
                merge.add(a.get(i));
                merge.add(b.get(i));

            }
            for(int j = a.size(); j <= b.size(); j++) {
                merge.add(b.get(j)); //here we add the leftover numbers to the list

            }

        } else { //this means that list A is bigger than list B
            for(int i = 0; i <= b.size(); i++) {
                merge.add(a.get(i));
                merge.add(b.get(i));
            }
            for(int j = b.size(); j <= a.size(); j++) {
                merge.add(b.get(j));
            }
        }


        return merge;
    }
}
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
TheSmile
  • 74
  • 7
  • 1
    `j<=a.size()` should be `j – Steve Smith Jan 31 '19 at 16:16
  • Possible duplicate of [Best way to merge and remove duplicates from multiple lists in Java](https://stackoverflow.com/questions/16453255/best-way-to-merge-and-remove-duplicates-from-multiple-lists-in-java) – alain.janinm Jan 31 '19 at 16:29

3 Answers3

0

Here's your code fixed. Mainly it was a case of changing all the <= to just <. If you compare the differences you'll be able to see where you went wrong:-

import java.util.ArrayList;
import java.util.Arrays;

public class test19{
    public static void main(String[]args){
        ArrayList<Integer> arrayListA = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
        ArrayList<Integer> arrayListB = new ArrayList<Integer>(Arrays.asList(11, 12, 13, 14, 15, 16, 17));
        System.out.println(merge(arrayListA,arrayListB));
    }

    public static ArrayList<Integer> merge (ArrayList<Integer> a, ArrayList<Integer> b){
        ArrayList<Integer> merge = new ArrayList<Integer>();

        if (a.size()<b.size()) {
            //here we check which list is the smallest and use that one (so we don't try to add blankspaces from the longer list to the merge list)
            for(int i=0; i<a.size(); i++){
                merge.add(a.get(i));
                merge.add(b.get(i));    
            }
            for(int j=a.size(); j<b.size(); j++){
                merge.add(b.get(j)); //here we add the leftover numbers to the list    
            }
        } else { //this means that list A is bigger than list B
            for(int i=0; i<b.size(); i++){
                merge.add(a.get(i));
                merge.add(b.get(i));
            }
            for(int j=b.size(); j<a.size(); j++){
                merge.add(a.get(j));    
            }
        }
        return merge;
    }
}
Steve Smith
  • 2,008
  • 2
  • 14
  • 21
0

I have corrected your merge function, you just need to use < operator in for loops instead of <=

public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) {
    ArrayList<Integer> merge = new ArrayList<Integer>();

    if(a.size() < b.size()) {
        //here we check which list is the smallest and use that one (so we don't try to add blankspaces from the longer list to the merge list)
        for(int i = 0; i < a.size(); i++) {
            merge.add(a.get(i));
            merge.add(b.get(i));
        }
        for(int j = a.size(); j < b.size(); j++) {
            merge.add(b.get(j)); //here we add the leftover numbers to the list
        }

    } else { //this means that list A is bigger than list B
        for(int i = 0; i < b.size(); i++) {
            merge.add(a.get(i));
            merge.add(b.get(i));
        }
        for(int j = b.size(); j < a.size(); j++) {
            merge.add(a.get(j));
        }
    }
    return merge;
}
aBnormaLz
  • 713
  • 4
  • 19
Imran Ali
  • 1
  • 1
0

Others have already answered this, just providing an alternate implementation that may be easier to read.

import java.util.*;
public class Main {
    public static void main(String[] args) 
    {
        ArrayList<Integer> arrayListA = new ArrayList<Integer>();
        ArrayList<Integer> arrayListB = new ArrayList<Integer>();

        arrayListA.add(1);
        arrayListA.add(2);
        arrayListA.add(3);

        arrayListB.add(9);
        arrayListB.add(8);
        arrayListB.add(7);
        arrayListB.add(6);
        arrayListB.add(5);

        merge(arrayListA, arrayListB);
    }

    public static void merge(ArrayList<Integer> arrayListA, ArrayList<Integer> arrayListB)
    {
        ArrayList<Integer> merged = new ArrayList<Integer>();

        int maxSize = Math.max(arrayListA.size(), arrayListB.size());

        for (int i = 0; i < maxSize; i++)
        {
            if(i < arrayListA.size())
                merged.add(arrayListA.get(i));
            if(i < arrayListB.size())
                merged.add(arrayListB.get(i));
        }

        for(int i = 0; i < merged.size(); i++)
        {
            System.out.println(merged.get(i));
        }
    }
}
hanoldaa
  • 596
  • 4
  • 10