合并数组然后进行排序和打印

时间:2018-01-06 01:40:38

标签: java arrays sorting merge

我能够使用以下代码合并数组并打印它但在使用import java.util.Arrays; import java.io.*; import java.util.Scanner; public class leetcode4Again { public static void main(String args[]) throws IOException { int a[] = new int[50]; int b[] = new int[50]; int merge[] = new int[a.length + b.length]; int count = 0, size1, size2, size; int i = 0, k; Scanner scan = new Scanner(System.in); System.out.print("Enter Array 1 Size : "); size1 = scan.nextInt(); System.out.print("Enter Array 1 Elements : "); for (i = 0; i < size1; i++) { a[i] = scan.nextInt(); } System.out.print("Enter Array 2 Size : "); size2 = scan.nextInt(); System.out.print("Enter Array 2 Elements : "); for (i = 0; i < size2; i++) { b[i] = scan.nextInt(); } System.out.print("Merging the Arrays...\n"); for (i = 0; i < size1; i++) { merge[i] = a[i]; } size = size1 + size2; for (i = 0, k = size1; k < size && i < size2; i++, k++) { merge[k] = b[i]; } Arrays.sort(merge); System.out.print("Now the New Array after Merging is :\n"); for (i = 0; i < size; i++) { System.out.print(merge[i] + " "); } } } 方法对数组进行排序后无法打印数组。

问题出在哪里?

renderer.render(atTime: time, viewport: viewport,
                commandBuffer: commandBuffer,
                passDescriptor: renderPassDescriptor)

4 个答案:

答案 0 :(得分:0)

也许你会得到&#34; 0 0 0 0 ..&#34;回答,因为你首先用零值填充你的合并数组,然后用[]和b []值填充它的一些单元格,你仍然有一些&#34; 0&#34;在那,所以当你排序&#34; 0&#34;会先来,答案是错的。 所以最好先定义合并数组

int merge[];

但不要初始化这个价值; 然后,在获得size1和size2之后,您可以使用正确的大小

来初始化它
size = size1 + size2;
    merge = new int[size];
    System.out.print("Merging the Arrays...\n");
    for (i = 0; i < size1; i++) {
        merge[i] = a[i];
    } 
    // .....

答案 1 :(得分:0)

问题在于为数组merge指定了大小,您可以找到已经回答的类似issue

您可以通过以下方式解决此问题:

public class leetcode4Again {
    public static void main(String args[]) throws IOException {

        int  size1, size2, size;
        int i = 0, k;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter Array 1 Size : ");
        size1 = scan.nextInt();
        int a[] = new int[size1];
        System.out.print("Enter Array 1 Elements : ");
        for (i = 0; i < size1; i++) {
            a[i] = scan.nextInt();
        }

        System.out.print("Enter Array 2 Size : ");
        size2 = scan.nextInt();
        int b[] = new int[size2];
        System.out.print("Enter Array 2 Elements : ");
        for (i = 0; i < size2; i++) {
            b[i] = scan.nextInt();
        }
        int merge[] = new int[a.length + b.length];
        System.out.print("Merging the Arrays...\n");
        for (i = 0; i < size1; i++) {
            merge[i] = a[i];
        }

        size = size1 + size2;

        for (i = 0, k = size1; k < size && i < size2; i++, k++) {
            merge[k] = b[i];
        }

        Arrays.sort(merge);

        System.out.print("Now the New Array after Merging is :\n");
        for (i = 0; i < size; i++) {
            System.out.print(merge[i] + "  ");
        }

    }
}

//...
int merge[] = new int[size1 + size2];
        System.out.print("Merging the Arrays...\n");
        for (i = 0; i < size1; i++) {
            merge[i] = a[i];
        }
//...

答案 2 :(得分:-1)

除非你实际上在第一个数组中填充了50个元素而在第二个数组中填充了50个元素,否则你会遇到一个带有大量空白空间的数组。 Arrays.sort正在对合并数组进行排序,你为它定义了100个元素位置,但实际上,如果你没有填充两个数组,这个合并数组将填充很多零(如果你有java中的defualt) 'nt将数组元素更改为0),并且因为sort从底部开始到顶部,所以当你打印数组时,所有0都将排在第一位。怎么解决这个? 只需要声明合并数组与你需要的确切位数,所以你不会有0,这个数字是size = size1 + size2,就像你在代码中一样。如果事情不明确,请问我。希望它会有所帮助

import java.util.Arrays;
import java.io.*;
import java.util.Scanner;

public class leetcode4Again {
    public static void main(String args[]) throws IOException {
        int a[] = new int[50];
        int b[] = new int[50];

        int count = 0, size1, size2, size;
        int i = 0, k;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter Array 1 Size : ");
        size1 = scan.nextInt();

        System.out.print("Enter Array 1 Elements : ");
        for (i = 0; i < size1; i++) {
            a[i] = scan.nextInt();
        }

        System.out.print("Enter Array 2 Size : ");
        size2 = scan.nextInt();

        System.out.print("Enter Array 2 Elements : ");
        for (i = 0; i < size2; i++) {
            b[i] = scan.nextInt();
        }
        size = size1 + size2; //change
        int merge[] = new int[size]; //change
        System.out.print("Merging the Arrays...\n");
        for (i = 0; i < size1; i++) {
            merge[i] = a[i];
        }

        for (i = 0, k = size1; k < size && i < size2; i++, k++) {
            merge[k] = b[i];
        }
        Arrays.sort(merge);
        System.out.print("Now the New Array after Merging is :\n");
        for (i = 0; i < size; i++) {
            System.out.print(merge[i] + "  ");
        }
    }
}

答案 3 :(得分:-1)

看到这个链接,它正确地解释了如何合并两个数组并打印它们,你需要首先在互联网上搜索这样的问题,有很多例子已经可用 https://www.tutorialspoint.com/javaexamples/arrays_merge.htm你的代码问题是在排序0之后出现在长度为100的数组的开头并且你只打印前几个元素,但是,参考上面的链接来执行这样的任务