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

时间:2018-05-11 22:40:44

标签: java arrays int

我试图比较两个int数组,其中数组1是标准(1 ... n),数组2是(1 ... n)范围内的随机数。需要打印出阵列2中缺少的数字。到目前为止,我已经管理了以下内容,但我似乎无法弄清楚如何使用布尔数组来为我带来好处。

.sort

目前,Leaves()正在打印出来:

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相对较新,并且无法通过谷歌搜索或解决我的问题。提前谢谢!

编辑:

当我将Leaves更新为:

0,1,2,3,4,5,6,7,8,

我收到错误:

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

2 个答案:

答案 0 :(得分:2)

首先,你在这里有终止点for (int i = 0; i < head.length; i++);,所以你甚至都没有运行这个循环。

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

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

使用Java 8 +:

import java.util.stream.IntStream;

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 (IntStream.of(m.original).noneMatch(value -> value == i))
                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

答案 1 :(得分:0)

更改此代码

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