ArrayList removes objects based on the equals(Object obj) method. So you should implement properly this method. Something like:
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof ArrayTest)) return false;
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
Or
public boolean equals(Object obj) {
if (obj instanceof ArrayTest) {
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
return false;
}
Answer from Valchev on Stack OverflowArrayList removes objects based on the equals(Object obj) method. So you should implement properly this method. Something like:
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof ArrayTest)) return false;
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
Or
public boolean equals(Object obj) {
if (obj instanceof ArrayTest) {
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
return false;
}
If you are using Java 8 or above:
test.removeIf(t -> t.i == 1);
Java 8 has a removeIf method in the collection interface. For the ArrayList, it has an advanced implementation (order of n).
Question and solution: How to delete or add an object to/from array list while iterating it.
performance - Deleting objects from an ArrayList in Java - Stack Overflow
How do I remove an object from an arraylist and delete it in java?
How to remove object from ArrayList depending on attribute?
Videos
You could iterate backwards and remove as you go through the ArrayList. This has the advantage of subsequent elements not needing to shift and is easier to program than moving forwards.
Another way: The Iterator has an optional remove()-method, that is implemented for ArrayList. You can use it while iterating.
I don't know though, which variant is the most performant, you should measure it.
starblue commented, that the complexity isn't good, and that's true (for removeAll() too), because ArrayList has to copy all elements, if in the middle is an element added or removed. For that cases should a LinkedList work better. But, as we all don't know your real use-cases the best is too measure all variants, to pick the best solution.
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
I am creating a phone book program and need a method to remove an entry from the directory depending on what name or phone number the user enters.
So far I have the following code:
public void deleteEntry(String string) {
for(Entry e : phonebook){
if (e.getFirstName().equalsIgnoreCase(string)){
phonebook.remove(e);
System.out.println("Entry " + e.toString() + " removed");
}
else {
System.out.println("Entry not deleted");
}
}
}Which I thought should work, however it gives the following errors. The program outputs the deletion message as well which is weird so I am not sure what is causing this error.
The code the method is being called in is shown here:
case 2:
printDirectory();
System.out.println("Enter name or number: ");
String nameOrNum = input.nextLine();
deleteEntry(nameOrNum);
break;I have an Entry class which contains information for a single Entry object. Is there a way I could override the equals() method to compare first names and implement the delete method this way? Or can my solution be resolved easily?
Thanks in advance!
Hello there...I'm creating a bank system using java,just for fun.Anyways,I've faced a problem and couldn't find a solution for it on .I want to delete all info about a customer(name,account number,balance),however, I keep getting this error.
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification([ArrayList.java:909](https://ArrayList.java:909)) at [java.util.ArrayList$Itr.next](https://java.util.ArrayList$Itr.next)([ArrayList.java:859](https://ArrayList.java:859)) at banksystem2.BankSystem2.main([BankSystem2.java:112](https://BankSystem2.java:112))
C:\Users\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 36 seconds)
I can't post a pic for some reason.
Here's the code for my removal case(remove by account number)
case 3:
boolean Delete=true;
System.out.println("Enter the account number which you want to remove: ");
delete=in1.nextInt();
for(Customer c:list)
{
if(delete==c.getaccNum())
{ Delete=false;
System.out.println("HERE!! "+list.indexOf(c));
locate=list.indexOf(c);
list.remove(locate);
}
else
{
Delete=true;
}
}
if(Delete)
System.out.println("Invalid account number!!!");
break;
Can someone help me,pls?