user3236101
user3236101

Reputation: 78

checking if an object is in a list before adding java

Ok so I am having problems with my add method on how to implement checking if an object is already in the arraylist before adding a new one.

I would also like to create a remove method but everytime I attempt to use it I end up with a null pointer exception.

import java.util.*;

public class ContactsArrayList extends AbstractList{

    private Contact[] contacts;
    private int size;

    public ContactsArrayList(){
    //initializes contactArray with a capacity of 1
        contacts = new Contact[1];
        size = 0;
    }
    public ContactsArrayList(int capacity){
        if(capacity < 0){
            try {
                throw new Exception("Capacity: must be greater than zero.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        contacts = new Contact[capacity];
        size = 0;
    }
    public int size(){
        //returns size of list
        return size;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    private void listRangeCheck(int index){
        //checks if index is within range as built in debugging method
        if(index >= size || index < 0){
            throw new IndexOutOfBoundsException("Index: " + index + " is not within the list.");
        }
    }
    public Contact get(int index){
        //returns index at Contact at specified index
        listRangeCheck(index);
        return contacts[index];
    }
    public void capacityCheck(int minCapacity){
        //checks current capacity if capacity is less than required,
        // array copies its current values over to an array double the size
        int oldCapacity = contacts.length;
        if(minCapacity > oldCapacity){
            int newCapacity = (oldCapacity * 2);
            if(newCapacity < minCapacity)
                newCapacity = minCapacity;
            contacts = Arrays.copyOf(contacts, newCapacity);
        }
    }
    public boolean add(Contact contact){
    //appends the specified element to the end
        capacityCheck(size + 1);
        contacts[size++] = contact;
        return true;
    }
    public int capacity(){
    //returns ContactArray size
        return contacts.length;
    }
    public void sort() {
        //sorts the specified contact array list
        List<Contact> c_list = new ArrayList();
        c_list = Arrays.asList(contacts);
        Collections.reverse(c_list);
        c_list.toArray(contacts);
    }
}

Upvotes: 0

Views: 2650

Answers (3)

lonelyminstrel
lonelyminstrel

Reputation: 27

Please clarify. Will you be checking if two separately created objects have the same contents? Or will you be checking if you have added that object before?

All objects have a .equals() method, which checks that both references are the same object.

To extend Roey:

private boolean contains(Contact contact) {
    for (int i = 0; i < contacts.length; i++) {
        if (contacts[i].equals(contact)) {
            return true;
        }
    }
    return false;
}

In your add method, make a call to the contains method:

if contains(contact)
    return true
else
    add object

As to your remove method, you can also use the contains method. General pseudo code:

if contains(contact)
    shift all elements above contact down one slot
    decrement size
    return true
else
    return true

Upvotes: 1

roeygol
roeygol

Reputation: 5028

Simple as always:

private boolean contains(Contact contact){  
    for (int i=0; i<contacts.length; i++){      
        if (<condition to know if the 'contact' object exists>) return true;        
    }
    return false;
}

But if you will consider using ArrayList<Contact>() you can simply use it's contains()

Upvotes: 3

Ceelos
Ceelos

Reputation: 1126

If you're comparing objects I suggest you come up with a property to compare them by, otherwise you will end up inserting the object again if there's some difference between the object that exists and the current object that you're trying to add.

Upvotes: 0

Related Questions