0

I have a strange problem with arraylist. I add objects to arraylist in for loop, everything is working fine except arraylist add method. It seems like my list is adding each object twice but I can't find a reason of this. Here is my code

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 is 15 in this case and this is what I got in logs

05-10 15:13:36.801: I/Citizens count(3237): 2 05-10 15:13:36.801: I/Citizens count(3237): 4 05-10 15:13:36.801: I/Citizens count(3237): 6 05-10 15:13:36.801: I/Citizens count(3237): 8 05-10 15:13:36.801: I/Citizens count(3237): 10 05-10 15:13:36.811: I/Citizens count(3237): 12 05-10 15:13:36.811: I/Citizens count(3237): 14 05-10 15:13:36.811: I/Citizens count(3237): 16 05-10 15:13:36.811: I/Citizens count(3237): 18 05-10 15:13:36.821: I/Citizens count(3237): 20 05-10 15:13:36.821: I/Citizens count(3237): 22 05-10 15:13:36.821: I/Citizens count(3237): 24 05-10 15:13:36.821: I/Citizens count(3237): 26 05-10 15:13:36.831: I/Citizens count(3237): 28 05-10 15:13:36.831: I/Citizens count(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

1 Answer 1

1

Did you subclass the List class ?

May new Citizen add itself to the list ? Or the attachChild method on scene ?

Sign up to request clarification or add additional context in comments.

1 Comment

Don't be too hard on yourself, that's common mistake. Try to define each object/entity role to avoid these mistakes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.