合并list1中的两个数组列表,同时保持排序。[英] merge two arraylist lists in list1 while it remain sorted

本文是小编为大家收集整理的关于合并list1中的两个数组列表,同时保持排序。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的作业中,第三步是调用该方法合并以合并List1中的两个列表,以使List1 保持分类.

我编写我的代码,但运行不佳,输出显示错误,因为对进行排序很重要

 public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
 {
        int i;
        int n=list1.size();
        int pos , j=0;

        for (pos =0 ;pos<n ; pos++)
        {
            for ( i=0 ; i<n ; i++)
                if (list1.get(j)>list2.get(pos))
                    list1.add(pos,list2.get(pos));
                else 
                    j++;
       } 
 }

推荐答案

您只需要一个for循环,假设两个列表都已排序:

public static void merge(List<Integer> l1, List<Integer> l2) {
    for (int index1 = 0, index2 = 0; index2 < l2.size(); index1++) {
        if (index1 == l1.size() || l1.get(index1) > l2.get(index2)) {
            l1.add(index1, l2.get(index2++));
        }
    }
}  

如果未排序l2,则需要两个循环:

public static void merge(List<Integer> l1, List<Integer> l2) {
    for (int index2 = 0; index2 < l2.size(); index2++) {
        for (int index1 = 0; ; index1++) {
            if (index1 == l1.size() || l1.get(index1) > l2.get(index2)) {
                l1.add(index1, l2.get(index2));
                break;
            }
        }
    }
}

其他推荐答案

简单修复:以后排序.

list1.addAll(list2);
Collections.sort(list1);

使用集合避免重复.

其他推荐答案

public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
{
    list1.addAll(list2);
    Collections.sort(list1);
}

本文地址:https://www.itbaoku.cn/post/978363.html

问题描述

In my assignment the third step is to Call the method merge to merge the two lists in list1 so that the list1 remains sorted.

I write my code but it doesn't work well , the output show wrong because it important to be sorted

 public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
 {
        int i;
        int n=list1.size();
        int pos , j=0;

        for (pos =0 ;pos<n ; pos++)
        {
            for ( i=0 ; i<n ; i++)
                if (list1.get(j)>list2.get(pos))
                    list1.add(pos,list2.get(pos));
                else 
                    j++;
       } 
 }

推荐答案

You only need one for loop assuming both lists are sorted:

public static void merge(List<Integer> l1, List<Integer> l2) {
    for (int index1 = 0, index2 = 0; index2 < l2.size(); index1++) {
        if (index1 == l1.size() || l1.get(index1) > l2.get(index2)) {
            l1.add(index1, l2.get(index2++));
        }
    }
}  

If l2 isn't sorted, you need two loops:

public static void merge(List<Integer> l1, List<Integer> l2) {
    for (int index2 = 0; index2 < l2.size(); index2++) {
        for (int index1 = 0; ; index1++) {
            if (index1 == l1.size() || l1.get(index1) > l2.get(index2)) {
                l1.add(index1, l2.get(index2));
                break;
            }
        }
    }
}

其他推荐答案

Easy fix: sort afterwards.

list1.addAll(list2);
Collections.sort(list1);

Use sets to avoid duplicates.

其他推荐答案

public static void merge (ArrayList<Integer> list1, ArrayList<Integer> list2)
{
    list1.addAll(list2);
    Collections.sort(list1);
}