如何使用 Java 中的 for 循环从类创建新对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19105401/
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
How would I create a new object from a class using a for loop in java?
提问by jdc987
I have a class named Card and I have this for loop:
我有一个名为 Card 的类,我有这个 for 循环:
int i;
for (i = 0; i < 13; i++) {
Card cardNameHere = new Card();
}
What I want to do is create new instances based on the for loop. So for example, I would like the names to be card1, card2, card3, etc. The number would come from the for loop.
我想要做的是基于 for 循环创建新实例。例如,我希望名称为 card1、card2、card3 等。数字将来自 for 循环。
I have tried this and it does not seem to work:
我试过这个,它似乎不起作用:
int i;
for (i = 0; i < 13; i++) {
Card card[i] = new Card();
}
Can anyone tell me what I am doing wrong?
谁能告诉我我做错了什么?
Thanks.
谢谢。
So I am using the solution from Hovercraft Full Of Eels, but I have another problem.
所以我正在使用来自 Hovercraft Full Of Eels 的解决方案,但我还有另一个问题。
I am using cardList.add(new Card()); , and when I try to use Card(i) to set the name, java won't let me do it. Using it without i works fine, but how do I access it so I can call another method on it such as setId. I would like to call cardName.setId();
我正在使用 cardList.add(new Card()); ,当我尝试使用 Card(i) 设置名称时,java 不会让我这样做。在没有 i 的情况下使用它可以正常工作,但是我如何访问它以便我可以在其上调用另一种方法,例如 setId。我想调用 cardName.setId();
采纳答案by Hovercraft Full Of Eels
Create your array beforethe loop, and fill it insidethe loop. Better to use an ArrayList though.
创建阵列之前的循环,并填写内部循环。不过最好使用 ArrayList。
List<Card> cardList = new ArrayList<Card>();
for (i = 0; i < MAX_CARD; i++) {
cardList.add(new Card());
// or new Card(i) as the case may be
}
If you're filling a deck of cards, and you have your Suit and Rank enums nicely created, then:
如果您正在填充一副纸牌,并且您的 Suit 和 Rank 枚举很好地创建,那么:
List<Card> cardList = new ArrayList<Card>();
for (Suit suit: Suit.values()) {
for (Rank rank: Rank.values()) {
cardList.add(new Card(suit, rank));
}
}
Edit
You state in comment:
编辑
您在评论中声明:
So I am using cardList.add(new Card()); , and when I try to use Card(i) to set the name, java won't let me do it. Using it without i works fine, but how do I access it so I can call another method on it such as setId. I would like to call the cardName.setId();
所以我使用 cardList.add(new Card()); ,当我尝试使用 Card(i) 设置名称时,java 不会让我这样做。在没有 i 的情况下使用它可以正常工作,但是我如何访问它以便我可以在其上调用另一种方法,例如 setId。我想调用 cardName.setId();
Card(i)
doesn't make sense as you can't treat the Card class as if it were a method -- again I'm not even sure what to make of it, what you're trying to do here. If you need to extract a Card from the ArrayList, you will need to call the get(...)
method on the ArrayList, here called cardList
. Better still would be to set the Card properties in its constructor as I show in my 2nd code snippet.
Card(i)
没有意义,因为您不能将 Card 类视为一种方法——同样,我什至不确定如何制作它,您在这里尝试做什么。如果需要从 ArrayList 中提取 Card,则需要调用get(...)
ArrayList 上的方法,这里称为cardList
. 更好的是在其构造函数中设置 Card 属性,如我在第二个代码片段中所示。
回答by Brandon
In Java, variable names have to be known at compile-time. Even in other languages like JavaScript, where that is not true, it is a good idea to not try to dynamically create variables at runtime.
在 Java 中,必须在编译时知道变量名。即使在其他语言(如 JavaScript)中,情况并非如此,最好不要尝试在运行时动态创建变量。
This is exactly what arrays are meant to solve: when you have items of a known type, but unknown quanity at compile time. You need to declare the card as an array: Card[] cards
这正是数组要解决的问题:当您拥有已知类型但在编译时数量未知的项目时。您需要将卡声明为数组:Card[] cards
Card[] cards = new Card[13];
for (int i = 0; i < cards.length; i++) {
cards[i] = new Card();
}
Notice I changed the for loop to loop through to cards.length, which is figured out at runtime, so that 13 does not have to be hard-coded in both places.
请注意,我将 for 循环更改为循环到 card.length,这是在运行时计算出来的,因此 13 不必在两个地方都进行硬编码。