将两个数组列表合并为一个新数组,并对其进行排序(请参见示例)

时间:2019-01-31 16:08:26

标签: java arraylist merge

我已经尝试解决了将近三个小时的练习,但我仍然不明白自己在做什么错。我应该带两个带有数字的ArrayList并将它们合并为一个,但是要注意的是,它们必须像这样排序:

如果arraylist“ A”具有数字[1、2、3],而arraylist“ B”具有[9、8、7、6、5],则ArrayList“ merge”应该为[1、9、2、8 ,3,7,6,5]。

即,它应该交替输入数组列表A和B中的数字。如果一个数组列表较长,则应继续填充数字(例如[7,6,5]的情况)。 另外,我们不知道任何arrayLists的长度。

这是我认为应该很好用的一种解决方案,但我无法使其正常工作。 非常感谢所有帮助!

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;
    }
}

3 个答案:

答案 0 :(得分:0)

这是您的固定代码。主要是将所有<=更改为<的情况。如果您比较差异,您将能够看到哪里出了问题:-

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;
    }
}

答案 1 :(得分:0)

我已经纠正了您的合并功能,您只需要在循环中使用<运算符即可代替<=

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;
}

答案 2 :(得分:0)

其他人已经回答了这个问题,只是提供了一种可能更易于阅读的替代实现。

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));
        }
    }
}
相关问题