【问题标题】:Compare two int arrays and print the missing numbers比较两个 int 数组并打印缺失的数字
【发布时间】:2018-05-11 22:40:44
【问题描述】:

我正在尝试比较两个 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();
    }
}

目前,Leaves() 只是打印出来:

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

有没有人知道可以做到这一点的方法?我对 Java 比较陌生,并且无法在谷歌上找到任何可以解决我问题的东西。提前致谢!

编辑:

当我将 Leaves 更新为:

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

我收到以下错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at findLeaves.Leaves(findLeaves.java:28)
at findLeaves.main(findLeaves.java:39)

【问题讨论】:

    标签: java arrays int


    【解决方案1】:

    首先,这里有终止点for (int i = 0; i &lt; head.length; 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);
            }
        }
    }
    

    使用 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
    

    【讨论】:

    • 谢谢你!您的第一个示例解决了我的问题,尽管出于我的目的,您的原始数组和随机数组被反转了,但没问题。 :)
    【解决方案2】:

    更改此代码

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

    【讨论】:

    • 不幸的是,这只导致了额外的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2016-05-07
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多