有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何计算重复联系人数

我正在学习编程课程的介绍,我们当前的任务是创建一个联系人管理器(本质上是一个地址簿),它有三个类:name(添加一个firstmiddlelast到一个contact)、contact(生成实际的contact本身)和ContactManager(保留一个contacts数组)。我在ContactManger类中的一个方法上遇到了问题,它应该检查数组中存在多少给定的contact对象,然后返回重复contacts的数量

下面是ContactManager类:

public class ContactManager
{
    private int nContacts;
    private Contact[] contacts;

    public ContactManager (int nMaxContact)
    {
        this.nContacts = 0;
        contacts = new Contact[nMaxContact];
    }

    /**
     * Given 4 String parameters, add a contact to the contacts array. Before adding
     * a contact to the array, do a parameter sanity check in this method.
     * If the array is full, or if first name or last name is null or an empty string, do
     * not add a contact but return false instead.
     *
     * Middle name can be left as null
     * or an empty String. Note that we allow adding duplicate contacts.
     *
     * If the name is acceptable, create a new contact and add the phone number to
     * the contact by calling the addPhoneNumber method of the Contact object. If
     * the method returns false, do not add the contact to the array and return
     * false (i.e., we discard the contact instantiated.) Otherwise, add the contact
     * to the array and return true.
     *
     * @param fName
     * @param lName
     * @param mName
     * @param phoneNumber
     *
     * @return a boolean value. See the description above
     */

    public boolean addContact (String fName, String lName, String mName, String phoneNumber)
    {
        if (fName == null || fName == "" || lName == null || lName == "" || contacts[contacts.length - 1] != null)
        {
            return false;
        }
        Name name;
        name = new Name(fName, lName, mName);

        Contact entry;
        entry = new Contact(name);

        Contact phone = new Contact(name);
        phone.addPhoneNumber(phoneNumber);

        nContacts++;

        return true;
    }

    /**
     * Given a Contact object, return the number of contacts with the identical name
     * (i.e., they have identical first, last, and middle name. If the input
     * parameter is null, return -1.
     *
     * For example, if the contacts array contains 10 contacts and there are 3
     * elements have the same name, return 3.
     *
     * @return an int value. See the description above.
     */

    public int countEqualContacts (Contact c)
    {
        int equal = 0;
        if (c == null)
        {
            equal = -1;
        }

        for (int i = 0; i < contacts.length; i++)
        {
            if (Contact.equals(c) == true)
            {
                equal++;
            }
        }

        return equal;
    }

    /**
     * This method returns the number of contacts (int)
     */

    public int countContacts ()
    {
        return nContacts;
    }
}

我知道我需要从namecontact类调用equals方法,但我不知道如何实现这些,因为我还在学习。此外,由于这是一门初级课程,我们还不允许使用ArrayLists。任何关于如何使countEqualContacts方法起作用的帮助都将不胜感激

谢谢


共 (1) 个答案

  1. # 1 楼答案

    在Contacts类中,override equals方法,仅当所有变量相等时才返回true,例如, 如果(this.fname.equals(fname)&&;这mName。等于(mname)。。等) 返回true; 其他的 返回false

    在CountEqualContacts方法中,你可以调用它来比较并找到相等的对象。小心使用空值(将检查空指针错误的方式和位置留给您)