2

Hi I am figuring out why this code is not working the way how i wanted it to work..

What i basically want is a random object that has a char and an int generating and putting it into an arraylist. However if the generated matches the same in the arraylist it must regenerate the number again and check to see if it exists. If it does not exist it will then be added into the arraylist.

    private final char letter;
    private final int num;
    private static Collection<RegistrationNumber> REGISTRATION_NUMBER = new ArrayList<RegistrationNumber>();

    private RegistrationNumber(){
        Random rand = new Random();
        this.num = (1+(rand.nextInt(3)));
        this.letter = Character.toUpperCase((char)(rand.nextInt(1)+'a'));
    }

    public static RegistrationNumber getInstance(){
        boolean foo = false;
        RegistrationNumber rn = null;
        while(!foo){
            rn = new RegistrationNumber();
            if(!REGISTRATION_NUMBER.contains(rn)){
                REGISTRATION_NUMBER.add(rn);
                foo=true;
            }           
        }return rn;
    } 

Once I look through the arraylist, there are still some repeating for example [A1,A1,A2] or [A2,A2,A3]

Many thanks!

4
  • Try to use set.Is it ok Commented Mar 4, 2013 at 14:27
  • Do you know what contains is doing internally ? Have you implemented equals for RegistrationNumber ? Maybe you should use a Set instead of a List. Commented Mar 4, 2013 at 14:28
  • Have you read the API documentation for the contains method? Commented Mar 4, 2013 at 14:29
  • RE Naming convention, use capital letters and underscores for final fields only :) it doesnt matter if it is static Commented Mar 4, 2013 at 14:35

4 Answers 4

3

In order for the contains(rn) method to work, RegistrationNumber must override equals. Otherwise, two different objects with identical information would not be considered the same.

When you override equals, you need to also override hashCode, because java.Object's contract requires you to override both at the same time.

Finally, you may consider using the LinkedHashSet collection for your registrations: it gives you a predictable order of iteration, while automatically preventing duplicates. Of course you still need to override equals and hashCode.

Sign up to request clarification or add additional context in comments.

Comments

0

ArrayLists's contains method requires the use of an equals method overload that tells the collection how it will compare this object to existing ones (and therefore be able to find duplicates)

@Override
public boolean equals(Object obj) {
  if (this == obj)
    return true;

  if (obj == null)
    return false;

  if (getClass() != obj.getClass())
    return false;

  final RegistrationNumber other = (RegistrationNumber ) obj;

  if (num != other.num && letter != other.letter) 
    return false;

  return true;
}

Comments

0

The contains method, by default, uses the class' equals method. If equals has not been overridden, it will act as if you've used the "==" operator. This will test if it is the same object; not the object with the same content.

What you need to do is override the equals() method in your RegistrationNumber class, so it tests for member equality, rather than reference equality.

Comments

0

RegistrationNumber class must implement equals() and hashCode().

I will suggest to first, override those methods and then use a HashSet, wich will give you a Set that doesn't allow duplicates.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + num;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    RegistrationNumber other = (RegistrationNumber) obj;
    if (num != other.num)
        return false;
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.