为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用for循环保存在数组中并检查Java中的现有值

如何使用for循环保存在数组中并检查Java中的现有值

慕田峪7331174 2021-06-29 11:09:25
我是 Java 新手,但我正在学习一门课程,在该课程中我们编写了一个能够保存、存储、编辑和显示简历的网络应用程序。我坚持使用“保存”方法,我们将简历作为参数发送,并且必须通过简历的 UUID 进行检查:如果 UUID 已存在于数组中 - 将显示一条错误消息,如果 UUID 不在数组中 - 将保存简历。我已经用临时布尔值创建了我的方法,但根据任务它是不对的,所以我试图用 for 循环和带 break 的 if 语句来解决它,但我不明白如何保存简历,因为NPE 抛出 if 语句。这是我的代码:  public class ArrayStorage {    private Resume[] storage = new Resume[10000];    private int size;    public void save(Resume resume) {        for (int i = 0; i <= size; i++) {            if (storage[i].getUuid().equals(r.getUuid())) {                System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");                break;            }            storage[size] = resume;            size++;        }    }大小变量是为了避免检查整个数组,只是存储的元素。已编辑。我忘记了 for 循环中的等号,这是一个错误Exception in thread "main" java.lang.NullPointerExceptionat com.webapp.storage.ArrayStorage.save(ArrayStorage.java:23)at com.webapp.MainTestArrayStorage.main(MainTestArrayStorage.java:22)在 MainTestArrayStorage 我有这些字段 Resume r1 = new Resume();    r1.setUuid("uuid1");    Resume r2 = new Resume();    r2.setUuid("uuid2");    Resume r3 = new Resume();    r3.setUuid("uuid3");    Resume r4 = new Resume();    r4.setUuid("uuid1");    ARRAY_STORAGE.save(r1);    ARRAY_STORAGE.save(r2);    ARRAY_STORAGE.save(r3);    ARRAY_STORAGE.save(r4);最后一行用于检查方法,不应保存它,因为它具有相同的 UUID 如果您需要任何其他信息,我很乐意回答。谢谢。
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 241 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信