0

I am trying to delete one object from an ArrayList, but after iterating through the list with the for loop i'm stuck at what to do next. nameInput is a lowercase string from the user.

If i run this it prints the object from arr list equal to the input from nameInput. But I cannot understand how to go from printing that object to deleting it?

I'm sure this is a stupid question but the 50+ answers i have read and tried all seem to fail me (or more likely I fail to understand them). I have tried the list.remove and removeIf.

private ArrayList<Arr> arr = new ArrayList<>();

private void removeItem() {
    for (Object arr : arr) {
        if (((Arr) arr).getName().equals(nameInput())) {
            System.out.println(arr);        
            break;
        } else {
            System.out.println("Error");
        }
    }
}

4 Answers 4

5

Using for loop

List<Arr> arr = new ArrayList<>();

for (Arr item : arr) {
    if (item.getName().equals(nameInput())) {
        arr.remove(item);
        break;
    }
}

If not call break after remove element, you get ConcurrentElementException

Note from @Aomine: you have to implement correct Arr.equals() method.


Using Iterator

List<Arr> arr = new ArrayList<>();
Iterator<Arr> it = arr.iterator();

while (it.hasNext()) {
    Arr items = it.next();

    if (item.getName().equals(nameInput())) {
        it.remove();
        break;  // you can continue iterating and remove another item
    }

}

Using Streams

List<Arr> arr = new ArrayList<>();
arr.removeIf(item -> item.getName().equals(nameInput()));

Remove all items that match given condition


This is not good to remove element from ArrayList. In case you know that you have to remove element from the middle of the List, do use LinkedList.

4
  • I'd also point out that arr.remove(item); requires overriding equals and hashcode in Arr based on the name.
    – Ousmane D.
    Commented Jan 4, 2019 at 21:31
  • @Aomine sure, thank you. But hasCode() is not using in this case. It is for HashMap. Commented Jan 4, 2019 at 21:33
  • You're welcome ;). I always suggest overriding both regardless of whether you only need equals. it's good practice.
    – Ousmane D.
    Commented Jan 4, 2019 at 21:37
  • 1
    If you rely on equals() and that only the name field matters for that. arr.remove(new Arr(name)); is enough. The loop is helpless.
    – davidxxx
    Commented Jan 4, 2019 at 21:50
1

You are trying to remove an item while you are traversing/iterating the list in the for loop. You cannot remove an item from the list iterating it in a for loop. Use an Iterator instead and invoke arr.remove().

1

If you use Java 8 you could do

private void removeItem() {
    arr.removeIf(t -> t.getName().equals(nameInput));
}

Note that this will remove all objects with name equal to nameInput

Also you should change your declaration of arr to

List<Arr> arr = new ArrayList<>();
4
  • 1
    this will remove all the objects satisfying the provided predicate instead of just the first
    – Ousmane D.
    Commented Jan 4, 2019 at 21:21
  • @Aomine I don't think that is an issue but it's worth pointing out in the answer. Thanks. Commented Jan 4, 2019 at 21:22
  • 1
    The "issue" is that it is not what the OP does in its posted code. We exit the loop as soon as the first match and removal.
    – davidxxx
    Commented Jan 4, 2019 at 21:25
  • @davidxxx no but I get the impression each item (name) is unique. And really it is up to OP to decide if this is acceptable/correct or not Commented Jan 4, 2019 at 21:28
0

A couple of things here...

  1. The loop variable receiver type should ideally be Arr instead of Object as the list contains Arr objects. This also means you no longer need the cast you're performing.
  2. You could remove the item via remove(Object o) but this requires overriding equals and hashcode based on name only. Another option is via an iterator but this would mean changing your code completely. Thus, to keep it as close to your code as possible you can use a for loop; get the index which the object is located and then remove.

Thus, you can do:

for(int i = 0; i < arr.size(); i++){
    if (arr.get(i).getName().equals(nameInput)) {
          Arr obj = arr.remove(i); // remove the item by index     
          System.out.println(obj);  // print the object             
          break;  // terminate the loop iteration
     }
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.