开发者

Issues with naming an instance of a class from an index of an array of Strings in java

Here's my code:

List<Player> l = new List<Player>();
String[] playerNumber = {"Red", "Green", "Blue", "Yellow" ,"Orange",
    "Black", "Purple"};
for(int i = 0; i < numberOfPlayers; i++){
        Player playerNumber[i] = new Player();
        System.out.println(numberOfPlayers);
        System.out.println(playerNumber[i]);
        l.add(playerNumber[i]);
}

on the 5th line I'm getting the following error in eclipse:

- Duplicate local variable playerNumber
- Debug Current Instruction Pointer
- Type mismatch: cannot convert from Player to Player[]
- Syntax error on token "i", delet开发者_如何学Pythone this token
- The method add(Player) in the type List<Player> is not applicable for the arguments 
 (String)

if I can't name it like that, how could I succesfully name them differently?

thanks in advance =)


String[] playerNumber = {"Red", "Green", "Blue", "Yellow" ,"Orange", "Black", "Purple"};
//...
Player playerNumber[i] = new Player();

What are you actually trying to accomplish here? playerNumber is an array of Strings, not Players. Can you say what you are trying to do in English? We'll help you translate it into Java.

Edit:

There are two ways you could "name" your players here. First, you could use a map to associate a name with a player:

Map<String, Player> players = new HashMap<String, Player>();
players.put("Red", new Player());
Player red = players.get("Red");

Or, if you just want your class Player to have an attribute for its name, you can add that to your class:

public class Player {

   //...
   private String name;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}

//...
Player[] playerNumber = new Player[5];
playerNumber[0] = new Player();
playerNumber[0].setName("Red");

It's unclear to me how you want to use these names so I can't say one is better than the other. In the above, however, it's probably better to use the constructor to specify the name, as in @Peter Lawrey's answer.


Another way to write this might be.

public class Start {
    private static final List<String> playerColours = Arrays.asList(
        "Red,Green,Blue,Yellow,Orange,Black,Purple".split(","));

    private final List<Player> playerlist = new ArrayList<Player>();

    public Start(int numberOfPlayers) {
        for(String playerColour: playerColors.subList(0, numberOfPlayers))
            playerlist.add(new Player(playerColour));
    }
}

EDIT: To break this down.

private static final List<String> playerColours = Arrays.asList(
        "Red,Green,Blue,Yellow,Orange,Black,Purple".split(","));

split(",") break a String into an array of String using the separator provided. So it is the same as.

private static final String[] playerColoursArray = {"Red", "Green", "Blue", 
    "Yellow" ,"Orange", "Black", "Purple"}
private static final List<String> playerColours = Arrays.asList(playerColoursArray);

Arrays.asList turns an array of Objects into a List.

This line

for(String playerColour: playerColors.subList(0, numberOfPlayers))

if the same as

List<String> subList = playerColors.subList(0, numberOfPlayers);
for(String playerColour: subList)

the subList is view of the elements from 0 to numberOfPlayers So if numberOfPlayers is 3, you get a list of the first 3 colours. The for loop iterates of the elements in the subList so would iterate over the first 3 elements.

The line

playerlist.add(new Player(playerColour));

is similar to

Player player = new Player();
player.setName(playerColour);
playerlist.add(player);

However it uses the constructor to construct the Player with the name. The advantage of using the constructor is that you can say a Player cannot be created without a name (with setters you cannot say that) You can also make the name final to make it clear it cannot change.


Would something like this work?:

List<Player> players = new List<Player>(); 
String[] playerColors = {"Red", "Green", "Blue", "Yellow" ,"Orange", "Black", "Purple"}; 
for(int i = 0; i < playerColors.length; i++){
         Player player = new Player();
         player.color = playerColors[i];
         players.add(player);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜