I have an object that looks like this:
Account account = new Account (0, fName, sName, adr, city, pos, uniqueID);
I then put that object, via user input, to an ArrayList:
List<Account> newAcc = new ArrayList<Account>();
Here's the problem. I need a slick way to remove that object based on user input. This is what I've tried:
System.out.print("1. Client with Accounts.\n2. Client with Savings Accounts.\n3. Remove all Accounts.\n");
int inputRmv = in.nextInt();
case 1:
for (Iterator i = newAcc.iterator(); i.hasNext(); ) {
if (i.equals(rmvID)) {
newAcc.remove(i);
This doesn't work. The object isn't removed this way.
Basically: is there a way to use user input and then iterate through the list to see if any object contains a string equivalent to any part of the object?
I am in dire need, so any help to send me in the right direction is immensely appreciated!
Cheers.
You compare the Iterator
object with the rmvId
. This will always return false
, because they are not of the same type
. I guess you want to check if the iterator's next object's id is equal to rmvId
.
So use i.next()
to get the next SavingsAccount
, compare it's id with the rmvId
and then remove it through the Iterator
.
SavingsAccount sa = i.next();
if (sa.getId().equals(rmvID)) { // just an example.. I don't know how you access the
// saving account's id nor it's type. This example expect
// it is `Integer`
i.remove();
}
If the SavingsAccount
id is of type int
you can compare it this way sa.getId() == rmvID
.
Check the objects attribute whether it contains given id , and then remove the relevant object in the list by its index
for(int i=0;i<newSacc.size();i++){
if (newSacc.get(i).getId().equals(rmvID)){
newSacc.remove(i);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.