Java 查找2D数组中的每个数字是否唯一?

Java 查找2D数组中的每个数字是否唯一?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我想知道如何创建一个方法,确保2d数组中的每个数字都是唯一的,而不是与任何其他数字相同。这应该有点简单,因为我的数字只能是1-9,而且只有9个,但我仍然不明白,我怎么能不做类似的事情就用简单的方式找到答案 if(数组[0][0]==数组[0][1]| |数组[0][0]==数组[0][2]) 以此类推,直到每个数字的数组[0][0]==数组[2][2]。我有一组非常长的9个数字的嵌套if语句,我怀疑我的教授是否打算让我们这样做 那么我如何使用循环来完成我想在这里做的事情呢?我在StackOver

我想知道如何创建一个方法,确保2d数组中的每个数字都是唯一的,而不是与任何其他数字相同。这应该有点简单,因为我的数字只能是1-9,而且只有9个,但我仍然不明白,我怎么能不做类似的事情就用简单的方式找到答案

if(数组[0][0]==数组[0][1]| |数组[0][0]==数组[0][2])

以此类推,直到每个数字的
数组[0][0]==数组[2][2]
。我有一组非常长的9个数字的嵌套if语句,我怀疑我的教授是否打算让我们这样做

那么我如何使用循环来完成我想在这里做的事情呢?我在StackOverflow上搜索了其他java 2d数组唯一算法,但我找不到一个想要用我为分配设置的规则测试每个数字与每个其他数字的算法。请记住,我需要将每个数字与其他数字进行对比测试

以下是我目前的代码:

class MagicSquare
{
   //2d array data member
   private int[][] array = {{}};

   /**
   Constructor: Initializes 2d array.
   @param arr The array.
   */
   public MagicSquare(int[][] arr)
   {
      array = arr;
   }

   /**
   This method displays all values from the elements
   in the array.
   */
   public void showArray()
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
            System.out.println(array[row][col]);
         }
      }
   }

   /**
   showResult for later
   */
   public void showResult()
   {

   }

   /**
   This method determines if every number in the array
   is in the range of 1-9.
   @return The value of range.
   */
   private boolean isInRange()
   {
      boolean range = false;

      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
            if (array[row][col] == 1 || array[row][col] == 2 || array[row][col] == 3 ||
                array[row][col] == 4 || array[row][col] == 5 || array[row][col] == 6 ||
                array[row][col] == 7 || array[row][col] == 8 || array[row][col] == 9)
            {
               range = true;
            }
            else
            {
               range = false;
               break;
            }
         }
      }

      return range;
   }

   /**

   */
   private boolean isUnique()
   {
      boolean unique = false;


   }



}
class MagicSquare
{
//二维数组数据成员
私有int[][]数组={{};
/**
构造函数:初始化2d数组。
@param arr数组。
*/
公共MagicSquare(int[]arr)
{
数组=arr;
}
/**
此方法显示元素中的所有值
在数组中。
*/
公共void showArray()
{
对于(int行=0;行<3;行++)
{
for(int col=0;col<3;col++)
{
System.out.println(数组[行][col]);
}
}
}
/**
稍后显示结果
*/
公开无效显示结果()
{
}
/**
此方法确定数组中的每个数字
在1-9的范围内。
@返回范围的值。
*/
私有布尔值isInRange()
{
布尔范围=假;
对于(int行=0;行<3;行++)
{
for(int col=0;col<3;col++)
{
如果(数组[行][col]==1 | |数组[行][col]==2 | |数组[行][col]==3||
数组[行][col]==4 | |数组[行][col]==5 | |数组[行][col]==6||
数组[行][col]==7 | |数组[行][col]==8 | |数组[行][col]==9)
{
范围=真;
}
其他的
{
范围=假;
打破
}
}
}
返回范围;
}
/**
*/
私有布尔值是唯一的()
{
布尔唯一=假;
}
}

将所有数字放在一个集合中,只需匹配数组和集合的大小即可。如果两者相等,则数组中的所有数字都是唯一的。如果您需要代码演示,请告诉我

编辑1:

试试这个代码

public static void main(String[] args) throws Exception {

    Integer[] numbers = new Integer[] { 2, 3, 1, 7, 4, 6, 5, 11 };

    Set<Integer> numberSet = new HashSet<Integer>(Arrays.asList(numbers));

    if (numbers.length == numberSet.size()) {
        System.out.println("Numbers in array are unique");
    } else {
        System.out.println("Numbers in array are not unique");
    }

// for 2d array

    Set<Integer> number2dSet = new HashSet<Integer>();

    Integer[][] numbers2d = new Integer[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    int numbers2dSize = 0;

    for (Integer[] num2d : numbers2d) {
        List<Integer> numberList = Arrays.asList(num2d);
        number2dSet.addAll(numberList);
        numbers2dSize += numberList.size();
    }

    if (numbers2dSize == number2dSet.size()) {
        System.out.println("Numbers in 2d array are unique");
    } else {
        System.out.println("Numbers in 2d array are not unique");
    }

}
publicstaticvoidmain(字符串[]args)引发异常{
整数[]个数=新整数[]{2,3,1,7,4,6,5,11};
Set numberSet=newhashset(Arrays.asList(numbers));
如果(numbers.length==numberSet.size()){
System.out.println(“数组中的数字是唯一的”);
}否则{
System.out.println(“数组中的数字不是唯一的”);
}
//用于二维阵列
Set number2dSet=newhashset();
整数[][]numbers2d=新整数[][{{1,2,3},{4,5,6},{7,8,9};
int numbers2dSize=0;
for(整数[]num2d:numbers2d){
List numberList=Arrays.asList(num2d);
number2dSet.addAll(numberList);
numbers2dSize+=numberList.size();
}
如果(numbers2dSize==number2dSet.size()){
System.out.println(“二维数组中的数字是唯一的”);
}否则{
System.out.println(“二维数组中的数字不是唯一的”);
}
}

如果保证元素仅在1到arr.length*arr[0].length的范围内,则可以按如下方式执行(适用于任何数组长度):

专用布尔值ISeverylementUnique(int[][]arr){
布尔[]等积=新布尔[arr.length*arr[0]。长度+1];
对于(int i=0;i

您不需要为此构建哈希集。它有不必要的开销。对于这种情况,一个简单的布尔数组就足够了。

使用java-8可以很容易地做到这一点

int a[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int distinctValues = Arrays.stream(a).flatMapToInt(IntStream::of).distinct().count();
对于上面的示例,如果数组中的每个元素都是不同的,那么值应该是9。如果不清楚,则该值将小于9

这将适用于任何n-D阵列。

因为

我的号码只能是1-9

替换这个庞大的代码:

if (array[row][col] == 1 || array[row][col] == 2 || array[row][col] == 3 ||
                array[row][col] == 4 || array[row][col] == 5 || array[row][col] == 6 ||
                array[row][col] == 7 || array[row][col] == 8 || array[row][col] == 9)


if(array[row][col]>=1&&array[row][col]你介意给我一个代码演示吗?我想具体看看你的意思。从我能收集到的信息来看,即使集合等于数组,它也不能确定数组的数字是否唯一,对吗?这难道不能告诉我数组等于集合吗?我不确定这将如何确定所有的数字都是唯一的。Upd我的答案是包含我所说的代码。只需使用数组中的数字并执行代码。我正在测试此代码,代码集numberSet=new HashSet(Arrays.asList(numbers))出现“找不到符号”错误;确保您的文件中有这些导入;导入java.util.array;导入java.util.HashSet;导入java.util.Set;如果数字数组是2d数组,这仍然有效吗?如果是,我会更改什么?使用HashSet,在插入之前只需检查需要插入的元素是否在集合中。包含(x).@suvojit\u 007:集合从不包含重复的值。因此从数组插入之前无需检查。@PushpeshKumarRajwanshi更正,这样我们就可以在插入之前检查重复值是否存在。hashmap也可以实现这一点。如果我错了,请纠正我。@suvojit\u 007:即使在hashmap中,键始终是唯一的,但需要映射对于键值对和少数数字,最好使用一组。我尝试交换
if (array[row][col] == 1 || array[row][col] == 2 || array[row][col] == 3 ||
                array[row][col] == 4 || array[row][col] == 5 || array[row][col] == 6 ||
                array[row][col] == 7 || array[row][col] == 8 || array[row][col] == 9)
if (array[row][col] >= 1 && array[row][col] <= 9)