有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java遍历数组并打印每个对象

我的阵列正在加载并按计划打印卡(按它们在文件中的显示顺序)。当我尝试以单独的方法循环返回arraylist以检查数据是否存在时,它只打印最后一个对象,而不是每个对象。有人能告诉我为什么吗

加载方法

public class
    TestFrame {

//VARIABLES
private static Deck deck;
private static Card card;
private static Scanner scan;
private final static String fileName = "cards.txt";
static ArrayList<Card> cards = new ArrayList<>();

private static void Load(){

    deck = new Deck();
    card = new Card();

    // Load in the card file so that we can work with the data from cards.txt internally rather than from the file constantly.

    try(FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            Scanner infile = new Scanner(br)){

        int numOfCards = infile.nextInt();
        infile.nextLine(); // Do we need this? Yes we do. Illuminati confirmed.
        for(int i=0; i < numOfCards; i++){
            String value = infile.nextLine();
            String suit = infile.nextLine();
            Card newCard = new Card(value, suit);
            card.addCard(newCard);
            System.out.print(newCard.getValue());
            System.out.print(newCard.getSuit());
            System.out.println(" ");
            //Print out the object before cycling through again so we can see if it's working
            //We can use this to add then cards to the shuffle array at a later date
        }

    }

在运行时打印此文件:

ah 
2h 
3h 
4h 
5h 
6h 
7h 
8h 

等等,这是他们的顺序。txt文件

然后我使用这些方法显示所有卡片,以确保我可以在其他地方操作数据

private static void displayAllCards(){
    Card[] cards = Card.getAll();
    for(Card c : cards){
        System.out.print(Card.getValue());
        System.out.print(Card.getSuit());
        System.out.println(" ");
    }
}

和getAll()方法

public static Card[] getAll(){
    Card[] brb = new Card[cards.size()];
    int tempCount = -1;
    for(Card c : cards){
        tempCount++;
        brb[tempCount] = c;
    }
    return brb;
}

当运行getAll()时,它只打印出“ks”(黑桃之王),这是系统中的最后一张牌。txt文件。有谁能告诉我为什么会这样

谢谢

编辑:卡片类

package uk.ac.aber.dcs.cs12320.cards;

import java.util.ArrayList;


public class Card {

protected static String value;
protected static String suit;
static ArrayList<Card> cardsList = new ArrayList<>();

public Card(String v, String s){
    this.value = v;
    this.suit = s;
}

public Card() {

}

public static Card[] getAll(){
    Card[] brb = new Card[cardsList.size()];
    int tempCount = -1;
    for(Card c : cardsList){
        tempCount++;
        brb[tempCount] = c;
    }
    return brb;
}

public static void deleteAll(){
    cardsList.clear();
}

public static String getValue() {
    return value;
}

public void setValue(String value) {
    Deck.value = value;
}

public static String getSuit() {
    return suit;
}

public void setSuit(String suit) {
    Deck.suit = suit;
}

public void addCard(Card card){
    cardsList.add(card);
}

}


共 (5) 个答案

  1. # 1 楼答案

    card.addCard(newCard);
    

    我认为这应该是cards.addCard(newCard);

  2. # 2 楼答案

    你的卡片类没有很好地实现。之所以只得到最后一个值,是因为gettersString变量是静态的。每个类只有一个静态变量的副本。您需要创建这些实例变量/方法,并将打印循环更改为

    for(Card c : cards){
        System.out.print(c.getValue());
        System.out.print(c.getSuit());
        System.out.println(" ");
    }
    

    有关静态vs实例的详细说明,请参见this answer

  3. # 3 楼答案

    你是在一张特定的卡片上调用getAll(),但是之前当你有card.addCard时,你只会在列表中添加一张卡片,因为每个Card都有自己的ArrayList。这就是为什么它只会打印一张卡片。您需要在文件中创建的ArrayList上使用getAll()

    在主文件中创建一个getAll()方法,该方法使用static ArrayList<Card> cards = new ArrayList<>();

       public static Card[] getAll(){
        Card[] brb = new Card[cards.size()];
        int tempCount = 0;
        for(Card c : cards){
            brb[tempCount] = c;
            tempCount++;
        }
        return brb;
    

    这会管用的

  4. # 4 楼答案

    Card中,变量suitvalue被声明为静态。这意味着只有一个变量,在所有卡之间共享

    加载时,创建一张卡片。设置suitvalue,这样它们分别得到ha。然后打印出来

    然后加载接下来的两行。你创建了一张新卡。但是变量是静态的,不是新对象状态的一部分,而是包含ha的相同suitvalue。新值为2。它取代了a。然后打印出来。旧的价值观消失了

    因此,加载中的打印显示了suitvalue的瞬时值,但实际上,只有一个值——从文件加载的最后一个值

    这些变量应该是卡状态的一部分,因此,不是静态的

    注意:当您试图从静态上下文中使用实例变量时,IDE会注意到。例如,您的getValue方法是静态的。所以它不能访问实例变量。然而,校正不是将变量设置为静态,而是将方法(逻辑上应该是实例方法)更改为而不是static。它可能会再次抱怨,因为您正在从静态上下文调用该方法。然后你必须仔细看看原因:你不应该静态地使用这个方法——你应该创建一个Card的实例,并使用你创建的变量调用这个方法

    因此,IDE建议“使其保持静态”并不是正确的解决方案!您应该使所有与实例相关的变量和方法都不是静态的,并通过检查为什么不创建实例并决定静态调用与实例相关的方法来解决“静态上下文”问题

  5. # 5 楼答案

    card.addCard(newCard);看起来可能是cards.add(newCard);,这也是一个提示,当getAll函数从零开始时,更改索引看起来更好

        public static Card[] getAll(){
        Card[] brb = new Card[cards.size()];
        int tempCount = -1;
        for(Card c : cards){
            tempCount++;
            brb[tempCount] = c;
        }
        return brb;
    }
    

    换成

        public static Card[] getAll(){
        Card[] brb = new Card[cards.size()];
        int tempCount = 0;
        for(Card c : cards){
            brb[tempCount] = c;
            tempCount++;
        }
        return brb;
    }