2016-11-24 62 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