首页 > 解决方案 > 如果对象的字段变量=方法的参数,我如何从对象数组中删除对象

问题描述

如果姓氏字段变量=方法参数姓氏,我想删除一个名为Entry的对象实例。这是我到目前为止所拥有的:

public class ArrayDirectory implements Directory {

    public Entry[] records_array = new Entry[100];
    List<Entry> record_list = new ArrayList<Entry>(Arrays.asList(records_array));
    public static void main(String[] args){
        ArrayDirectory newArrayDirectory = new ArrayDirectory();
        Entry a = new Entry("Alexander" , "H.A", "53454");
        Entry b = new Entry("Hughes","H.H", "12234" );
        Entry c = new Entry("Brown" , "L.B", "47665");
        Entry d = new Entry("Jenkins", "A.J", "34456");
        Entry e = new Entry("Cole", "P.C","57811");


        newArrayDirectory.insertEntry(a);
        newArrayDirectory.insertEntry(b);
        newArrayDirectory.insertEntry(c);
        newArrayDirectory.insertEntry(d);
        newArrayDirectory.insertEntry(e);

        newArrayDirectory.deleteEntryUsingName("Alexander");

    }



    public void deleteEntryUsingName(String surname) {
        // turns records array into ArrayList
        Predicate<Entry> condition = Entry -> Entry.getSurname().contains(surname);
        record_list.removeIf(condition);
        records_array = record_list.toArray(records_array);// turns record_list back into an array
        System.out.print(Arrays.toString(records_array));
    }

}

我在调用该方法的主类中不断收到空指针异常,老实说,我真的不知道这意味着什么。

这是目录类:

import java.util.List;


public interface Directory {

    /**
     * Insert a new entry into the directory.
     *
     * @param entry the new entry to add
     */
    public void insertEntry(Entry entry);


    /**
     * Remove an entry from the directory using their surname.
     *
     * @param surname the surname of the entry to remove
     */
    public void deleteEntryUsingName(String surname);

    /**
     * Remove an entry from the directory using their extension number.
     *
     * @param number the extension number of the entry to remove
     */
    public void deleteEntryUsingExtension(String number);

    /**
     * Update an entry's extension number using their surname.
     *
     * @param surname   surname of the entry to be updated
     * @param newNumber the new number
     */
    public void updateExtensionUsingName(String surname, String newNumber);

    /**
     * Get the extension number of an entry using their surname.
     *
     * @param surname the surname of the entry
     * @return the extension number of the entry
     */
    public String lookupExtension(String surname);

    /**
     * Return an array list of all entries in the directory.
     *
     * @return an array list of all entries
     */
   public List<Entry> toArrayList();

}

标签: javaarraysobject

解决方案


原因是在类上声明 record_list 变量时,您正在将“records_array”数组转换为列表。此“records_array”数组最初包含 100 个 NULL 值,因此在 record_list 中填充了 null Entry 对象。然后在 deleteEntry 方法中对空对象调用“getSurname()”。因此出现空指针异常。您可以编写如下以避免删除方法中的异常:

Predicate<Entry> condition = entry -> Objects.nonNull(entry) && entry.getSurname().contains(surname);

推荐阅读