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);
}
}
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:
It wouldn’t hurt to name things using standard naming conventions and capitalization either: