我正在尝试在此数组中添加 ENTRY 类型的元素。但在添加之前,我想确保它不是重复的,因为我不想要重复的条目。尝试了很多,但它仍然没有添加条目:/我从 IF 子句中尝试了 BREAK 似乎也没有工作。我真的很傻吗?如果有人可以帮助我,我将不胜感激。干杯
import java.util.Arrays;
public class PhoneDirectory {
private Entry[] ar;
static int index = 0;
public PhoneDirectory() {
ar = new Entry[0];
}
public PhoneDirectory(Entry[] ent) {
ar = ent;
}
public boolean addEntry(Entry ent) throws NullPointerException {
boolean newEntry = true;
for (int i = 0; i < ar.length; i++) {
if (ar[i] == ent) {
newEntry = false;
else
newEntry = true;
}
int length = ar.length;
Entry[] temp = new Entry[length];
for (int x = 0; x <= length - 1; x++) {
temp[x] = ar[x];
}
length++;
ar = new Entry[length];
// add the new entry in the last index
for (int i = length - 1; i >= 0; i--) {
if (newEntry == true) {
ar[i] = ent;
newEntry = false;
// return true;
} else {
// ar[i] = temp[i];
return false;
}
}
return true;
}
}