简体   繁体   English

将两个已排序的Arraylist合并为一个已排序的Arraylist

[英]Merging two sorted Arraylists into one sorted Arraylist

My code should merge two already sorted arraylists into one sorted arraylist and if one of the arraylists used is not sorted then it should return null. 我的代码应将两个已经排序的数组列表合并到一个排序的数组列表中,如果使用的数组列表之一未排序,则它应该返回null。

public class MergeSorted {
public static void merge(ArrayList<Integer> a, ArrayList<Integer> b) {

    for (int i = 0, j = 0; j < b.size(); i++) {
        if (i == a.size() || a.get(i) > a.get(j)) {
            a.add(i, b.get(j++));
        }
    }
}  
}

This is what I attempted but can't get the idea of returning null if they are not equal, Im new to java and this is my second week so please be patient with me. 这是我尝试过的方法,但是如果它们不相等,将无法返回null,这是Java的新手,这是我第二周了,请耐心等待。 I know I should have an if statement checking if they are sorted and an else but what should I include inside the if? 我知道我应该有一个if语句来检查它们是否已排序,而else则应该包含在if中吗?

Problem 问题

Check if the two lists are sorted, if they are then it will merge the two lists into a single sorted list, whereas if the lists are not sorted return null. 检查两个列表是否已排序,如果两个列表已排序,则它将两个列表合并为一个排序的列表,如果列表未排序则返回null。

Code Solution: 代码解决方案:

Try the following code: 尝试以下代码:

public class MergeSorted {
public static List merge(List<Integer> aList, List<Integer> bList) {

    List mergeList = new ArrayList<Integer>();

    //checking if list 'A' is sorted
    List temp = new ArrayList(aList);
    Collections.sort(temp);
    boolean aSorted = temp.equals(aList);

    //checking if list 'B' is sorted
    temp = new ArrayList(bList);
    Collections.sort(temp);
    boolean bSorted = temp.equals(bList);

    //if both lists are sorted then merge them
    if(true == aSorted && true == bSorted) {
        mergeList.addAll(aList);
        mergeList.addAll(bList);
        Collections.sort(mergeList);
    }

   return mergeList; 
    }
  }

The generic way is something like this: 通用方式是这样的:

public class MergeSort {

    public static <T extends Comparable<? super T>> List<T> merge(List<T> a, List<T> b) {
        if (!isSorted(a) || !isSorted(b)) {
            return null;
        }

        final List<T> result = new ArrayList<T>(a.size() + b.size());
        result.addAll(a);
        result.addAll(b);

        Collections.sort(result);

        return result;
    }

    private static <T extends Comparable<? super T>> boolean isSorted(final List<T> list) {
        T prevItem = null;
        for (T item : list) {

            if (prevItem != null && item.compareTo(prevItem) < 0) {
                return false;
            }

            prevItem = item;
        }

        return true;
    }

}

You can easily replace all these generic Ts with Integer if you only need it for them... 如果只需要它们,可以用Integer轻松替换所有这些通用T。

  • Please refer to this link for comparing if the List implementation is equal. 如果List实现相等,请参考此链接进行比较。 Please note that the ArrayList implementation of yours contains List<Integer> and thus the sorting takes place in the natural order. 请注意,您的ArrayList实现包含List<Integer> ,因此排序以自然顺序进行。 In case of equal return true or else false . 如果相等,则返回truefalse You can change it as required to return any flag. 您可以根据需要更改它以返回任何标志。

Sort and compare 2 lists if equal - List of String 排序并比较2个列表(如果相等)-字符串列表

  • There are useful Apache Common library classes available, which can help you in merging the array lists that are sorted : This will give you what you are looking for. 有许多有用的Apache Common库类,可以帮助您合并排序的数组列表:这将为您提供所需的内容。 org.apache.commons.collections4 package. org.apache.commons.collections4软件包。 Apache Common Lib version 4 - collate() Apache Common Lib版本4-collat​​e()

eg., List<Integer> mergedList = CollectionUtils.collate(list1, list2); 例如, List<Integer> mergedList = CollectionUtils.collate(list1, list2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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