从java中的字符串数组的索引命名类实例的问题

时间:2011-02-15 20:36:53

标签: java arrays class

这是我的代码:

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]);
}

在第5行我在eclipse中遇到以下错误:

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

如果我不能这样命名,我怎么能以不同的方式成功地命名它们?

提前感谢=)

3 个答案:

答案 0 :(得分:2)

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

你到底想要在这里完成什么? playerNumber是一个字符串数组,而不是玩家。你能用英语说出你想做什么吗?我们会帮助您将其翻译成Java。

编辑:

有两种方法可以在这里“命名”你的球员。首先,您可以使用地图将名称与玩家相关联:

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

或者,如果您只是希望您的班级玩家拥有其名称的属性,您可以将其添加到您的班级:

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

我不清楚你想如何使用这些名字所以我不能说一个比另一个好。但是,在上面的内容中,最好使用构造函数来指定名称,如@Peter Lawrey的答案。

答案 1 :(得分:2)

另一种写这种方式可能是。

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

编辑:打破这一点。

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

split(“,”)使用提供的分隔符将String分解为String数组。所以它和。相同。

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

Arrays.asList将一个Object数组转换为List。

这一行

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

如果与

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

subList是从0numberOfPlayers的元素的视图。因此,如果numberOfPlayers为3,则会获得前3种颜色的列表。 for循环遍历subList中的元素,因此将迭代前3个元素。

该行

playerlist.add(new Player(playerColour));

类似于

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

但是它使用构造函数来构造具有名称的Player。使用构造函数的好处是你可以说没有名字就无法创建一个玩家(你不能这么说你可以创建)你也可以命名为final以明确它不能改变。

答案 2 :(得分:1)

这样的事情有用吗?:

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