我对arraylist有一个奇怪的问题。我在for循环中向arraylist添加对象,除了arraylist add方法之外,一切正常。看起来我的列表是两次添加每个对象,但我找不到这个的原因。 这是我的代码
private void generateCitizens()
{
int counter = Constants.CITIZENS_NUMBER;
Random r = new Random(Calendar.getInstance().getTimeInMillis());
while(counter > 0)
{
int x = r.nextInt(Constants.TILES_IN_WIDTH) + 1;
int y = r.nextInt(Constants.TILES_IN_HEIGHT) + 1;
if(map.collisionTable[y][x] == 2)
{
Citizen c = new Citizen(x, y, citizenTexture.deepCopy(), map);
citizensList.add(c);
Log.i("Citizens count", String.valueOf(citizensList.size()));
scene.attachChild(c);
counter--;
}
}
for(int i = 0; i<citizensList.size(); i++)
{
Log.i("Citizen - ", citizensList.get(i).toString());
}
}
在这种情况下,Constants.CITIZENS_NUMBER为15,这就是我在日志中得到的
05-10 15:13:36.801:我/公民数(3237):2 05-10 15:13:36.801:我/公民数(3237):4 05-10 15:13:36.801:我/公民数(3237):6 05-10 15:13:36.801:我/公民数(3237):8 05-10 15:13:36.801:我/公民数(3237):10 05-10 15:13:36.811:我/公民数(3237):12 05-10 15:13:36.811:我/公民数(3237):14 05-10 15:13:36.811:我/公民数(3237):16 05-10 15:13:36.811:我/公民数(3237):18 05-10 15:13:36.821:我/公民数(3237):20 05-10 15:13:36.821:我/公民数(3237):22 05-10 15:13:36.821:我/公民数(3237):24 05-10 15:13:36.821:我/公民数(3237):26 05-10 15:13:36.831:我/公民数(3237):28 05-10 15:13:36.831:我/公民数(3237):30 05-10 15:13:36.831:I / Citizen - (3237):com.lpp.towndefence.Citizen@40535d40 05-10 15:13:36.831:I / Citizen - (3237):com.lpp.towndefence.Citizen@40535d40 05-10 15:13:36.831:I / Citizen - (3237):com.lpp.towndefence.Citizen@40537318 05-10 15:13:36.831:I / Citizen - (3237):com.lpp.towndefence.Citizen@40537318 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@40514688 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@40514688 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@40799fe8 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@40799fe8 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@4079c308 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@4079c308 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@4079abd8 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@4079abd8 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@4079b4a0 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@4079b4a0 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba160 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba160 05-10 15:13:36.841:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba230 05-10 15:13:36.851:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba230 05-10 15:13:36.851:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba300 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba300 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba3d0 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba3d0 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba4a0 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407ba4a0 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407bac18 05-10 15:13:36.861:I / Citizen - (3237):com.lpp.towndefence.Citizen@407bac18 05-10 15:13:36.871:I / Citizen - (3237):com.lpp.towndefence.Citizen@407bb3f0 05-10 15:13:36.871:I / Citizen - (3237):com.lpp.towndefence.Citizen@407bb3f0 05-10 15:13:36.871:I / Citizen - (3237):com.lpp.towndefence.Citizen@407bbc80 05-10 15:13:36.871:I / Citizen - (3237):com.lpp.towndefence.Citizen@407bbc80
答案 0 :(得分:1)
您是否继承了List
类?
可以new Citizen
将自己添加到列表中吗?或者场景中的attachChild
方法?