简体   繁体   English

将字符串值与数组列表进行比较<object>

[英]Compare a string value with arraylist<object>

I have a class of persons.我有一类人。

Public class Person{

private String name = "";

    public Person(String name) {
        
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

Now I have another class and inside the class I declared an arraylist现在我有另一个类,在类中我声明了一个数组列表

public class AddPerson {
public ArrayList<Person> pers = new ArrayList<Person>();

//here we add some persons to the arraylist
pers.add(new Person("Simon"));
pers.add(new Person("Oscar"));
pers.add(new Person("Alfred"));

String name = "Simon";
pers.contains(name); //return false
pers.equals(name); //return false 

//I also want to be able to return the value and index of the name in arraylist if it exsists.
if(pers.contains(name)) //return index and value
}

When I try to check if two strings are equal or if a string is already in my list I get both false.当我尝试检查两个字符串是否相等或者一个字符串是否已经在我的列表中时,我得到两个错误。 I did some research and I saw that I need to override my equals method (and mabey hash and contains method as well).我做了一些研究,发现我需要覆盖我的 equals 方法(以及 mabey hash 和 contains 方法)。 I do not know how to do it and I could not find a good reference how to do it.我不知道该怎么做,也找不到很好的参考方法。 Please help me to achive that.请帮助我实现这一目标。

That is because you are trying to compare a string ( "simon" ) and an object Person built from a string ( Person("Simon") ).那是因为您正在尝试比较字符串 ( "simon" ) 和从字符串构建的对象 Person ( Person("Simon") )。 These are two different objects, and it is thus normal to have false as a result.这是两个不同的对象,因此结果为false是正常的。

Whether you want to compare two Person (this would make sense) or you are actually trying to compare a string and a person are it is most certainly a bad idea (you can't actually compare apples and oranges, well this is the same here).无论您是想比较两个Person (这很有意义),还是您实际上是在尝试比较一个字符串和一个人,这肯定是一个坏主意(您实际上无法比较苹果和橙子,这在这里是一样的) )。

What you might want to do is whether您可能想要做的是

  1. to override Person 's equals method to, for example, return true if they have the same name .覆盖Personequals方法,例如,如果它们具有相同的name ,则返回 true 。 => comparing Person s => 比较Person
  2. to iterate on your pers array and fetch it's name property via getName() to compare it with you name string => comparing String s迭代你的pers数组并通过getName()获取它的 name 属性以将它与你的name string 进行比较 => 比较String s

1 => 1 =>

@Override
public boolean equals(final Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    final Person person = (Person)o;
    return name.equals(person.name);
}

@Override
public int hashCode() {
    return Objects.hash(name);
}

2 => 2 =>

for (final Person person : pers) {
    if(name.equals(person.getName())){
        // ...
    }
}

Here the problem is you are comparing a Person object with String object using the contains method.So actually in this case there is no point of implementing equals() method and hashCode() methods or using the contains() method to solve this .Only you have to do is check if person's attribute name is equal to your comparing String called name.这里的问题是您正在使用 contains 方法将 Person 对象与 String 对象进行比较。所以实际上在这种情况下,实现 equals() 方法和 hashCode() 方法或使用 contains() 方法来解决这个问题没有意义。只有您需要做的是检查人的属性名称是否等于您的比较字符串名称。

Sample solution:示例解决方案:

String name="Simon";

Person result  = null;//To store the person object if a person name Simon found in the list
int personIndex = -1; //To store the index of founded person
int indexCount =-1;//To maintain the current index value of arrayList

for(Person tempPerson:pers){//Iterating the list to find a person name Simon
   indexCount++;
   if(pers.getName().equals(name)){
         personIndex = indexCount;
         result = tempPerson;
    }
}

Now you can use the values in personIndex and indexCount variables for future calculations.You can use a condition like if(personIndex >= 0) to check if really a person found with the comparing name after the iteration and then you can get the result variable value.现在您可以使用 personIndex 和 indexCount 变量中的值进行未来的计算。您可以使用if(personIndex >= 0)类的条件来检查迭代后是否真的找到了具有比较名称的人,然后您可以获得结果变量价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM