简体   繁体   English

Java-多维数组如何测试数组的所有唯一值

[英]Java - Multidimensional Arrays How to test an array for all unique values

I'm trying to test a multidimensional array to see if contains any duplicate values. 我正在尝试测试多维数组,以查看是否包含任何重复的值。 If it does I would like the method to return false. 如果可以,我希望该方法返回false。 Otherwise I would like it to return true. 否则,我希望它返回true。

Here is my current code. 这是我当前的代码。 Where is my logic wrong? 我的逻辑错在哪里?

public static boolean isUnique(int[][] array2, int num) {

    for (int i = 0; i < array2.length - 1; i++) {
        for (int j = i + 1; j < array2.length; j++) {
            if (array2[i] == array2[j]) {
                return false;
            }
        }
    }
    return true;
}

Your current implementation is checking whether two rows are the same (this is a reference based check, rather than a value.) This means that it asks 'Are array2[i] and array2[j] the same address in memory, rather than do they contain the same things. 您当前的实现是检查两行是否相同(这是基于引用的检查,而不是值。)这意味着它会问' array2[i]array2[j]是否在内存中是相同的地址,而不是它们包含相同的内容。

If you want to see whether the rows are unique you'd use array2[i].equals(array2[j]) instead of array2[i] == array2[j] . 如果要查看行是否唯一,可以使用array2[i].equals(array2[j])而不是array2[i] == array2[j]

If you wanted to check for unique elements ( array[i][j] != array2[i+m][j+n] where !(m == n == 0)) you'd need to iterate through both levels in a 如果要检查唯一元素( array[i][j] != array2[i+m][j+n]其中!(m == n == 0)),则需要遍历两个级别在一个

for (int i = 0; i < array2.length; i++) {
    for (int j = 0; j < array2[i].length; j++) {
        // compare array2[i][j] to all other array2[m][n] here.
    }
}

Right now your code is checking whether any of the arrays inside array2 are the same. 现在,您的代码正在检查array2内部的任何数组是否相同。 array2[i] and array2[j] are both referring to arrays, because array2 is an array of arrays. array2[i]array2[j]都引用数组,因为array2是数组的数组。

Instead, you want to look at the values inside of each of those arrays. 相反,您想查看每个数组内部的值。 Since you want to fail on any repeated value anywhere in the grid, you're effectively trying to flatten the structure into one collection, and check for duplicates in that. 由于您想使网格中任何地方的任何重复值都失败,因此您实际上是在尝试将结构展平为一个集合,并检查其中是否有重复项。

A HashSet is the best data structure to use in this case. 在这种情况下, HashSet是最好的数据结构。 Traverse the entire grid, row-by-row, adding values into your new struture. 逐行遍历整个网格,将值添加到新结构中。 If you encounter a duplicate, return false : 如果遇到重复,则返回false

public static boolean isUnique(int[][] array2) {
    Set<Integer> values = new HashSet<>();

    for (int i = 0; i < array2.length; i++) {
        for (int j = 0; j < array2[i].length; j++) {
            if (!values.add(array2[i][j])) {
                return false;
            }
        }
    }

    return true;
}

Some things to note here: 这里要注意一些事情:

  • The set's add method will return false if you attempt to add a duplicate to the collection, so that's wrapped in an if statement for a simple, fail-fast stopping condition. 如果您尝试向集合中添加重复项,则该集合的add方法将返回false,因此将其包装在if语句中,以实现简单的快速失败停止条件。

  • The sizes of each of the inner arrays are completely independent from the size of the outer array, so you still want to loop from 0 to the length of the array (when you're using < , you don't need the length - 1 ). 每个内部数组的大小完全独立于外部数组的大小,因此,您仍然希望从0循环到数组的长度(使用< ,不需要length - 1 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM