2016-02-14 29 views
0

如果數組中的四個不同數字都相等,則此方法應該返回true。但每當我試圖用4相等數量的運行它,我得到一個錯誤,指出: 如何檢查數組中的4個不同數字是否相等?

異常線程「main」 java.lang.ArrayIndexOutOfBoundsException:5
在Assignment4.containsFourOfaKind(Assignment4.java: 93)
在Assignment4.main(Assignment4.java:16)

public static boolean containsFourOfaKind(int hand[]){ 
    for (int i = 0; i < 5; i++) { 
     if (hand[i ] == hand[i + 1] && 
      hand[i + 1] == hand[i + 2] && 
      hand[i + 2] == hand[i + 3] 
     ) { 
      return true; 
     } 
    } 
    return false; 
} 

我怎樣才能解決這個問題?

+0

您正在從0循環到4.當您到達'i == 2'時,if語句中'i + 3'的值是什麼? –

+0

如果(hand [i]!= hand [i + 1])返回false;當循環終止時,你知道所有的卡必須是相等的,所以你可以返回true。您的循環控制需要進行相應的設置才能正常工作。 – Feek

回答

1

最答案只能解決ArrayIndexOutOfBoundsException,但是它們並沒有解決你的原始代碼沒有被檢測到的某種類型。它試圖檢測四行。想象一下一隻手{3, 0, 3, 3, 3}:即使你的代碼沒有導致ArrayIndexOutOfBoundsException,它仍然會說這不是四種類型,儘管它很明顯。

你需要的代碼實際上是統計了有多少種,然後檢查它是否總共有四個或更多。 (在一個典型的撲克牌套牌中,你不可能有超過4種類型,所以你可以使用==來檢查4)

下面的代碼甚至不知道手牌的數量,雖然從你上面的代碼,它看起來像你的手大小爲5(這是在撲克中非常典型)

public static boolean containsFourOfaKind(int hand[]) { 
    for (int i = 0; i < hand.length; i++) { 
     int countOfKind = 0; 
     for (int j = 0; j < hand.length; j++) { 
      if (hand[i] == hand[j]) { 
       countOfKind++; 
      } 
     } 
     if (countOfKind >= 4) { 
      return true; 
     } 
    } 
    return false; 
} 

(請注意,這是一個本地方法,您可以進一步優化此;例如,如果你在這個仔細看將看到i不必比01更進一步。)

1

當您從(i=0; i<5;...)運行循環時,您正在檢查五個值...在您的if聲明中,您正在查看hand[i] == hand[i+1] && hand[i+1] == hand[i+2] && hand[i+2] == hand[i+3]。這意味着在迭代過程中,當您嘗試訪問hand[4]hand[7]i=4

我懷疑你的陣列hand沒有那麼多元素。

+0

它應該檢查數組中的5個數字。 – heinrichben

+0

這可能是,但你檢查下標7. –

1
public static boolean containsFourOfaKind(int hand[]){ 
    for(int x=0; x < hand.length; x++){ 
    for(int y=0; y < hand.length; y++){ 
     if(y!=x){ 
     if(hand[x]!=hand[y]){ 
      return false; 
     } 
     } 
    } 
    } 
    return true; 
} 

您正在使用循環內的+1進入索引之外。上面的代碼檢查數組中的所有元素是否相同。

+0

這似乎是更多的嵌套循環,那麼需要什麼。 – Feek

-1

在Java8你可以做到這一點很簡單:

private static boolean isEqualElements(int[] arr) { 
    return Arrays.stream(arr).allMatch(value -> arr[0] == value);; 
} 
+0

請在投票時留下評論。謝謝 –

0
//brain compiled code 
public static boolean containsFourOfaKind(int hand[]) 
{ 
    for(int i=0; i < hand.length - 1; i++) 
    { 
     if(hand[i] != hand[i + 1]) 
      return false; 
    } 

    return true; 
} 

你的方法去,你可以有一個簡單的檢查,這是不重複,將只檢查,看看是否所有四張牌是平等的,但如果你想要一個迭代的方法,那麼這可能是你最好的選擇。每當你收到一個arrayindexoutofbounds異常,你總是知道它與你的數組有關,在你的情況下,只有一個地方處理數組,所以一旦你知道異常的含義,它應該很容易被可視化。

一個非重複的方法如下...

//brain compiled code 
public static boolean containsFourOfaKind(int hand[]) 
{ 
    if((hand[0] == hand[1]) && (hand[1] == hand[2]) && (hand[2] == hand[3])) 
     return true; 

    return false; 
} 

這可以用來但它是不推薦使用。

1

而另一些解決的ArrayIndexOutOfBoundsException很清楚,我想建議,不使用索引的另一種方法:

private boolean isArrayEqual(int[] array) { 
     Arrays.sort(array); //Sort array to place four of a kind in sequence 

     int[] setOfFour = Arrays.copyOfRange(array, 0, 4); //Copy first four values 
     int[] compareArray = new int[4]; 

     Arrays.fill(compareArray, setOfFour[0]); //Make an array containing only first value 
     if (Arrays.equals(compareArray, setOfFour)) { //Test if first four are equal 
      return true; 
     } else { //Test is last four are equal 
      setOfFour = Arrays.copyOfRange(array, 1, 5); //Copy of last four values 
      Arrays.fill(compareArray, setOfFour[0]); 
      return Arrays.equals(compareArray, setOfFour); 
     } 
    } 

您創造出充滿從陣列中的一個值第二陣列問題(任何值都會做 - 我選擇了第一個)。然後看看數組是否相等。完成。

+0

這怎麼能檢測出5種手中的一種?它看起來像只能檢測到所有的數字是相等的 - 所以:5種。 –

+0

@ErwinBolwidt:你說的沒錯,我誤解了這個問題。我編輯了我的答案,檢查了4種。 – rothloup

0

一種並不專門針對一隻手的方法可能是針對一個更大的羣體;其中數組可能比4大得多。在這種情況下,你可以有一個循環添加到地圖計數一定的「對象」(字面意思)了多少次是在該列表:

public static boolean fourOfaKind(Integer[] hand) { 
    HashMap<Integer,Integer> counts = new HashMap<Integer,Integer>(); 
    for(Integer i : hand) { 
    if(counts.containsKey(i)) 
     { 
      int count = counts.get(i); 
      counts.put(i, ++count); 
      if(count >= 4) 
       return true; 
     } 
     else 
      counts.put(i, 1);  
     } 
    return false; 
} 
0

簡單的代碼可以如下,這將適用於N個元素。

public static boolean containsFourOfaKind(int hand[]){ 

     for(int i=1; i < hand.length; i++){ 
      if(hand[i-1] != hand[i]){ 
       return false; 
      } 
     } 
     return true; 
    } 
相關問題