Java for循环创建n个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1815955/
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-10-29 18:06:14  来源:igfitidea点击:

Java for loop to create n amount of object

javaarraylist

提问by Sizemj

I need some help. I want to create a for loop that creates n number of objects of a class, and then adds them into an arraylist. Something like this:

我需要一些帮助。我想创建一个 for 循环来创建一个类的 n 个对象,然后将它们添加到一个数组列表中。像这样的东西:

//Player is a custom class 
ArrayList<Player> numberofPlayersArray;
numberofPlayersArray = new ArrayList<Player>();

//n is a variable for the number of Player class objects that I want to create
  for(int i = 0; i < n; i++)
  {

    //this is what I can come up with but I am missing something 

     Player p;
     p = new Player
     numberofPlayersArray.add(p);

    }

Any help would be appreciated

任何帮助,将不胜感激

回答by Asaph

Your code looks syntactically correct with one exception.

除了一个例外,您的代码在语法上看起来是正确的。

Change

改变

p = new Player

to

p = new Player();

I'm assuming the variable nis declared and initialized and the Playerclass is defined with an argless constructor.

我假设变量n已声明并初始化,并且Player类是使用无参数构造函数定义的。

回答by Bozho

//Player is a custom class 
ArrayList<Player> numberofPlayersArray = new ArrayList<Player>(n);

//n is a variable for the number of Player class objects that I want to create
for(int i = 0; i < n; i++) {

    //this is what I can come up with but I am missing something 

     Player p = new Player();
     numberofPlayersArray.add(p);
}

Note that it's better to initialize the ArrayListwith the size, if it is known (as in your case)

请注意,最好ArrayList用大小初始化(如果已知)(如您的情况)

回答by Fortyrunner

Don't forget to code to the interface (rather than the concrete class).

不要忘记对接口(而不是具体类)进行编码。

List<Player> numberofPlayers = new ArrayList<Player>(n);

Forgetting to do this (or not knowing about it) is a common beginners mistake.

忘记这样做(或不知道)是初学者常见的错误。

If you decide to switch to an alternative list implementation later on (LinkedList or maybe a Google Collection or an Apache Commons Collection list) you won't have to change every reference to the list - just the initial allocation.

如果您决定稍后切换到替代列表实现(LinkedList 或 Google 集合或 Apache Commons 集合列表),您将不必更改对列表的每个引用 - 只需更改初始分配。

回答by Andreas Dolk

I don't see a problem here, just do

我在这里没有看到问题,只是做

p = new Player();

(but this might just have been a typo) and the playerlist will be populated with n different Player objects.

(但这可能只是一个错字)并且玩家列表将填充 n 个不同的 Player 对象。

Note, that I'm just assuming, you want to use the default constructor for Player.

请注意,我只是假设您想使用 Player 的默认构造函数。

Naming hint: you shouldn't name a List '..Array', unless you want to confuse yourself ;) Just name it '..List'

命名提示:您不应该将 List 命名为“..Array”,除非您想混淆自己;) 只需将其命名为“..List”