java - 如果对象的字段变量=方法的参数,如何从对象数组中删除对象

标签 java arrays object

我想删除一个名为 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();

}

最佳答案

原因是在类上声明 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);

关于java - 如果对象的字段变量=方法的参数,如何从对象数组中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60291517/

相关文章:

java - 在服务器上运行我的应用程序时如何避免 max_allowed_pa​​cket 错误?

python - 如何在 Django 模板中显示我过滤对象的其他条目

arrays - 我应该如何获取作为 Int 通过测试的对象的最后一个索引?

arrays - 如何循环遍历 JSONb 字段中包含的数组?

javascript - 如何转换对象数组?

javascript - ajax 请求后访问 JSON 中的元素

java - Jenkins : System. out.println 不在控制台窗口上打印任何内容

java - Spring重试和恢复

java - 什么是 NullPointerException,我该如何解决?

c# - 所有可能的数组初始化语法