简体   繁体   English

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

[英]Java- Calling the integer part of an object array

I have an object array in which each object in the array has an integer component along with a string and a buffered image component. 我有一个对象数组,其中数组中的每个对象都有一个整数成分以及一个字符串和一个缓冲的图像成分。 My code creates a deck of fifty two cards each with a unique integer. 我的代码创建了52张卡片组,每张卡片都有一个唯一的整数。 I want to make a program that finds cards based on there integer value. 我想制作一个基于整数值查找卡的程序。

So for example if the integer 10 corresponds to the king of spades, the user can enter in the number 10 and the program will find the king of spades and move it to the top of the deck. 因此,例如,如果整数10对应于黑桃王,则用户可以输入数字10,程序将找到黑桃王,并将其移至牌组的顶部。 How can I find a card based on only its' integer value? 如何仅根据其整数值找到一张卡?

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")));
    }
}

Here is the deck constructor: 这是deck构造函数:

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);
        }
    }     
   }

You can do something like this: 您可以执行以下操作:

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

Your predicate is the matching criteria, in this case being the value matching the given number. 您的谓词是匹配条件,在这种情况下,是与给定数字匹配的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM