0 java arrays string arraylist
我正在进行一场扑克游戏,而我正试图测试是否有任何玩家手中有皇家同花顺.然而,在我测试之前,我遇到了障碍:我没有一种快速而简单的方法来测试每个玩家的手牌,包括王牌,国王,女王,杰克和十分之一的西装.
到目前为止这是我的代码:
private void findWinner() {
// p1 hand
List<String> p1hand = cards.subList(10, 15);
p1hand.addAll(cards.subList(0, 2));
System.out.println("Player 1's total hand is " + p1hand);
// p1 hand
List<String> p2hand = cards.subList(10, 15);
p2hand.addAll(cards.subList(2, 4));
System.out.println("Player 2's total hand is " + p2hand);
// testing for royal flush
String[] spadeRoyalFlush = {"Ace of spades", "King of spades", "Queen of spades", "Jack of spades", "10 of spades"};
String[] heartRoyalFlush = {"Ace of hearts", "King of hearts", "Queen of hearts", "Jack of hearts", "10 of hearts"};
String[] dimRoyalFlush = {"Ace of diamonds", "King of diamonds", "Queen of diamonds", "Jack of diamonds", "10 of diamonds"};
String[] clubRoyalFlush = {"Ace of clubs", "King of clubs", "Queen of clubs", "Jack of clubs", "10 of clubs"};
// p1
boolean p1royalFlush = false;
if(p1hand.contains(spadeRoyalFlush)) { // line 104
p1royalFlush = true;
}
if(p1hand.contains(heartRoyalFlush)) {
p1royalFlush = true;
}
if(p1hand.contains(dimRoyalFlush)) {
p1royalFlush = true;
}
if(p1hand.contains(clubRoyalFlush)) {
p1royalFlush = true;
}
if(p1royalFlush) {
System.out.println("Player 1 got a royal flush with " + p1hand);
}
// p2
boolean p2royalFlush = false;
for(int i=0; i<p2hand.size(); i++) {
if(p2hand.contains(spadeRoyalFlush)) {
p2royalFlush = true;
}
if(p2hand.contains(heartRoyalFlush)) {
p2royalFlush = true;
}
if(p2hand.contains(dimRoyalFlush)) {
p2royalFlush = true;
}
if(p2hand.contains(clubRoyalFlush)) {
p2royalFlush = true;
}
}
if(p2royalFlush) {
System.out.println("Player 2 got a royal flush with " + p2hand);
}
} // line 165 is where I call this method
Run Code Online (Sandbox Code Playgroud)
cards是一个字符串的ArrayList,包含每张卡的每个名称(例如"黑桃王牌","心中的3"等).
当我运行此代码时,我收到错误:
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$SubList.checkForComodification(Unknown Source)
at java.base/java.util.ArrayList$SubList.listIterator(Unknown Source)
at java.base/java.util.AbstractList.listIterator(Unknown Source)
at java.base/java.util.ArrayList$SubList.iterator(Unknown Source)
at java.base/java.util.AbstractCollection.contains(Unknown Source)
at poker.PlayGame.findWinner(PlayGame.java:104)
at poker.PlayGame.doEverything(PlayGame.java:165)
at poker.MainPoker.main(MainPoker.java:7)
Run Code Online (Sandbox Code Playgroud)
我研究了这意味着什么,我感到震惊,因为据我所知,我没有迭代我的代码中的任何东西.
这是DoEverything方法:
public void doEverything() {
dealHands();
throwCards();
findWinner(); // line 165
}
Run Code Online (Sandbox Code Playgroud)
这是主要方法:
public static void main(String[] args) {
PlayGame game = new PlayGame();
game.doEverything(); // line 7
}
Run Code Online (Sandbox Code Playgroud)
我会List.containsAll()按照此处的说明使用.
如果此列表包含指定集合的所有元素,则返回true.
String[] array = ...;
List<String> list = ....;
list.containsAll(Arrays.asList(array));
Run Code Online (Sandbox Code Playgroud)
Reagrding将ConcurrentModificationException在线路104,您呼叫contains()的p1hand,但是p1hand是从cards.subList(10, 15).
我们看不出cards来自哪里.您尚未发布必要的代码来调试该点.但是请参阅@devpuh的答案,了解有关调用contains()子列表的更多详细信息.
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |