So, basically I want to do a shopping list that you can put the price, quantity, name. I want to implement a button that deletes an object. I have tried doing "int s = objectList.indexOf(myobject); objectList.remove(s); myobject=null;", but it keeps giving me errors. Please help me, if you need any code from my project, I will give it. Btw, the object extends JPanel
java - How to delete specific element from array list - Stack Overflow
java - Circular Linked List Implementation - Code Review Stack Exchange
(Java) Can't remove element from List/ArrayList, always gives NullPointerException or ConcurrentModificationException.
How does an ArrayList remove an element from an array?
Videos
The problem you have right now is you're just removing an element from the list's index based on user input while you should make sure list is of that size. Second thing, You can't remove an element from the list while browsing it via the forEach loop, you would get a ConcurrentModificationException , read concurrentModificationException. The way to do it would be using an iterator, check
CopyIterator<Passenger> itr = passengerList.iterator();
while (itr.hasNext()){
Passenger p = itr.next();
if (id == p.getId()) {
itr.remove();
break;
}
}
if you want to remove passenger from ArrayList using your passenger id, you can create your own delete function.
Copypublic void removeList(ArrayList<Passenger> passengerList, int passengerId) {
for (int i = 0; i<passengerList.size(); i++) {
if (passengerList.get(i).getId() == passengerId) {
passengerList.remove(i);
}
}
}
You can use like this
Copy@Override
public void deletePassenger(ArrayList<Passenger> passengerList) {
System.out.println("---Passengers list--");
for (Passenger tempPassenger : passengerList) {
System.out.println(tempPassenger);
System.out.println("If you want to see options menu, enter 6");
}
System.out.println("Enter ID of passenger that you want to delete!");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("---Passengers list--");
removeList(passengerList, id);
}
Error handling
I would expect the following to:
- Write errors to
System.errrather thanSystem.out. - Better yet, throw a
java.lang.IllegalArgumentExceptionthat can be handled accordingly by the caller.
public void remove(int index) { int counter = 1; int currentSize = size(); if (index > currentSize + 1 || index <= 0) { System.out.println("Invalid index."); return; }
Throwing an exception also simplifies, because the return is no longer necessary.
Indexing
You also seem to be indexing starting at 1 rather than 0. This is inconsistent with pretty much any other indexed data structure in Java or most other modern, mainstream programming language.
Iteration
Many operations (like printing a string representation) on your list your become easier if you implement an operator over lists.
I think it is unfortunate that insert and remove call size. While I understand that you would want to reject a bad index early in the function, calling size gives these functions the property that inserting/removing cost time linear in the size of the list, not linear in the index as may be expected (or hoped for). The out-of-range-ness could be detected during the (inevitable) iteration up to the insertion/removal index.
Generally data structures and algorithms should not print errors to System.out (where it may be mixed awkwardly in the middle of other output, or be invisible in GUI applications, etc, anyway it's a mixing of concerns), but report them to the caller.