In your case, there's no need to iterate through the list, because you know which object to delete. You have several options. First you can remove the object by index (so if you know, that the object is the second list element):
a.remove(1); // indexes are zero-based
Or, you can remove the first occurence of your string:
a.remove("acbd"); // removes the first String object that is equal to the
// String represented by this literal
Or, remove all strings with a certain value:
while(a.remove("acbd")) {}
It's a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can't remove them by using remove with an object that is equal to the one you want to delete.
In those case, I usually use a second list to collect all instances that I want to delete and remove them in a second pass:
List<MyBean> deleteCandidates = new ArrayList<>();
List<MyBean> myBeans = getThemFromSomewhere();
// Pass 1 - collect delete candidates
for (MyBean myBean : myBeans) {
if (shallBeDeleted(myBean)) {
deleteCandidates.add(myBean);
}
}
// Pass 2 - delete
for (MyBean deleteCandidate : deleteCandidates) {
myBeans.remove(deleteCandidate);
}
Answer from Andreas Dolk on Stack OverflowQuestion and solution: How to delete or add an object to/from array list while iterating it.
How does an ArrayList remove an element from an array?
Remove Element From ArrayList
Don't know if I can fix your problem, but here are a couple of notes:
You should first update your ArrayList and then your UI component. Reason: the user selects something with the UI component - if you modify the UI component first and then ask what the user selected so that you can remove it from the ArrayList, the selection the user made won't be there anymore. With luck it'll still somehow work out, but it's better to first do background processing and then update the UI.
what does your listener do? What do you need it for, especially in this single instance when the ok button is pressed?
you say you want your user to be able to remove an element (one), but you use removeAll(...). There is a better way to just remove one element.
getItems()gives you an ObservableList. ObservableList inherits two wonderful simpleremovemethods from List, use one of those. As for what you want to remove:getSelectionModel()should give you a MultipleSelectionModel if you didn't change that. MultipleSelectionModel inherits getSelectedIndex() and getSelectedItem() from SelectionModel. You should use one of those with the aforementioned remove method to only remove one element.
How do I make an arraylist of objects remove an object if one of its variables appears twice?
Videos
In your case, there's no need to iterate through the list, because you know which object to delete. You have several options. First you can remove the object by index (so if you know, that the object is the second list element):
a.remove(1); // indexes are zero-based
Or, you can remove the first occurence of your string:
a.remove("acbd"); // removes the first String object that is equal to the
// String represented by this literal
Or, remove all strings with a certain value:
while(a.remove("acbd")) {}
It's a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can't remove them by using remove with an object that is equal to the one you want to delete.
In those case, I usually use a second list to collect all instances that I want to delete and remove them in a second pass:
List<MyBean> deleteCandidates = new ArrayList<>();
List<MyBean> myBeans = getThemFromSomewhere();
// Pass 1 - collect delete candidates
for (MyBean myBean : myBeans) {
if (shallBeDeleted(myBean)) {
deleteCandidates.add(myBean);
}
}
// Pass 2 - delete
for (MyBean deleteCandidate : deleteCandidates) {
myBeans.remove(deleteCandidate);
}
One-liner (java8):
list.removeIf(s -> s.equals("acbd")); // removes all instances, not just the 1st one
(does all the iterating implicitly)