微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 hashMap 键与 ArrayList 进行比较

如何解决将 hashMap 键与 ArrayList 进行比较

我正在做纸牌游戏作业,我们的讲师希望我们使用不同的数据结构。现在我有一个 Cards 对象的 ArrayList 和一个 Points 的 HashMap。我正在尝试将 HashMap 键与 ArrayList 中的元素进行比较,以检查 ArrayList 中卡片的点数,但似乎没有任何效果

这是哈希映射

    Map<Cards,Integer> Points = new HashMap<>();
    Points.put(new Cards("d","A"),1);
    Points.put(new Cards("c",1);
    Points.put(new Cards("h",1);
    Points.put(new Cards("s",1);
    Points.put(new Cards("d","2"),2);
    Points.put(new Cards("c",2);
    Points.put(new Cards("s",2);
    Points.put(new Cards("h",2);
    Points.put(new Cards("d","3"),3);
    Points.put(new Cards("c",3);
    Points.put(new Cards("s",3);
    Points.put(new Cards("h",3);
    Points.put(new Cards("d","4"),4);
    Points.put(new Cards("c",4);
    Points.put(new Cards("s",4);
    Points.put(new Cards("h",4);
    Points.put(new Cards("d","5"),5);
    Points.put(new Cards("c",5);
    Points.put(new Cards("s",5);
    Points.put(new Cards("h",5);
    Points.put(new Cards("d","6"),6);
    Points.put(new Cards("c",6);
    Points.put(new Cards("s",6);
    Points.put(new Cards("h",6);
    Points.put(new Cards("d","7"),7);
    Points.put(new Cards("c",7);
    Points.put(new Cards("s",7);
    Points.put(new Cards("h",7);
    Points.put(new Cards("d","8"),8);
    Points.put(new Cards("c",8);
    Points.put(new Cards("s",8);
    Points.put(new Cards("h",8);
    Points.put(new Cards("d","9"),9);
    Points.put(new Cards("c",9);
    Points.put(new Cards("s",9);
    Points.put(new Cards("h",9);
    Points.put(new Cards("d","X"),10);
    Points.put(new Cards("c",10);
    Points.put(new Cards("s",10);
    Points.put(new Cards("h",10);
    Points.put(new Cards("d","J"),"Q"),"K"),10);

这是对象卡片的 ArrayList (Player1)

    /**Cards class*/
    class Cards implements Comparable<Cards> {
    public String suit;
    public String face;
    
    public Cards() {
        
    }

    //Create Cards object 
    public Cards (String suit,String face){
        this.suit = suit;
        this.face = face;
    }

    //Return card suit
    public String getSuit() {
        return suit;
    }

    //Return card face
    public String getFace() {
        return face;
    }


    @Override
    public int compareto(Cards currentCards) {
        if (getSuit().compareto(currentCards.getSuit()) > 0) {
            return 1; 
        } else if ((getSuit().compareto(currentCards.getSuit())) < 0){
            return -1; 
        } else if ((getSuit().compareto(currentCards.getSuit())) == 0){
            if (getFace().compareto(currentCards.getFace()) > 0){
                return 1;
            } else if (getFace().compareto(currentCards.getFace()) < 0){
                return -1;
            }
        }
        return 0;
            
    }
    
    @Override
    public String toString() {
        return this.getSuit() + this.getFace();
    }
    }

/**Creating Cards object*/
String[] SUIT = new String[]{ "d","c","h","s" };
String[] FACE = new String[]{ null,"A","2","3","4","5","6","7","8","9","X","J","Q","K" };
Cards[] cardDeck = new Cards[52];
int index = 0;
    
  for (int i = 0; i <= 3; i++) {
      for (int j = 1; j <= 13; j++) {
          cardDeck[index] = new Cards(SUIT[i],FACE[j]);
          index++;
      }
  }

/**Player 1 in hand*/
ArrayList<Cards> player1 = new ArrayList<Cards>();
int j=0;

    for (int i = 0; i <18; i++){
        player1.add(i,cardDeck[j++]);
    }

我们试过了

for(Cards n : player1){
   if (Points.containsKey(n)){
       System.out.println("Card points: " + Points.get(n)); 
   }
}

程序编译并运行,但循环没有通过,因为打印语句没有打印

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?