1

I want to delete/remove an object from an array list of objects. Would this work if my class was:

class bookings {
  public String getuser () {
    return user;
  }

  public Date getbooking() {
    return bookingDate;
  }

  private String user;
  private Date bookingDate;
}

and I had an arraylist similar to:

ArrayList <bookings> book;


public void foo (String user, Date date) {
  for (bookings b : book) {
    if (user.equals(b.getuser()) && date.equals(g.getbooking()) book.remove(b);
  }
}
3
  • @xdazz yeah i've tried doing that and the object in the list still gets printed out when i tried system.out.println(b.getUser()) Commented Aug 28, 2011 at 3:03
  • b will still exist, but it will not be in the book arraylist. Commented Aug 28, 2011 at 3:21
  • Class name always starts with a capital letter, The more sound your fundamentals the better programmer you will be, and never use plural word for class name. It should be "Booking" not "bookings" Commented Aug 28, 2011 at 3:35

3 Answers 3

2

Firstly, you can't use a foreach loop for (bookings b : book) because it won't allow you to modify the collection (it will throw an exception).

This code should work:

public void foo (String user, Date date) {
  for (Iterator<bookings> i = book.iterator(); i.hasNext();) {
    bookings b = i.next(); // Oops - forgot this line!
    if (user.equals(b.getuser()) && date.equals(b.getbooking())
        i.remove(); // Corrected this too!
  }
}

It wouldn't hurt to name things using standard naming conventions and capitalization either:

class Booking {
    String getUser() ...
    Date getDate() ...
}

ArrayList<Booking> bookings;
Sign up to request clarification or add additional context in comments.

6 Comments

the b object is undefined in this, do i replace this with i?
@Clark @Bohemian how would I define i? I tried (bookings) i but that is throwing an error at me
@FHr did you see my example? you need to try (booking)i.next(); and bookings can be omitted here.
@Bohemian I tried your method. Using book.remove(b) will cause java.util.ConcurrentModificationException! I will not downvote . but it will cause misleading.
|
1

FHr, Your way will have problem.

java.util.ConcurrentModificationException

which means you are modifying your collection concurrently both in your loop and removal.

If you want to know the root cause why the error occurs.

You'd better look into this post

Why doesn’t java.util.Collection define next(), hasNext() directly?

So please use Iterator instead.

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }

You can refactor it with bookings type in your Iterator like Iterator<bookings>

6 Comments

i've tried this way and when i place two System.out.println() before and after it.remove() it shows that it has not been deleted
"will have problem" -- what problem?
if (user.equals(b.getuser()) && date.equals(g.getbooking()) { System.out.println(b.getUser()); book.remove(b); System.out.println(b.getUser()); }
@FHr you need to print out the list not b. Your goal is to remove b from book.
@Clark Bao yes and I stated it. You claim his code has a problem but do not say what it is.
|
0

Please check this: The Collection Interface

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

Comments

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.