简体   繁体   中英

Iterating over a set of hardcoded strings

I have the below blob of code. However, this looks ugly, and it gets tedious if I want to, for some bizarre reason, add another limb:

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

As you can see, this is gettig very repetitive, so what is a better way of writing this? If i was as fluent in java as I am in perl I would go by something like this pseudo-code:

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

Basically, instead of repeating each line on which i will have to substitute the string three times, i would like to iterate over each string and insert it into one single line of code.

You can also use a enum instead.

[1] if you change your player to use enum instead of string (my favorite)

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

public static void main(String[] args) throws Exception {
    for (limbs limb : limbs.values()){
        player.get(limb).setPosition(
            playerInfo.get(limb).getPos().x,   playerInfo.get(limb).getPos().y   );
    }
}

[2] if you want to keep using strings to index

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

You could do this:

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

Java has an equivalent of the foreach loop that you can use:

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

You could use an array and a for loop to do this so instead of hardcoding body parts like you are doing. Like this:

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

I also want to note that because you are going to be changing the array infrequently (as opposed to having a user change it) it only needs to be a traditional array not a list like some of the other posts suggest. This is because a traditional array is more efficient but lacks some features of lists, like adding or removing items, but because you would not be using these features in this situation there is no need to add the overhead of a list to this program.

You may declare your strings in an array or ArrayList and iterate over them

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM