我正在嘗試在此數組中添加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;
}
}
你如果塊括號是不均衡的。請更正它。 –