java代码-合并数组和排序

java代码-合并数组和排序,java,arrays,Java,Arrays,作业提醒 创建一个合并两个非负(等于或大于0)整数数组的程序 接受每个数组作为键盘的输入 您事先不知道将输入多少个值,但可以假定每个数组的最大长度为10000个元素 要停止输入值,请输入负数 您可以忽略输入的任何负数,而不将其存储在数组中 两个输入数组的元素应按递增顺序排列。 换句话说,每个数组元素的值必须大于或等于前一个元素的值 数组可能包含重复的元素 输入两个数组后,程序必须检查以确保每个数组的元素都按顺序输入 如果发现无序元素,请打印消息“错误:数组顺序不正确” 您的任务是将两个输入数

作业提醒
创建一个合并两个非负(等于或大于0)整数数组的程序

  • 接受每个数组作为键盘的输入
  • 您事先不知道将输入多少个值,但可以假定每个数组的最大长度为10000个元素
  • 要停止输入值,请输入负数
  • 您可以忽略输入的任何负数,而不将其存储在数组中
  • 两个输入数组的元素应按递增顺序排列。 换句话说,每个数组元素的值必须大于或等于前一个元素的值
  • 数组可能包含重复的元素
  • 输入两个数组后,程序必须检查以确保每个数组的元素都按顺序输入
  • 如果发现无序元素,请打印消息“错误:数组顺序不正确”
您的任务是将两个输入数组合并到一个新数组中,所有元素按从低到高的顺序排列

打印输入的每个原始数组,然后打印合并的数组。请注意,您的程序输出的数组中,每个数字之间必须正好有一个空格

Sample Run 1:
Enter the values for the first array, up to 10000 values, enter a negative number to quit 3 3 5 6 8 9 -1
Enter the values for the second array, up to 10000 values, enter a negative number to quit 3 4 5 6 -5
First Array: 3 3 5 6 8 9
Second Array: 3 4 5 6
Merged Array: 3 3 3 4 5 5 6 6 8 9

Sample Run 2: Enter the values for the first array, up to 10000 values, enter a negative number to quit 4 5 7 2 -1
Enter the values for the second array, up to 10000 values, enter a negative number to quit 3 3 3 3 3 3 -100
First Array: 4 5 7 2
Second Array: 3 3 3 3 3 3
ERROR: Array not in correct order
这是我的密码:

import java.util.Scanner;
import java.lang.String;
class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int a [] = new int[10000];
        int b [] = new int[10000];
        System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
        int count = 0;
        int count2 = 0;
        int i = 0;
        int c = 0;
        int print = 0;
        int print2 = 0;
        int flag = 0;
        while(count >= 0) {
            a[i] = scan.nextInt();
            if(a[i] < count)
            {
                flag = 1;
            }
            count = a[i];
            if(a[i] >= 0){
                i++;
                print ++;
            }
        }
        while(count2 >= 0) {
            System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to             quit");
            b[c] = scan.nextInt();
            count2 = b[c];
            if(b[c] >= 0){
                c++;
                print2++;
            }
        }
        System.out.print("First Array: \n");
        for(int j = 0;j < print;j++) {
            System.out.print(+a[j]+" ");
        }
        System.out.print("\n\nSecond Array: \n");
        for(int h = 0;h < print2;h++) {
            System.out.print(+a[h]+" ");
        }
        if(flag == 1) {
            System.out.println("\n\nERROR: Array not in correct order");
        }
        else {
            System.out.println("\n\n Merged Array: ");
            for(int j = 0;j < print;j++) {
                System.out.print(+a[j]+" ");
            }
            for(int h = 0;h < print2;h++) {
                System.out.print(+a[h]+" ");
            }
        }
    }
}
import java.util.Scanner;
导入java.lang.String;
班长{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
整数a[]=新整数[10000];
整数b[]=新整数[10000];
System.out.println(“输入第一个数组的值,最多10000个值,输入一个负数退出”);
整数计数=0;
int count2=0;
int i=0;
int c=0;
int print=0;
int print2=0;
int标志=0;
而(计数>=0){
a[i]=scan.nextInt();
if(a[i]<计数)
{
flag=1;
}
计数=a[i];
如果(a[i]>=0){
i++;
打印++;
}
}
而(count2>=0){
System.out.println(“输入第二个数组的值,最多10000个值,输入一个负数退出”);
b[c]=scan.nextInt();
count2=b[c];
如果(b[c]>=0){
C++;
print2++;
}
}
System.out.print(“第一个数组:\n”);
对于(int j=0;j
我主要不明白的是

  • 如何在输入负数后停止代码,然后阻止它进入数组

  • 另外,如何对数组进行排序,然后合并已排序的数组


举例真的会很有帮助,我已经压力过大了

要回答您的具体问题:

  • 当输入(删除负数后)为空时,停止程序
  • 使用流在一行中合并、排序和格式化
要实现,您不需要使用数组。由于只能指定行为(而不是实施),因此可按如下方式实施:

  • 创建一个
    列表
    以保存所有输入
  • 从输入中为每个字符串读取两个字符串
    • 按空格分割
    • 将每个标记解析为整数
    • 忽略否定
    • 要求每个数字不少于前一个数字
    • 将每个数字添加到列表中
  • 对列表排序
  • 打印列表
这一切都可以在几行代码中实现

“以负数结束输入”的要求是不必要的-按Enter键就足够了(如果您真的想测试它,您可以自己编写代码)

publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
重复:
while(true){
System.out.format(“输入第一个数组”);
List first=readList();
if(first.isEmpty())返回;
List second=readList();
if(second.isEmpty())返回;
如果(!isSorted(第一)| |!isSorted(第二))继续重复;
System.out.println(“第一个数组:+First.stream().collect(collector.joining(“”));
System.out.println(“第二个数组:+Second.stream().collect(collector.joining(“”));
System.out.println(“合并数组”+流(第一,第二)
.flatMap(列表::流)
收集(收集者。加入(“”));
}
}
专用静态列表读取列表(扫描仪输入){
返回Arrays.stream(在.nextLine().split(“+”)中)
.map(整数::parseInt)
.filter(n->n>=0)
.collect(Collectors.toList());
}
私有静态布尔isSorted(列表){
if(list.equals(list.stream().sorted().collect(Collectors.toList()))返回true;
System.out.println(“错误:数组顺序不正确”);
返回false;
}
免责声明:在iPhone上翻阅的代码-未编译或测试


因为这是勺子喂养,我会让你知道这是如何工作之前,你考虑提交它作为自己的工作。< /p> @阿里看到编辑的答案,一些代码应该做到这一点。

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    repeat:
    while (true) {
        System.out.format("Enter the first array");
        List<Integer> first = readList();
        if (first.isEmpty()) return;
        List<Integer> second = readList();
        if (second.isEmpty()) return;
        if (!isSorted(first) || !isSorted(second)) continue repeat;
        System.out.println("First array: " + first.stream().collect(Collectors.joining(" ")));
        System.out.println("Second array: " + second.stream().collect(Collectors.joining(" ")));
        System.out.println("Merged array " + Stream.of(first, second)
            .flatMap(List::stream)
            collect(Collectors.joining(" ")));
    }
}

private static List<Input> readList(Scanner in) {
    return Arrays.stream(in.nextLine().split(" +"))
        .map(Integer::parseInt)
        .filter(n -> n >= 0)
        .collect(Collectors.toList());
}

private static boolean isSorted(List<Integer> list) {
    if (list.equals(list.stream().sorted().collect(Collectors.toList())) return true;
    System.out.println("ERROR: Array not in correct order");
    return false;
}