Java-调用对象数组的整数部分

时间:2017-11-22 14:59:13

标签: java arrays object

我有一个对象数组,其中数组中的每个对象都有一个整数组件以及一个字符串和一个缓冲的图像组件。我的代码创建了一个五十二张牌的牌组,每张牌都有一个唯一的整数我想制作一个基于整数值找到卡片的程序。

因此,例如,如果整数10对应于黑桃王,则用户可以输入数字10,程序将找到黑桃之王并将其移动到甲板的顶部。如何仅根据其“整数值”找到卡片?

public class Card_Class {
    private String suit, face;
    private int value;
    public BufferedImage cardImage;

public Card_Class(String suit, String face, int value, BufferedImage card) {
    this.suit = suit;
    this.face = face;
    this.value = value;
    cardImage = card;

}

public static void main(String[] args) throws IOException {

    Card_Class HeartKing = new Card_Class("Hearts", "King", 13, ImageIO.read(new File("HeartKing.jpg")));
    }
}

这是甲板构造函数:

public class Deck {

public static Card_Class[] deckOfCards;
private int currentCard;

public Deck() throws IOException {

    String[] faces = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
    deckOfCards = new Card_Class[52];
    currentCard = 0;

    final int width = 88;
    final int height = 133;
    final int rows =4;
    final int columns = 13;

    BufferedImage bigImage = ImageIO.read(new File("AllCards.png"));
    BufferedImage tempCardImage;

    for(int suit=0;suit <4; suit++){

        for(int face=0;face<13;face++){
            tempCardImage = bigImage.getSubimage(
                    face*width+(face*10)+8,
                    suit*height+(suit*10)+38,
                    width,
                    height);
            deckOfCards[(face+(suit*13))] = new Card_Class(suits[suit],faces[face],(13*suit+face),tempCardImage);
        }
    }     
   }

1 个答案:

答案 0 :(得分:1)

您可以这样做:

 private Card_Class find(int number){
    Predicate<Card_Class> p1 = c -> c.value == number;
    return Arrays.stream(deckOfCards).anyMatch(p1);
 }

您的谓词是匹配条件,在这种情况下是与给定数字匹配的值。