简体   繁体   English

JAVA将2D数组的各个维度作为1D数组传递

[英]JAVA Pass individual dimensions of a 2D array as 1D arrays

I'm trying to check if each row of an array, and then each column contains duplicate values (latin rectangle check). 我正在尝试检查数组的每一行,然后每一列是否包含重复值(拉丁矩形检查)。 I made a method that checks a 1D array, but I can't figure out how to pass a part of a 2D array into it. 我制作了一种检查1D数组的方法,但无法弄清楚如何将2D数组的一部分传递到其中。 Here's my 1D array checking method: 这是我的一维数组检查方法:

public static boolean hasDuplicates(int [] inArray)
   {
      for(int i = 0; i < inArray.length; i++)
         for(int j = i + 1; j < inArray.length; j++)
            if(inArray[i] == inArray[j])
               return true;
      return false; 
   }

Here's my mess of a method that calls my previous method: 这是我调用先前方法的方法的混乱之处:

   public static boolean arrayChecker(int[][] inArray)
   {
      int [] splitRows = new int[inArray.length];
      int [] splitCols = new int[inArray[0].length];

      for(int row = 0; row < inArray.length; row++)   
      {
         for(int col = 0; col < inArray[0].length; col++)
            splitRows[col] = inArray[row][col];

         if(hasDuplicates(splitRows))
               return true;
      }

      for(int col = 0; col < inArray[0].length; col++)  
      {
         for(int row = 0; row < inArray.length; row++)
            splitCols[row] = inArray[row][col];

         if(hasDuplicates(splitCols))
               return true;
      }


      return false;
   }

I know the problem with it right now is that my splitRows and splitCols arrays don't reset values after each loop, so when it runs again it will check new values vs the old ones. 我现在知道它的问题是我的splitRows和splitCols数组在每个循环后都不会重置值,因此当它再次运行时,它将检查新值与旧值。 I can't seem to find a way around it. 我似乎找不到解决方法。 I tried setting the arrays = null before the loop, but that clears out the size and screws it up even more. 我尝试在循环之前将arrays设置为null,但这会清除大小并将其拧紧更多。

Any help is appreciated. 任何帮助表示赞赏。

EDIT: Here's my call in my main method: 编辑:这是我主要方法中的调用:

      if (arrayChecker(originalArray))
         System.out.println("Invalid");
      else
         System.out.println("Valid");
hasDuplicates(inArray[row])

将传递1d数组给hasDuplicates

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

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