Java 在一组硬编码字符串上迭代

Java 在一组硬编码字符串上迭代,java,iteration,equivalent,Java,Iteration,Equivalent,我有下面的代码块。然而,这看起来很难看,如果出于某种奇怪的原因,我想添加另一个肢体,这会变得很乏味: player.get("legs").setPosition( playerInfo.get("legs").getPos().x, playerInfo.get("legs").getPos().y ); player.get("thighs").setPosition( playerInfo.get("thighs").getPos().x, playerInfo.get("th

我有下面的代码块。然而,这看起来很难看,如果出于某种奇怪的原因,我想添加另一个肢体,这会变得很乏味:

player.get("legs").setPosition(   playerInfo.get("legs").getPos().x,   playerInfo.get("legs").getPos().y   );
player.get("thighs").setPosition( playerInfo.get("thighs").getPos().x, playerInfo.get("thighs").getPos().y );
player.get("torso").setPosition(  playerInfo.get("torso").getPos().x,  playerInfo.get("torso").getPos().y  );
player.get("arms1").setPosition(  playerInfo.get("arms1").getPos().x,  playerInfo.get("arms1").getPos().y  );
player.get("arms2").setPosition(  playerInfo.get("arms2").getPos().x,  playerInfo.get("arms2").getPos().y  );
player.get("head").setPosition(   playerInfo.get("head").getPos().x,   playerInfo.get("head").getPos().y   );
正如你所看到的,这是非常重复的,那么什么是更好的写作方式呢?如果我能像perl一样流利地使用java,我会使用以下伪代码:

foreach my $limb ( qw( legs thighs torso arms1 arms2 head )) {
    player.get($limb).setPosition(   playerInfo.get($limb).getPos().x,   playerInfo.get($limb).getPos().y   );
}

基本上,我不想重复每一行,我将不得不替换字符串三次,而是希望迭代每个字符串,并将其插入到一行代码中。

Java有一个等价物,您可以使用:

List<String> limbs = Arrays.asList("legs", "thighs", "torso", "arms1", "arms2", "head");
for (String limb : limbs)
{
    player.get(limb).setPosition(
        playerInfo.get(limb).getPos().x,   playerInfo.get(limb).getPos().y   );
}
List四肢=数组;
用于(字符串肢体:肢体)
{
玩家。获取(肢体)。设置位置(
playerInfo.get(limb.getPos().x,playerInfo.get(limb.getPos().y);
}
您可以这样做:

String[] limbs = {"legs", "thighs", "torso", "arm1", "arm2", "head"};
for (String limb : limbs) {
    // . . .
}

您可以在数组或ArrayList中声明字符串并对其进行迭代

String[] s = new String[]{"legs", "thighs".....}
for (String key : s) {
    Position p = playerInfo.get(key).getPos(); //assuming this return Position
    player.get(key).setPosition(p.x, p.y);
}

您可以使用数组和for循环来实现这一点,而不是像现在这样对身体部位进行硬编码。 像这样:

//This creates an array of strings which you can add to if need be
String[] limbs = {"legs", "thighs", "torso", "arms1", "arms2", "head"};
    // This is equivalent to saying something like this (String limb : limbs)
    // It means for each string in the limbs array call it "limb and perform 
    //the operation below. This is just a shorthand and they do the same thing
    //but since you are new to Java I used the traditional for loop
    //The "-1" is there because the index of an array in java starts at 0 not 1
    for (int i = 0; i < limbs.length - 1; i++) 
    {
        //This will run this code for every limb in the limbs array replacing limb with "legs" or 
        //whatever else is in the array
        player.get(limb).setPosition(playerInfo.get(limb).getPos().x,   playerInfo.get(limb).getPos().y);
    }
//这将创建一个字符串数组,如果需要,可以添加到该数组中
字符串[]四肢={“腿”、“大腿”、“躯干”、“手臂1”、“手臂2”、“头”};
//这相当于这样说(字符串肢体:肢体)
//这意味着对于四肢数组中的每个字符串,将其称为“四肢并执行”
//下面的操作。这只是一个简写,他们做同样的事情
//但是因为您是Java新手,所以我使用了传统的for循环
//存在“-1”是因为java中数组的索引从0开始,而不是从1开始
对于(int i=0;i

我还想指出,因为您将不经常更改数组(而不是让用户更改它)它只需要是一个传统的数组,而不是像其他帖子所建议的那样是一个列表。这是因为传统数组效率更高,但缺少列表的一些功能,比如添加或删除项目,但因为在这种情况下您将不使用这些功能,所以不需要向该程序添加列表的开销。

Y您也可以改用枚举

[1] 如果您将播放机更改为使用枚举而不是字符串(我的最爱)

[2] 如果要继续使用字符串进行索引

enum limbs {legs,thighs,torso,arms1,arms2,head};

public static void main(String[] args) throws Exception {

    for (limbs limb : limbs.values()){
        player.get(limb.toString()).setPosition(
            playerInfo.get(limb.toString()).getPos().x,   playerInfo.get(limb.toString()).getPos().y   );
    }
}

你能更努力地描述你正在尝试做什么吗?这不是Java中枚举的正确语法。抱歉@DavidConrad,修复了
enum limbs {legs,thighs,torso,arms1,arms2,head};

public static void main(String[] args) throws Exception {

    for (limbs limb : limbs.values()){
        player.get(limb.toString()).setPosition(
            playerInfo.get(limb.toString()).getPos().x,   playerInfo.get(limb.toString()).getPos().y   );
    }
}