Java 遍历数组并打印每个对象
我的阵列正在加载并按计划打印卡(按它们在文件中的显示顺序)。当我尝试以单独的方法循环返回arraylist以检查数据是否存在时,它只打印最后一个对象,而不是每个对象。有人能告诉我为什么吗 加载方法Java 遍历数组并打印每个对象,java,arraylist,Java,Arraylist,我的阵列正在加载并按计划打印卡(按它们在文件中的显示顺序)。当我尝试以单独的方法循环返回arraylist以检查数据是否存在时,它只打印最后一个对象,而不是每个对象。有人能告诉我为什么吗 加载方法 public class TestFrame { //VARIABLES private static Deck deck; private static Card card; private static Scanner scan; private final static String
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
}
}
public static Card[] getAll(){
Card[] brb = new Card[cards.size()];
int tempCount = -1;
for(Card c : cards){
tempCount++;
brb[tempCount] = c;
}
return brb;
}
等等。这是它们在.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 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
}
}
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);
}
包uk.ac.aber.dcs.cs12320.cards;
导入java.util.ArrayList;
公务舱卡{
受保护的静态字符串值;
防静电绳套;
静态ArrayList cardsList=新ArrayList();
公共卡(字符串v、字符串s){
该值=v;
这个.suit=s;
}
公共卡(){
}
公共静态卡[]getAll(){
卡片[]brb=新卡片[cardsList.size()];
int tempCount=-1;
用于(卡c:cardsList){
tempCount++;
brb[tempCount]=c;
}
返回brb;
}
公共静态void deleteAll(){
cardsList.clear();
}
公共静态字符串getValue(){
返回值;
}
公共void设置值(字符串值){
Deck.value=值;
}
公共静态字符串getSuit(){
反诉;
}
公共诉讼(线状诉讼){
Deck.suit=套装;
}
公共作废卡片(卡片){
卡片列表。添加(卡片);
}
}
我认为这应该是cards.addCard(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;
}
您的卡类没有很好地实现。之所以只获取最后一个值,是因为getter
和String
变量是静态的。每个类只有一个静态变量的副本。您需要创建这些实例变量/方法,并将打印循环更改为
for(Card c : cards){
System.out.print(c.getValue());
System.out.print(c.getSuit());
System.out.println(" ");
}
有关静态vs实例的详细信息,请参见,您正在对特定卡调用getAll()
,但是以前当您有card.addCard
时,您只能向列表中添加一张卡,因为每个卡都有自己的ArrayList。这就是为什么它只会打印一张卡。您需要在文件中创建的ArrayList上使用getAll()
在主文件中创建一个getAll()方法,该方法使用static ArrayList cards=new ArrayList()代码>
这将起作用。在卡中
变量套装
和值
声明为静态。这意味着只有一个变量,在所有卡之间共享
加载时,您将创建一张卡。您可以设置suit
和value
,这样它们分别得到h
和a
。然后你把它们打印出来
然后加载接下来的两行。您创建了一张新卡。但是变量是静态的,不是新对象状态的一部分,而是包含h
和a
的相同suit
和value
。新值为2
。它取代了a
。然后你把它打印出来。旧的价值观消失了
因此,您在加载中的打印显示了suit
和value
的瞬时值,但实际上,只有一个值—您从文件中加载的最后一个值
这些变量应该是卡状态的一部分,因此,不是静态的
注意:当您尝试从静态上下文使用实例变量时,IDE会注意到。例如,您的getValue
方法是静态的。所以它不能访问实例变量。但是,纠正的方法不是将变量设置为static,而是将方法(逻辑上应该是实例方法)更改为notstatic
。它可能会再次抱怨,因为您正在从静态上下文调用该方法。然后,您必须仔细查看原因:您不应该静态地使用此方法-您应该创建Card
的实例,并使用您创建的变量调用该方法
因此,IDE建议“使其保持静态”并不是正确的解决方案!您应该使所有与实例相关的变量和方法都不是静态的,并通过检查为什么不创建实例并决定静态调用与实例相关的方法来解决“静态上下文”问题。因此,在同一代码块中使用单词的单数和复数是危险的!我也这么认为。但是实际上根本没有任何东西可以填充卡片
,所以它甚至不应该在循环中得到一个值。因此我不确定你的答案是否正确。我想在卡片上发生了一些事情。谢谢@pens-fan-69我已经更新了变量名,这样就不会那么混乱了。另外,正如我对另一个答案的评论一样,ArrayList
@realpoinsist中没有addCard
方法。我将用Card类更新OP,以显示中存在保存卡值的相关代码。何时填充卡ArrayList?请显示Card
类。我知道这听起来可能很愚蠢,但是有没有可能您没有填充卡片arrayList?另外,您的getAll()
方法可以简化为一行返回cards.toArray(新卡[cards.size()])
作为旁注,System.out.print(newCard.getValue());System.out.print(newCard.getSuit());System.out.println(“”)
可以简化为System.out.println(“+newCard.getValue()+newCard.getSuit()) public static Card[] getAll(){
Card[] brb = new Card[cards.size()];
int tempCount = 0;
for(Card c : cards){
brb[tempCount] = c;
tempCount++;
}
return brb;