简体   繁体   English

将2个具有相同或不同长度的不同整数数组合并为一个大数组,并将它们从最小到最大排序

[英]Merging 2 different integer arrays with same or different lengths into one big array and sorting them from least to greatest

In one of my programs I am having trouble merging the two different lists and put it into an array and sorted from least to greatest. 在我的一个程序中,我很难将两个不同的列表合并到一个数组中,并且从最小到最大排序。 One of the teacher assistants said I can hard code it in to make it easier or do it the better way, the hard way of doing it. 一位助教说,我可以对其进行硬编码,以使其变得更容易或以更好的方式(困难的方式)进行编码。 I want to do it the better way so I can think/become better of coding in. The TAs said it was doable in one for loop. 我想以更好的方式进行操作,以便可以更好地进行编码。TA表示它可以在一个for循环中使用。 Been thinking and trying for about 5 hours now and give up. 已经思考和尝试了大约5个小时,现在就放弃了。 Any suggestions? 有什么建议么?

import java.util.Scanner;

public class Lab10Part1 {

    public static void main(String[] args) {
        int[] list1, list2;

        Scanner input = new Scanner(System.in);
        System.out.print("Enter list1 size and contents: ");
        int len1 = input.nextInt();
        list1 = new int[len1];
        for(int i = 0; i < list1.length; i++) {
            list1[i] = input.nextInt();
        }

        System.out.print("Enter list2 size and contents: ");
        int len2 = input.nextInt();
        list2 = new int[len2];
        for(int i = 0; i < list2.length; i++) {
            list2[i] = input.nextInt();
        }

        input.close();

        System.out.print("List1 is ");
        for(int i = 0; i < list1.length; i++) {
            System.out.print(list1[i] + " ");
        }
        System.out.println();
        System.out.print("List2 is ");
        for(int i = 0; i < list2.length; i++) {
            System.out.print(list2[i] + " ");
        }

        System.out.println();
        System.out.print("The merged list is ");
        for(int i = 0; i < merge(list1, list2).length; i++) {
        System.out.print(merge(list1, list2)[i] + " ");
        }

    }

    public static int[] merge(int[] list1, int[] list2) {
        int[] merge = new int[list1.length + list2.length];
        int min = Math.min(list1.length, list2.length);

        for(int i = 0, j = 0, k = 0; i < merge.length; k++) {
            if (min <= list1.length || min <= list2.length) {
                if(list2[j] <= list1[i]) {
                    merge[k] = list2[j];
                    j++;
                }
                else {
                    merge[k] = list1[i];
                    i++;
                }
                min++;
            }


            else if(list2.length >= list1.length) {
                if(list1[i] <= list2[j]) {
                    merge[k] = list1[i];
                    i++;
                }
                else {
                    merge[k] = list2[j];
                    j++;
                }
            }


        }
        return merge;
    }

}

我认为您可以通过使用collections(只是List)来解决问题.Array to List的方法是Arrays.asList(array),然后使用add和sort方法。您可以参考Java Collections api来获得更多信息。我因为我的英语不好。

Here is a way to merge sorted lists, and maintain the order in one for loop: 这是合并排序列表并在for循环中保持顺序的一种方法:

public class MergeSort {

public static void main(String[] args) {
    int[] a1 = { 1, 5, 6 };
    int[] a2 = { 2, 4, 6, 9, 11 };
    int[] a3 = merge(a1,a2);
    for (int i = 0; i<a3.length; i++)
        System.out.printf("%d ", a3[i]);
    System.out.println();

}
public static int[] merge(int[] list1, int[] list2) {
    int[] merge = new int[list1.length + list2.length];
    int i = 0; // Array index for list1
    int j = 0; // Array index for list2
    for (int k = 0; k < merge.length; k++) {
        int v1 = (i < list1.length) ? list1[i] : Integer.MAX_VALUE;
        int v2 = (j < list2.length) ? list2[j] : Integer.MAX_VALUE;
        if (v1 < v2) {
            merge[k] = v1;
            i++;
        } else {
            merge[k] = v2;
            j++;
        }
    }
    return merge;
}

}

Which creates output: 创建输出:

1 2 4 5 6 6 9 11 

Alternative to ternary operator 三元运算符的替代

//int v1 = (i < list1.length) ? list1[i] : Integer.MAX_VALUE;
int v1;
if (i < list1.length)
    v1 = list1[i];
else 
    v1 = Integer.MAX_VALUE;

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

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