如何在java中检查多个数组元素是否与来自同一方法的给定值匹配

时间:2013-03-17 17:19:25

标签: java arrays equals

如果我有一个存储人姓名地址电话等的数组然后我想搜索一个人是否存在,我会检查数组值是否等于给定值,但是,我不知道如何检查对于此声明中的多个数组值

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

我如何确保this.information [1]同等[1]和[2]同时保留人名?

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

感谢您的帮助!!!!

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] .....)))
        }
}

2 个答案:

答案 0 :(得分:0)

清洁方法将是

  • 具有各种字段的getter和setter的Person Javabean
  • 比较这些字段的等于方法。

答案 1 :(得分:0)

您可以使用toString()的{​​{1}}方法按以下方式检查相等性:

Person