I've just finished an assignment and I wondered about somenthing.
I've created a method that takes an BankAccount ArrayList, substracts the Customer Objects out of it and put that in a new (Customer) ArrayList. After that returns that ArrayList.
The values in the Customer ArrayList have to be distinct.
I've created the following, which works:
public static ArrayList<Customer> extractCustomer(ArrayList<BankAccount> bankAccounts)
{
ArrayList<Customer> customerList = new ArrayList<Customer>();
for(BankAccount ba : bankAccounts)
{
if(!customerList.contains(ba.customer))
{
customerList.add(ba.customer);
}
else
{
//Not sure why I have to specifically remove it
//but if I leave it clear it doesn't work
customerList.remove(ba.customer);
}
}
return customerList;
}
I can check for duplicate values with contains().
What I don't understand why I can't do this:
if(!customerList.contains(ba.customer))
{
customerList.add(ba.customer);
}
else
{
}
And just leave else blank because if the if isn't correct it can't add the value to the list anyway?and I have to clearly state I don't what that value with remove()
Like I do here:
if(!customerList.contains(ba.customer))
{
customerList.add(ba.customer);
}
else
{
//Not sure why I have to specifically remove it
//but if I leave it clear it doesn't work
customerList.remove(ba.customer);
}
I print the values with the following loop
for(Customer c : extractCustomer(oldBankAccounts))
{
System.out.println(c.firstName);
}


Sign In
Create Account


Back to top











