我对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:I /公民数(3237):2
05-10 15:13:36.801:I /公民数(3237):4
05-10 15:13:36.801:I /公民数(3237):6
05-10 15:13:36.801:I /公民数(3237):8
05-10 15:13:36.801:I /公民数(3237):10
05-10 15:13:36.811:I /公民数(3237):12
05-10 15:13:36.811:I /公民数(3237):14
05-10 15:13:36.811:I /公民数(3237):16
05-10 15:13:36.811:I /公民数(3237):18
05-10 15:13:36.821:I /公民数(3237):20
05-10 15:13:36.821:I /公民数(3237):22
05-10 15:13:36.821:I /公民数(3237):24
05-10 15:13:36.821:I /公民数(3237):26
05-10 15:13:36.831:I /公民数(3237):28
05-10 15:13:36.831:I /公民数(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
最佳答案
您是List
类的子类吗?new Citizen
可以将自己添加到列表中吗?还是场景中的attachChild
方法?