Java 比较两个整数数组并打印缺少的数字

Java 比较两个整数数组并打印缺少的数字,java,arrays,int,Java,Arrays,Int,我试图比较两个int数组,其中数组1是标准的(1…n),数组2是(1…n)范围内的随机数。数组2中缺少的数字需要打印出来。到目前为止,我已经做到了以下几点,但我似乎不知道如何使用布尔数组来为我带来好处 import java.util.Scanner; public class findLeaves { private boolean[] id; private String[] surface; private int[] head; public bool

我试图比较两个int数组,其中数组1是标准的(1…n),数组2是(1…n)范围内的随机数。数组2中缺少的数字需要打印出来。到目前为止,我已经做到了以下几点,但我似乎不知道如何使用布尔数组来为我带来好处

import java.util.Scanner;

public class findLeaves {
    private boolean[] id;
    private String[] surface;
    private int[] head;

    public boolean[] inputId() {
        System.out.println("Input number of IDs");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        id = new boolean[n];
        return this.id;
    }

    public int[] inputHead() {
        System.out.println("Input each head value.");
        head = new int[id.length];
        for (int i = 0; i < id.length; i++){
            Scanner scan = new Scanner(System.in);
            head[i] = scan.nextInt();
        }
        return this.head;
    }

    public boolean Leaves() {

        for (int i = 0; i < head.length; i++);
        for (int j = 0; j < id.length; j++) {
            if (!id[j]) System.out.print(j + ",");
        }
        return true;
    }

    public static void main(String[] args) {
        findLeaves x = new findLeaves();
        x.inputId();
        x.inputHead();
        x.Leaves();
    }
}
有人知道实现这一目标的方法吗?我对Java比较陌生,在谷歌上或这里都找不到任何解决我问题的方法。提前谢谢

编辑:

当我将叶子更新为:

public boolean Leaves() {

    for (int i = 0; i < head.length; i++) id[head[i]] = true;
    for (int j = 0; j < id.length; j++) {
        if (!id[j]) System.out.print(j + ",");
    }
    return true;
}

首先,这里有终止点
for(inti=0;i,因此您甚至不会运行此循环

这是从
random
中的
original
数组中查找缺失值的较短方法:

import java.util.Arrays;

public class Main {
    private int[] original = new int[] {1, 3, 5, 7, 9}; // original array
    private int[] random = new int[] {1, 2, 3, 4, 5, 6}; // missing value from original array will be printed

    public static void main(String[] args) {
        Main m = new Main();
        Arrays.sort(m.original); // sort is required for binarySearch()
        for (int i : m.random) {
            if (Arrays.binarySearch(m.original, i) < 0)
                System.out.println(i);
        }
    }
}
没有任何图书馆:

public class Main {
    private int[] original = new int[] {1, 3, 5, 7, 9};
    private int[] random = new int[] {1, 2, 3, 4, 5, 6};

    public static void main(String[] args) {
        Main m = new Main();
        for (int i : m.random) {
            if (!contains(m.original, i))
                System.out.println(i);
        }
    }

    public static boolean contains(int[] array, int value) {
        for (int i : array)
            if (i == value)
                return true;

        return false;
    }
}
输出:

2
4
6
更改此代码

public boolean Leaves() {

    for (int i = 0; i < head.length; i++) id[head[i] -1] = true;
    for (int j = 0; j < id.length; j++) {
        if (!id[j]) System.out.print((j +1)+ ",");
    }
    return true;
}
公共布尔叶(){ 对于(inti=0;i
谢谢您!您的第一个示例解决了我的问题,尽管出于我的目的,您的原始数组和随机数组被反转,但没有问题。:)不幸的是,这只会导致额外的错误
public class Main {
    private int[] original = new int[] {1, 3, 5, 7, 9};
    private int[] random = new int[] {1, 2, 3, 4, 5, 6};

    public static void main(String[] args) {
        Main m = new Main();
        for (int i : m.random) {
            if (!contains(m.original, i))
                System.out.println(i);
        }
    }

    public static boolean contains(int[] array, int value) {
        for (int i : array)
            if (i == value)
                return true;

        return false;
    }
}
2
4
6
public boolean Leaves() {

    for (int i = 0; i < head.length; i++) id[head[i] -1] = true;
    for (int j = 0; j < id.length; j++) {
        if (!id[j]) System.out.print((j +1)+ ",");
    }
    return true;
}