首页 > 解决方案 > 将一个号码添加到一个已经存在重复号码的列表中

问题描述

/*
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;
    }
}

这个不工作,我做错了什么?列表中的重复项未显示某些原因。

标签: android

解决方案


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;

}

推荐阅读