使用for循环执行此操作

时间:2010-12-29 13:37:45

标签: java android

现在这是一个非常简单的java问题......

        Object[] objectList={
            new Object(name[0], description[0], R.drawable.creep_0),
            new Object(name[1], description[1], R.drawable.creep_1),
            new Object(name[2], description[2], R.drawable.creep_2),
            new Object(name[3], description[3], R.drawable.creep_3),
};

如何使用for周期动态执行此操作?谢谢!

2 个答案:

答案 0 :(得分:7)

Creep[] creeps = new Creep[] 
    { R.drawable.creep_0, R.drawable.creep_1, R.drawable.creep_2, 
      R.drawable.creep_3 };
Object[] objectList = new Object[4];
for (int i = 0; i < 4; i++) {
    objectList[i] = new Object(name[i], description[i], creeps[i]);
}

答案 1 :(得分:0)

ArrayList objectList = new ArrayList(); 
for (int i = 0; i < 4; i++)
{
     Object o = new Object(name[i], description[i], R.drawable.creep_i);
     objectList.Add(o);
}