如何使用for循环在数组中保存,并检查Java中的现有值
我是一名Java新手,但我正在学习一门课程,我们将编写一个能够保存、存储、编辑和显示简历的web应用程序。我一直使用“保存”方法,在这种方法中,我们将简历作为参数发送,并且必须通过简历的UUID进行检查:如何使用for循环在数组中保存,并检查Java中的现有值,java,arrays,Java,Arrays,我是一名Java新手,但我正在学习一门课程,我们将编写一个能够保存、存储、编辑和显示简历的web应用程序。我一直使用“保存”方法,在这种方法中,我们将简历作为参数发送,并且必须通过简历的UUID进行检查: 如果阵列中已经存在UUID-将显示错误消息 如果UUID不在阵列中-将保存恢复 我已经用一个临时布尔值创建了我的方法,但是根据一个任务它是不正确的,所以我尝试用一个for循环和一个带break的if语句来解决它,但是我不知道如何保存简历,因为NPE会抛出if语句。这是我的密码: p
- 如果阵列中已经存在UUID-将显示错误消息
- 如果UUID不在阵列中-将保存恢复
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++;
}
}
在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);
最后一行是check方法,它不应该保存它,因为它具有相同的UUID
如果你需要任何额外的信息,我很乐意回答。谢谢
另外,对不起,我的英语不好。如果没有实际的错误消息,我假设您遇到了NPE,因为resume.getUuid()返回null(可能为了测试您,resume也是null)。 我还假设存储中的简历是有效的(它们有一个Uuid)
公共类数组存储{
私人简历[]存储=新简历[10000];
私有整数大小;
公共作废保存(恢复){
对于(int i=0;i
我认为您混淆了大小和for循环的结束条件。在您的示例中,它的工作方式是,它将遍历数组,直到找到或复制,或者直到抛出NPE,因为存储[I]
为null
。但是因为您同时增加了索引(I
)和停止条件(size
)同时,您永远不会停止。当您找到重复项时,您将中断,当您找到空插槽时,您将获得NPE,或者当您到达满存储结束时,您将获得IndexArrayoutBoundsException
在这种情况下,在编写任何代码之前,首先对需要执行的操作有一个很好的了解是很有帮助的。如果你不能用普通的语言描述这个方法,那么你将很难编写它
试试这个:
- 遍历整个存储
- 如果你发现一个空槽,把新的简历放入存储器
- 找到重复项时停止,显示错误并中止
现在一步一步地写下来:
public void save(Resume resume) {
for (int i = 0; i < storage.length; i++) {
if (storage[i] == null) {
// good result: found an empty slot
storage[i] = resume;
size = i + 1;
return;
}
if (storage[i].getUuid().equals(resume.getUuid())) {
// bad result: found a duplicate
System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");
return;
}
}
// bad result: storage full
System.out.println("ERROR: Storage full");
}
公共作废保存(恢复){
for(int i=0;i
你应该使用列表而不是数组。列表的大小是可变的,你不必创建一万个空对象。你也不必检查大小,只需迭代列表中的所有对象并比较UUID。当然,只有在你不必使用数组的情况下才可以。你没有告诉我们从哪里获得NPE,所以让我们来分析一下分析您的代码(但您应该进行调试):
更新:
public class ArrayStorage {
private Resume[] storage = new Resume[10000];
private int size;
public void save(Resume resume) {
if(size == 0){
storage[size] = resume;
size++;
} else {
for (int i = 0; i < size; i++) {
if(size == storage.length){
System.out.println("ERROR: Storage is full");
break;
}
if (storage[i].getUuid().equals(r.getUuid())) {
System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");
break;
}
storage[size] = resume;
size++;
}
}
}
}
<>我刚才看到你把你的循环条件改为<代码> i为什么不考虑SET而不是数组?你可以在uxID>恢复> <代码>类中实现uuID为基础的<代码>等于<代码/> >代码> hash码< /代码>,然后简单使用<代码> hash映射。
另一种方法(jdk 8+)是使用Stream.anyMath
:
if(!Stream.of(storage).parallel().anyMatch(v->v.getUuid().equals(r.getUuid())){
存储[大小++]=恢复;
}
在插入之前,首先检查存储是否为空,并使用至少一个元素对其进行初始化。在尝试添加新元素之前,还要验证存储是否已满
试试这个:
public class ArrayStorage {
private Resume[] storage = new Resume[10000];
private int size;
public void save(Resume resume) {
if(size == 0){
storage[size] = resume;
size++;
} else {
for (int i = 0; i < size; i++) {
if(size == storage.length){
System.out.println("ERROR: Storage is full");
break;
}
if (storage[i].getUuid().equals(r.getUuid())) {
System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");
break;
}
storage[size] = resume;
size++;
}
}
}
}
公共类数组存储{
私人简历[]存储=新简历[10000];
私有整数大小;
公共作废保存(恢复){
如果(大小==0){
存储[大小]=恢复;
大小++;
}否则{
对于(int i=0;i
私人简历[]存储=新简历[10000];
创建一个具有10000个空对象的数组,其大小为0@GBlodgett没错,但看看他是如何在数组上迭代的:仅在第一个大小元素上迭代,即当数组为空时,根本没有迭代。之后,索引0处的元素应该是一个恢复(假设参数不为null)因此,下一次调用应该只检查该文件。@DmitriiKozlovskii我猜您对其进行了多次编辑,因此我得到了较旧版本的代码I存储[0]
为空。请参考他的答案。如果您仍然有一些问题,可以询问。我希望您使用的是IDE。只需放置一个debu
public void save(Resume resume) {
//size is initialized to 0, so all is fine here
for (int i = 0; i < size; i++) {
//now here's the candidate for the NPE
if (storage[i].getUuid().equals(resume.getUuid())) {
System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");
break;
}
//this could cause the NPE in a later iteration
storage[size] = resume;
size++;
}
}
public void save(Resume resume) {
if( resume == null || resume.getUuid() == null ) {
//Log the error here
return;
}
//the rest of your code goes here
}
public class ArrayStorage {
private Resume[] storage = new Resume[10000];
private int size;
public void save(Resume resume) {
if(size == 0){
storage[size] = resume;
size++;
} else {
for (int i = 0; i < size; i++) {
if(size == storage.length){
System.out.println("ERROR: Storage is full");
break;
}
if (storage[i].getUuid().equals(r.getUuid())) {
System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");
break;
}
storage[size] = resume;
size++;
}
}
}
}