Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

ArrayList, Distinct Objects

extract array

  • Please log in to reply
10 replies to this topic

#1 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 February 2012 - 12:21 PM

Hey CC'ers,

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);
}

  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#2 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 06 February 2012 - 01:55 PM

Why not use a collection that doesn't allow duplicates? Ex.: A HashSet or a TreeSet.
  • 0

#3 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 February 2012 - 02:09 PM

I know there are better ways to approach this but this is an exercise to learn a bit about ArrayLists, so I have to do it this way, just wanted to know why I have to use remove().
This is by the way an exercise for KnowledgeBlackBelt.
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#4 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 06 February 2012 - 02:37 PM

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);
            }
This seems illogical to me. This is how I'm reading what you have:
If you don't have an item in the list, then you want to add it. Otherwise, remove it. Why would you want to remove anything from your customerList?

The following seems to work and reproduces the results you want:

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListTest {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>( Arrays.asList( new String[]{"a", "e", "i", "o", "u", "a", "e", "i", "o", "u"}) );
		
		ArrayList<String> otherList = new ArrayList<>();
		
		for(String s : list) {
			if(!otherList.contains(s))
				otherList.add(s);
		}
		
		
		System.out.println(list);
		System.out.println(otherList);
	}
}

prints:
[a, e, i, o, u, a, e, i, o, u]
[a, e, i, o, u]

  • 0

#5 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 February 2012 - 02:41 PM

That's what I thought would happen. but even if I do it exactly as you did I still get a multiple Object.

only when I use remove() it gives me distinct objects
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#6 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 06 February 2012 - 02:58 PM

Do you think it has something to do with the way .contains() is comparing objects?
What type of object does BankAccount.customer return? A Customer Object? a String Object?

---------- Post added at 04:58 PM ---------- Previous post was at 04:53 PM ----------

Dur. I just looked at your code again. I see it returns a Customer Object. Are you overriding the equals() method in your Customer class?
  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 06 February 2012 - 02:59 PM

I assume, you have overridden the .equals(..) method from the Customer class, right?
  • 0

#8 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 February 2012 - 03:21 PM

Nope nothing at all

Customer.java:
public class Customer
{
    public String firstName;
    public String lastName;
}


(I know you should use getters and setters, but mind this is an exercise and it's suppose to be this way ;))
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#9 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 06 February 2012 - 03:25 PM

Added my project file.
Hope I'm not doing somenthing wrong but without the remove() I get back 2 Johns

Attached Files


  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#10 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 06 February 2012 - 04:36 PM

Check out the information for the contains method of ArrayList:

The definition states that it will use the equals method to compare the 2 objects. So, in your case, you'll need to override the equals method.
Without overriding the equals method, the contains method will use the inherited equals method of the Customer class.
  • 0

#11 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 10 February 2012 - 02:26 AM

Oke, then I'll override the equals() method in the main class I think.

Thanks for the help!
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download