简体   繁体   中英

How to merge two arrays in Java into one sorted ArrayList?

I have lArr (left) {1,2,4,5} and rArr (right) {6,8,10,13} , I want to merge them into one sorted array, but my code is not functioning how I want it to.

private static ArrayList merge(int[] lArr, int[] rArr) {
    ArrayList mergedArray = new ArrayList();
    int i = 0;
    int j = 0;
    while (i < lArr.length && j < rArr.length) {
        if (lArr[i] < rArr[j]) {
            mergedArray.add(lArr[i]);
            i++;
        }
        // so up until here, the code runs,
        // but it never reaches the else segment.
        else {
            mergedArray.add(rArr[j]);
            j++;
        }
    }
    return mergedArray;
}

after calling the merge() method from the main, lArr(left array) is only displayed.

You should merge the arrays first and then sort the array.
You can easily merge the arrays by creating an ArrayList and for each element in your arrays, add them to the ArrayList, like this:

// just an example of values
int[] lArr = {1,2,8};
// just an example
int[] rArr = {-7,54,9,34,27};
ArrayList<Integer> mergedList = new ArrayList<Integer>();
// Loop through lArr and add each element to mergedList
for(int element : lArr) mergedList.add(element);
// Loop through rArr and add each element to mergedList
for(int element : rArr) mergedList.add(element);

Now you can sort the list using Collections.sort(list, comparator) , like this:

Collections.sort(mergedList, new Comparator<Integer>() {
   // A comparator compares two value (logic)
   // This function needs to return 1 if a > b and -1 if b < a
   // and if a = b then 1 or -1 won't change anything
   public int compare(Integer a, Integer b){
      if(a > b) return 1;
      return -1;
   }
});

You can merge and sort two arrays using streams :

public static void main(String[] args) {
    int[] arr1 = {5, 2, 3, 4, 1};
    int[] arr2 = {2, 1, 3, 4, 5};
    ArrayList<Integer> list = merge(arr1, arr2);

    System.out.println(list);
    // [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
}
private static ArrayList<Integer> merge(int[] lArr, int[] rArr) {
    return Stream.of(lArr, rArr)
            .flatMapToInt(Arrays::stream)
            .sorted()
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}

You might want to add a custom comparator and use varargs :

public static void main(String[] args) {
    int[] arr1 = {5, 2, 5, 4, 1};
    int[] arr2 = {2, 1, 3, 4, 3};
    ArrayList<Integer> list =
            merge(Comparator.reverseOrder(), arr1, arr2);

    System.out.println(list);
    // [5, 5, 4, 4, 3, 3, 2, 2, 1, 1]
}
private static ArrayList<Integer> merge(
        Comparator<Integer> comparator, int[]... arrays) {
    return Stream.of(arrays)
            .flatMapToInt(Arrays::stream)
            .boxed()
            .sorted(comparator)
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM