I have an ArrayList, which I want to hold instances of a particular class, Player. I have two classes, Player and Team. So essentially, the Team class contains an ArrayList of players for that team.
teamPlayers = new ArrayList(numPlayers);
I can add players to the ArrayList using
But I want to iterate through the ArrayList and print out the names of all players that have been added.
Can I use an interator, or a list iterator? How would I do this? I thought of something like:
But this obviously doesn't work. Could someone offer a suggestion as to how to do this?
Thanks
(Damn this forum needs a preview button)
teamPlayers = new ArrayList(numPlayers);
I can add players to the ArrayList using
code:<HR>Code:public void addPlayer(Player newPlayer) { System.out.println("Adding player: " + newPlayer.getPlayerName()); teamPlayers.add(newPlayer); }
But I want to iterate through the ArrayList and print out the names of all players that have been added.
Can I use an interator, or a list iterator? How would I do this? I thought of something like:
code:<HR>Code:public void listPlayers() { Iterator iter = teamPlayers.iterator(); while(iter.hasNext()) { System.out.println(iter.next().getPlayerName() + ", "); } }
But this obviously doesn't work. Could someone offer a suggestion as to how to do this?
Thanks
(Damn this forum needs a preview button)