Java ArrayList Crear, editar y eliminar

I am trying to Add and delete from an arraylist of a collection of objects in the same loop but in different methods and my problem is after adding and deleteing i try to print my array list but i cant get the values that is added recently. I am using the ListIterator to add and the Iterator to delete because I read in a bunch of website that this is the only ways to add and delete and the change will take place directly in the arraylist but my problem is that it is adding and deleting and the arraylist count or reference number on the debugger looks fine but when I search through it I cant find the new. added object and I still find the deleted objects. this is the remove and add methods

public void RemoveFromAnimalList(Animal A) {
    Iterator<Animal> i = AnimalList.iterator();
    while (i.hasNext()) {
        if (A == i.next()) {
            i.remove();
        }
    }
}   

public boolean AddNewAnimalList(Animal A) {
    ListIterator litr = AnimalList.listIterator();
    Animal B = null;
    while (litr.hasNext()) {
        B = (Animal) litr.next();

            litr.add(A);
            return true;

    }
    return false;
}

this is the method that is calling those methods above and i have tried everything and debug everything and everytime i cant find the new add element eventhough the arraylist number looks right i have 400 objects and when i delete it reachs to 399 and then again when i add it get back to 400 but when i try to print and go through the objects i cant find the new ones.

public void FirstBirth() {
    TheXYAxisControl XYSave = null;
    int X = 0;
    int Y = 0;
    for (Animal A : AnimalList) {
        if (A.Type.equals("Empty")) {

            X = A.XAxis;
            Y = A.YAxis;
            XYSave = new TheXYAxisControl(X, Y);
            TheXYaxisArrayList.add(XYSave);

        }
    }
    Random RandomNumberGen = new Random();

    for (int s = 0; s < 3; s++) {

        int RandomNumberGenerator = RandomNumberGen.nextInt(TheXYaxisArrayList.size());

        Animal NewAnimal = new Animal(0, "Male", 0, TheXYaxisArrayList.get(RandomNumberGenerator).getX(), TheXYaxisArrayList.get(RandomNumberGenerator).getY(), 0);
        RemoveFromAnimalList(AnimalList.get(RandomNumberGenerator));
        AddNewAnimalList(NewAnimal);
        RemoveFromOtherList(TheXYaxisArrayList.get(RandomNumberGenerator));
    }

    int RandomNumberGenerator = RandomNumberGen.nextInt(TheXYaxisArrayList.size());
    Animal NewAnimal = new Animal(0, "Female", 0,  TheXYaxisArrayList.get(RandomNumberGenerator).getX(),    TheXYaxisArrayList.get(RandomNumberGenerator).getY(), 0);
    RemoveFromAnimalList(AnimalList.get(RandomNumberGenerator));
    AddNewAnimalList(NewAnimal);
    RemoveFromOtherList(TheXYaxisArrayList.get(RandomNumberGenerator));

}

please if you have any question or other part of code i can explain.

preguntado el 01 de febrero de 12 a las 14:02

Why do you not use arraylist's remove method instead? -

As an FYI your method and variable names should start with a lower case letter, it helps distinguish them from Objects. -

2 Respuestas

You should really use Java generics in your Collection, List, Iterator, ListIterator etc and avoid casting.

Also you're comparing with == Firma aqui:

if (A == i.next())

Which is just comparing 2 references. Change that to:

if (A.equals(i.next()))

Respondido 01 Feb 12, 18:02

How can i Avoid casting and what do you mean by avoiding casting ?. And even if i avoided casting is my objects will be directly added to the list in my way ?? - dori naji

and the casting part in the code is not effective because i dont use it. i need to modify it but it doesn't effect - dori naji

If you declare your list like this: List<Animal> animalList = new ArrayList<Animal>();no necesito hacer B = (Animal) litr.next();, rather you can use Animal B = litr.next(); - anubhava

First some advice about the code appearance in Java

  • The methods should start with small letter
  • Variables should start with small letter

The problem might be that you are not comparing the equality of object but only that they are the same object.

El operador ==, used on Object type compare the instance. In other words this compare the reference to memory where the object is assigned to.

If we wan to compare the object that they have equal content we should use the method equals(Object object)

Así que reemplace el if (A == i.next()) a if (A.equals(i.next())).

In addition a good practice is to compare a constant to value, so instead of A.Type.equals("Empty") debiera ser "Empty".equals(A.Type). This practice may reduce unwanted NullPointerExceptions in your code.

Respondido 01 Feb 12, 18:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.