我有以下blob代码。然而,这看起来很丑陋,如果我想以某种奇怪的理由再加上另一个肢体,它会变得单调乏味:
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 );
}
基本上,我不想重复我必须用三次替换字符串的每一行,而是想迭代每个字符串并将其插入一行代码中。
答案 0 :(得分:3)
您也可以使用枚举。
[1]如果你改变你的玩家使用枚举而不是字符串(我最喜欢的)
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]如果你想继续使用字符串来索引
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 );
}
}
答案 1 :(得分:2)
你可以这样做:
String[] limbs = {"legs", "thighs", "torso", "arm1", "arm2", "head"};
for (String limb : limbs) {
// . . .
}
答案 2 :(得分:1)
Java具有您可以使用的foreach loop的等价物:
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 );
}
答案 3 :(得分:1)
您可以使用数组和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);
}
我还要注意,因为你不经常更改数组(而不是让用户更改它),它只需要是一个传统的数组,而不是像其他一些帖子建议的列表。这是因为传统的数组效率更高但缺少列表的某些功能,例如添加或删除项目,但由于在这种情况下您不会使用这些功能,因此无需将列表的开销添加到此程序中。
答案 4 :(得分:0)
您可以在数组或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);
}