2016-02-14 152 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; 
    } 
相关问题