2016-11-24 43 views
0

我在Netbeans的jForm/GUI中做一个Lotto应用程序,3行5个数字,我不希望重复项被允许每一行。要在第1行上有一个数字,并且在第3行上有相同的数字,但是要将这些数字放在同一行上并不正确。Java(尤其是Netbeans和JForms):检查二维数组中每行的重复项

我能想到做到这一点的唯一途径就是对其进行硬编码,最好不要这样做。

我曾尝试:

boolean dup = false; 
    for (int k = 0; k < num[0].length){ //loop through columns 
    for (i = 0; i < num.length-1; i++) { 
     for (int j = i; j < inArray.length; j++){ 
      if (num[k][i] == num[k][j]){ 
      dup = true; 
      break; 
      } 
     } 
     } 
    } 

这:

public static boolean hasDuplicates(int [][] num) { 
     for (int row = 0; row < num.length; row++) { 
      int curRow = num[row]; 
      Set set = Sets.newHashSet(Arrays.asList(curRow)); 
      if (set.size() < curRow.length) { 
       return true; 
      } 
     } 
     return false; 
    } 

我也看了其它广泛的编码,我不能让一个工程。

我试图做的确切的事情是:

获得用户对通过文本字段三行乐透的输入,检查各行重复,打印到JLabel如果它是一个重复的或离开的JLabel空白并在没有重复的情况下运行其余的代码。

当前的代码我已经是:

 private void playBtnActionPerformed(java.awt.event.ActionEvent evt) {           

    num[0][0] = Integer.parseInt(line00Tf.getText()); 
    num[0][1] = Integer.parseInt(line01Tf.getText()); 
    num[0][2] = Integer.parseInt(line02Tf.getText()); 
    num[0][3] = Integer.parseInt(line03Tf.getText()); 
    num[0][4] = Integer.parseInt(line04Tf.getText()); 
    num[1][0] = Integer.parseInt(line10Tf.getText()); 
    num[1][1] = Integer.parseInt(line11Tf.getText()); 
    num[1][2] = Integer.parseInt(line12Tf.getText()); 
    num[1][3] = Integer.parseInt(line13Tf.getText()); 
    num[1][4] = Integer.parseInt(line14Tf.getText()); 
    num[2][0] = Integer.parseInt(line20Tf.getText()); 
    num[2][1] = Integer.parseInt(line21Tf.getText()); 
    num[2][2] = Integer.parseInt(line22Tf.getText()); 
    num[2][3] = Integer.parseInt(line23Tf.getText()); 
    num[2][4] = Integer.parseInt(line24Tf.getText()); 

     duplicateLbl.setText(""); 
     LottoPhase1 p1 = new LottoPhase1(); 
     p1.setNum(num); 
     p1.createSecret(); 
     secret = p1.getSecret(); 
     p1.computeCheckInput(); 
     correctL1 = p1.getCorrectL1(); 
     correctL2 = p1.getCorrectL2(); 
     correctL3 = p1.getCorrectL3(); 

     //prints secret to output 
     System.out.println("Phase 1 Main Secret: " + Arrays.toString(secret)); 
     System.out.println(); 


     displayResults0Lbl.setText(Integer.toString(secret[0]) + ", " + Integer.toString(secret[1]) + ", " + Integer.toString(secret[2]) + ", " + Integer.toString(secret[3]) + ", " + Integer.toString(secret[4])); 

     matched1NumLbl.setText(Integer.toString(correctL1)); 
     matched2NumLbl.setText(Integer.toString(correctL2)); 
     matched3NumLbl.setText(Integer.toString(correctL3)); 
    } 

回答

0
public static boolean hasDuplicates(int[][] num) 
{ 
    boolean hasDuplicate = false; 
    // for each line in num 
    for(int[] line : num) 
    { 
     // for every number in the row 
     for(int i = 0; i < line.length && !hasDuplicate; i++) 
     { 
      // for every number in the row 
      for(int j = 0; j < line.length; j++) 
      { 
       // if we are not comparing the same number 
       if(i != j) 
       { 
        // check for equality 
        if(line[i] == line[j]) 
        { 
         hasDuplicate = true; // we have found a duplicate 
         break; // no need to keep checking; break the loop and return 
        } 
       } 
      } 
     } 
    } 
    return hasDuplicate; 
} 
+0

完美工作,谢谢。我曾尝试过其他人,他们没有工作,只有这个人工作。谢谢,非常感谢。 –

0

第二种方法有几个错误,例如,

int curRow = num[row]; 

实际上应该是:

int[] curRow = num[row]; 

而且,你似乎在使用集合,这可能来自某些li你正在使用(Guava,Google Common等)。你没有使用任何库假设,你可以更改您的代码类似于:

public static boolean hasDuplicates(int [][] num) { 
    for (int[] curRow : num) { 
     Set<Integer> set = new HashSet<>(); 
     for (int n : curRow) { 
      if (!set.add(n)) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

如果您使用的是Java 8中,一个方法来消除第二个for循环是通过使用Stream

public static boolean hasDuplicates(int [][] num) { 
    for (int[] curRow : num) { 
     Set<Integer> set = IntStream.of(curRow).boxed().collect(Collectors.toSet()); 
     if (set.size() < curRow.length) { 
      return true; 
     } 
    } 
    return false; 
} 

Stream的其他替代方法可以在these这样的线程中找到。 测试用下面的输入产生什么,我认为你会期望:

int[][] testA = {{0,1,2,3,4}, {0,1,2,3,4}, {0,1,2,3,4}}; //false 
int[][] testB = {{0,1,2,3,4}, {0,2,2,3,4}, {0,1,2,3,4}}; //true 
int[][] testC = {{0,1,2,3,4}, {0,1,2,3,4}, {0,4,3,3,4}}; //true 
int[][] testD = {{0,1,2,3,4}, {5,6,7,8,9}, {10,11,12,13,14}}; //false