在 Java 中创建对象类数组的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23188454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:15:18  来源:igfitidea点击:

Creating an instance of an array of objects class in Java

javaarraysclassinitializationinstance

提问by singmotor

I'm new to Java, and I'm getting an error in my main function when I try to create an instance of a DeckofCards class. The DeckofCards class is supposed to be a private array of "Cards" objects. I think my issue is something in the DeckofCards class, for some reason it's not an array I think? Maybe I created it wrong?

我是 Java 新手,当我尝试创建 DeckofCards 类的实例时,我的主函数出现错误。DeckofCards 类应该是“卡片”对象的私有数组。我认为我的问题出在 DeckofCards 类中,出于某种原因,我认为它不是数组?也许我创建错了?

The errors in my main are '(' or '[' expectedand array required but DeckofCards found

我的主要错误是 '(' 或 '[' 预期数组,但发现 DeckofCards

Here is my main function:

这是我的主要功能:

public static void main(String[] args) {

        Card myCard = new Card(13,1);
        System.out.println(myCard.getSuit());
        System.out.println(myCard);
        DeckofCards myDeck = new DeckofCards;  //error here
        for(int i=0; i<53; i++) {
            System.out.println(myDeck[i]); //second error here
        }
    }

Here is my DeckofCards class:

这是我的 DeckofCards 课程:

public class DeckofCards {

    private Card[] deck = new Card[52];

    public DeckofCards(){
        int i = 0;

            for(int s = 1; s<5; s++){
                for(int r = 1; r<14; r++){
            deck[i].rank = r;
            deck[i].suit = s;
            i++;

    }
}
    }
}

If anyone can tell me if I'm missing some syntax or something that'd be great! Thank you.

如果有人能告诉我我是否遗漏了一些语法或一些很棒的东西!谢谢你。

采纳答案by Ted Hopp

You need to call the constructor:

您需要调用构造函数:

DeckofCards myDeck = new DeckofCards(); // note parens!

In Java, the parentheses are required.

在 Java 中,括号是必需的。

In the constructor, you will also need to initialize each element of the array to a new Cardobject:

在构造函数中,您还需要将数组的每个元素初始化为一个新Card对象:

for(int s = 1; s<5; s++){
    for(int r = 1; r<14; r++){
        deck[i] = new Card();
        deck[i].rank = r;
        deck[i].suit = s;
        i++;

回答by Reimeus

The error is pretty clear, myDeckis a single custom Objectrather than an array

错误很明显,myDeck是单个自定义Object而不是数组

DeckofCards myDeck = new DeckofCards();  // parenthesis here
for (int i=0; i<53; i++) {
    System.out.println(myDeck); // no brackets here
}

Although the loop itself should be located within the toStringmethod of the DeckofCardsclass.

虽然循环本身应该位于类的toString方法中DeckofCards

回答by TNT

Replace

代替

DeckofCards myDeck = new DeckofCards;

with

DeckofCards myDeck = new DeckofCards();

and myDeckwas never initialized to be an array.

并且myDeck从未被初始化为数组。

回答by Robert Moskal

In order to iterate through the DeckofCards you'll want to expose that private Card[] array. You can use the bean notation getCards() or just make the array public.

为了遍历 DeckofCards,您需要公开该私有 Card[] 数组。您可以使用 bean 符号 getCards() 或仅将数组设为公开。

    public class DeckofCards {

        private Card[] deck = new Card[52];

        public DeckofCards(){
            int i = 0;

                for(int s = 1; s<5; s++){
                    for(int r = 1; r<14; r++){
                deck[i].rank = r;
                deck[i].suit = s;
                i++;

        }
      } 
    }
     public Card[] getCards(){

          return deck;

     }
   }

I would probably just make the deck public.

我可能只是让甲板公开。