java - 如果对象的字段变量=方法的参数,我如何从对象数组中删除对象
问题描述
如果姓氏字段变量=方法参数姓氏,我想删除一个名为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);
推荐阅读
- php - 为 php 响应式网站选择输入 onChange 事件在 Firefox 浏览器中不起作用
- reactjs - Fabric-ui-react GroupedList 组件如何在恢复每个组的折叠状态的同时动态添加项目?
- sql - 按模式返回具有逗号分隔值的行 (Oracle SQL)
- javascript - 仅在第二次操作后添加 animate.css 动画工作
- php - 如果一个条件不存在则返回值 mysql
- node.js - nginx proxy_pass 不适用于 nodejs 服务器
- ios - 不推荐使用 initWithReceiverApplicationID 使用 initWithDiscoveryCriteria 警告 Chromecast
- ios - 上传到 App Center 后缺少 dSYM
- python - 即使成功也会显示收到的任务
- django - 如何将过滤器应用于 Django rest-framework 序列化器字段?