我怎样才能创建一个新的对象在Java中使用for循环的类? [英] How would I create a new object from a class using a for loop in java?

查看:188
本文介绍了我怎样才能创建一个新的对象在Java中使用for循环的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  int i;我有一个名为Card的类,我有这个for循环: 
for(i = 0; i <13; i ++){
Card cardNameHere = new Card();



$ b我想要做的是基于for循环创建新的实例。例如,我想要名称是card1,card2,card3等。这个数字来自for循环。



我已经试过了,它似乎不工作:

  int i; 
(i = 0; i <13; i ++){
Card card [i] = new Card();



$ b $ p
$ b

任何人都可以告诉我我做错了什么?



谢谢。




所以我使用的是气垫船Full Of Eels的解决方案我有另一个问题。
$ b我正在使用cardList.add(new Card()); ,当我尝试使用卡(我)设置名称,Java不会让我这样做。使用它没有我工作正常,但我如何访问它,所以我可以调用其他方法,如setId。我想调用cardName.setId();解决方案在循环之前创建你的数组 并在循环内填充。不过最好使用ArrayList。

 列表< Card> cardList = new ArrayList< Card>(); 
for(i = 0; i cardList.add(new Card());
//或新卡(i)(视情况而定)
}

如果你正在填充一副牌,并且你的Suit和Rank枚举很好的创建,那么:

$ $ p $ $ $ $卡> cardList = new ArrayList< Card>(); ($)
(套装:Suit.values()){
(Rank rank:Rank.values()){
cardList.add(new Card(suit,rank));









所以我使用的是cardList.add(新卡()); ,当我尝试使用卡(我)设置名称,Java不会让我这样做。使用它没有我工作正常,但我如何访问它,所以我可以调用其他方法,如setId。我想调用cardName.setId();

Card(i)没有意义,因为你不能把Card类看作是一个方法 - 我甚至不知道该怎么做,你在这里要做什么。如果你需要从ArrayList中提取一张卡片,你需要调用ArrayList上的 get(...)方法,这里叫做 cardList 。更好的办法是在其构造函数中设置Card属性,就像我在第二个代码片段中展示的那样。


I have a class named Card and I have this for loop:

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.

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.

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

解决方案

Create your array before the loop, and fill it inside the loop. Better to use an ArrayList though.

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:

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

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.

这篇关于我怎样才能创建一个新的对象在Java中使用for循环的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆