在添加java之前检查对象是否在列表中

时间:2015-09-24 21:33:04

标签: java arrays arraylist

好的,我的add方法有关如何在添加新对象之前检查对象是否已经在arraylist中的问题。

我还想创建一个删除方法,但每次我尝试使用它时,我最终会得到一个空指针异常。

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

3 个答案:

答案 0 :(得分:4)

一如既往地简单:

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

但如果你考虑使用ArrayList<Contact>(),你可以简单地使用它contains()

答案 1 :(得分:1)

请澄清。你会检查两个单独创建的对象是否具有相同的内容?或者你会检查你之前是否添加了该对象?

所有对象都有一个.equals()方法,它检查两个引用是否是同一个对象。

扩展Roey:

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

在add方法中,调用contains方法:

if contains(contact)
    return true
else
    add object

对于remove方法,您还可以使用contains方法。一般伪代码:

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

答案 2 :(得分:0)

如果您正在比较对象,我建议您提出一个属性来比较它们,否则如果存在的对象与您尝试的当前对象之间存在一些差异,则最终会再次插入对象添加。

相关问题