3

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:

//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

3
  • 2
    You are thinking correct. What's the problem? Is that the source code you're trying to compile? In that case, add () after new Player. Like this: Player p = new Player(); numberOfPlayers.add(p); (assuming your Player class has a default constructor. Commented Nov 29, 2009 at 16:26
  • What is wrong with your code? Do you get an error? Commented Nov 29, 2009 at 16:30
  • problem is p must be null because I get a java.lang.NullPointerException when I call p.method(); Commented Nov 29, 2009 at 17:08

4 Answers 4

5
//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++) {

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

OK now how would I call p.method(); ie: p.dosomething();
what do you mean 'how would I call'? Just call it as you wrote.
problem is p must be null because I get a java.lang.NullPointerException when I call p.method();
ask another question including the entire source code and point where the null pointer exception happens.
5

Your code looks syntactically correct with one exception.

Change

p = new Player

to

p = new Player();

I'm assuming the variable n is declared and initialized and the Player class is defined with an argless constructor.

Comments

0

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.

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.