/*
ADDING NUMBERS TO ArrayLIST AND IF NEW NUMBER ENTERED SAME AS already EXISTING NUMBER
RESAULT IS DUBLICATE;
*/
private String[] phoneNumbers;
public String addPhoneNumber(String newPhoneNumber) throws SMSDataModelFullException {
String result;
if (numPhoneNumbers == phoneNumbers.length) {
result = FULL;
} else {
boolean exists;
exists = findPhoneNumberIndex(newPhoneNumber) != -1;
if (exists) {
result = DUPLICATE;
} else {
phoneNumbers[numPhoneNumbers] = newPhoneNumber;
numPhoneNumbers++;
result = newPhoneNumber;
}
}
return result;
}
这对Array来说是完美的。我需要一些有关复制现有号码到列表
的帮助private List<String> phoneNumber;
public String addPhoneNumber(String newPhoneNumber) throws SMSDataModelFullException {
String result;
if (maxNumPhoneNumbers!=0 && phoneNumber.size() >= maxNumPhoneNumbers) {
throw new SMSDataModelFullException(newPhoneNumber);
}
else{
boolean exist;
exist =findPhoneNumberIndex(newPhoneNumber)!= -1;
if(exist)
{
result = DUPLICATE;
}
else{
phoneNumber.add(newPhoneNumber);
}
return newPhoneNumber;
}
}
这是行不通的,我在做什么错?列表中的重复项未显示某些原因。
答案 0 :(得分:1)
public String addPhoneNumber(String newPhoneNumber) {
String result = newPhoneNumber;
if(!phoneNumber.contains(newPhoneNumber)){ // if it is not already in the list
phoneNumber.add(newPhoneNumber); // add it to the list
}else{
result = "DUPLICATE"; // if number was already in the list result = DUPLICATE
}
return result;
}
答案 1 :(得分:0)
您总是返回newPhoneNumber
return newPhoneNumber;
与第一种情况相同
...
if (exists) {
result = DUPLICATE;
} else {
phoneNumber.add(newPhoneNumber);
result = newPhoneNumber;
}
}
return result;
您的findPhoneNumberIndex()
也可以替换为phoneNumber.contains(newPhoneNumber)