-2

如果我有一个存储人名地址电话等的数组,然后我想搜索以查看一个人是否存在,我会检查数组值是否等于给定值,但是,我不确定如何检查多个数组此语句中的值

if (((Person) object.getFullName().equals(this.information[0] .....))) 

如果 [1] 和 [2] 都持有人的全名,我将如何确保 this.information[1] 相等?

我在下面包含了代码,问题出在代码的最后几行。

谢谢你的帮助!!!!

public class Person
{
    private String [] information = new String [7];

    public Person()
    {
        for (int i = 0; i < information.length; i++)
        {
            information [i] = null; 
        }
    }

    //construct a person object storing values in array
    public Person (String lName, String fName, String st, String city, String state, String zip, String phone)
    {
        information [0] = lName;
        information [1] = fName; 
        information [2] = st;
        information [3] = city;
        information [4] = state; 
        information [5] = zip;
        information [6] = phone; 

    }

    public void setFullName(String lastname, String firstname) 
    {
        if (lastname != null && firstname != null)
        {
            this.information[0] = lastname;
            this.information[1] = firstname;
        }
    }



    //set entire address together 
    public void setCompleteAddress(String street, String city, String state, String zip)
    {
        if (street != null && city != null && state != null && zip != null)
        {
            this.information[2] = street;
            this.information[3] = city;
            this.information[4] = state; 
            this.information[5] = zip;
        }
    }

    //set phone number 
        public void setPhoneNumber(String phone)
        {
            if (phone != null)
            {
                this.information[6] = phone;
            }
        }

        //get lastname 
        public String 


        //get full name 
        public String getFullname()
        {
            return this.information[0] + "  " + this.information[1];
        }

        //get full address
        public String getFullAddress()
        {
            return this.information[2] + " " +  this.information[3] + " "  + this.information[4] + " " + this.information[5];
        }


        //to string method 
        public String toString()
        {
            String temp = "Person: ";
            for (int i = 0; i < information.length; i++)
            {
                temp = temp + information[i];
            }
            return temp;
        }

        //searches if person exists 
        public boolean equals (Object object)
        {
            if (!(object instanceof Person || object == null)){
                return false 
            }
            if (((Person) object.getFullName().equals(this.information[0] .....)))
        }
}
4

2 回答 2

0

更清洁的方法是

  • 一个 Person Javabean,具有用于各个字段的 getter 和 setter
  • 比较这些字段的 equals 方法。
于 2013-03-17T17:26:27.497 回答
0

您可以使用以下toString()方式Person检查相等性的方法:

public boolean equals (Object object)
    {
        if (!(object instanceof Person || object == null)){
            return false 
        }
        Person per = (Person)object;
        if ((this.toString()).equals(per.toString()))
        {
           return true;
        }
        return false;
    }
于 2013-03-17T17:26:55.603 回答