猿问

同一行没有重复数字的多维数组

  int matrice2 [][] = new int [5][5];

  for (int i=0;i<5;i++) {

      System.out.println(" ");

      for(int j=0;j<5;j++) {

          matrice2[i][j] = (int)(Math.random()*10);

          System.out.print(" "+matrice2[i][j]+" ");

      }

  }

  System.out.println(" ");

  System.out.println(" ");

  for (int i=0;i<5;i++) {

      System.out.println(" ");

      for(int j=0;j<5;j++) {

          for (int k=0;k<5;k++) {

              if(j!=k) {

                  if (matrice2[i][j]==matrice2[i][k]) {

                      matrice2[i][k]=(int)(Math.random()*10);

                  }

              }

          }

          System.out.print(" "+matrice2[i][j]+" ");

      }

  }

我想制作一个多维数组,而在同一行中没有任何重复的数字,因此该列是否有重复的数字并不重要。


我在这里所做的是生成方形的 5x5 数组。将索引“j”作为与索引“k”中的数字进行比较的索引,如果存在与索引“j”中包含的数字相等的数字,则检查整行。所以我唯一的问题是,在检测到“k”中的数字等于“j”中的数字后,索引“k”中的数字将生成一个新数字来替换“k”中的当前数字,但结果在控制台显示原始 5x5 数组(在更改之前是数组的第一个形式)已更改,但已替换的数字生成的数字已经存在,即使它应该继续更改,直到“k”无法检测到任何重复的数字。


我可以想到其他方法,但我真的很想知道为什么它不起作用,尽管我真的认为它不应该带来任何问题,但我可能错过了一些东西。


这是不应该发生的结果示例。



9 1 3 8 4

5 3 2 4 8

9 8 5 6 5

6 3 0 8 7

2 8 6 3 9



9 1 3 8 4

5 3 2 4 8

9 8 5 6 9

6 3 0 8 7

2 8 6 3 9


它不应该发生,因为“k”应该从索引 0 开始到 4 看到它。


虽然“j”在索引 4 中,而“k”在索引 0 中,它应该立即检测到它并将“k”中的数字更改为其他随机数。


编辑:我看到你们展示的不同方式,但我要求的是在不使用任何导入的情况下提供更好的解决方案。我们的老师给了我们这个作业,告诉我们没有使用任何这些导入,这使得它变得更加复杂,但这正是我们所要求的。我尝试了一段时间,但仍然没有发生任何变化。


江户川乱折腾
浏览 127回答 3
3回答

胡子哥哥

我以不同的方式编辑了您的代码。我写了一些注释来清楚地理解代码。请尝试一下。public class UniqueMatrix {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int matrix[][] = new int[5][5];&nbsp; &nbsp; &nbsp; &nbsp; boolean uniqeMatrixFound = false;&nbsp; &nbsp; &nbsp; &nbsp; while (!uniqeMatrixFound) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //fill matrix until uniqe matrix found value is true&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fillMatrix(matrix);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < matrix.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashSet<Integer> columnNumber = new HashSet<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < matrix.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnNumber.add(matrix[j][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if set size not equal to matrix size , create an new uniqe matrix with breaking false value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (columnNumber.size() != matrix.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uniqeMatrixFound = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uniqeMatrixFound = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //print an array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < matrix.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < matrix.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" " + matrix[i][j] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //create a matrix with unique value in all rows.&nbsp; &nbsp; private static void fillMatrix(int[][] matrice2) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> list = new ArrayList<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < matrice2.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collections.shuffle(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < matrice2.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrice2[i][j] = list.get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

弑天下

我阅读您的逻辑的方式是,当您找到重复项时,您会生成一个新数字,并且该新数字将在外 (&nbsp;j) 循环的下一次迭代中进行验证。问题是j==k因为该数字不会被验证,通常这不是问题,因为j会增加然后该数字将被验证,j==4因为那是最后一次迭代的时间除外。因此,修改最右边的列并且不会检查该值,因为 'j==k' 永远不会为假。

当年话下

这是解决问题的不同方法,它使用混洗的 ArrayList 而不是检查当前行中是否存在值。int matrice2[][] = new int[5][5];ArrayList<Integer> sourceMatrix = new ArrayList<Integer>();for (int i = 0; i < 10; i++)&nbsp; &nbsp; sourceMatrix.add(i);//generate random matrix using shuffled arraylistfor (int i = 0; i < matrice2.length; i++) {&nbsp; &nbsp; Collections.shuffle(sourceMatrix);&nbsp; &nbsp; for (int j = 0; j < matrice2[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; matrice2[i][j] = sourceMatrix.get(j);&nbsp; &nbsp; }}//print generated matrixfor (int i = 0; i < matrice2.length; i++) {&nbsp; &nbsp; for (int j = 0; j < matrice2[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(matrice2[i][j]);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}
随时随地看视频慕课网APP

相关分类

Java
我要回答