简体   繁体   English

使用Helper时Android中的indexOf()

[英]indexOf() in Android when using Helper

I know that the below code gives the index of that particular element in java. 我知道下面的代码给出了Java中该特定元素的索引。

List<String> list = new ArrayList<>();

list .add("100");

Log.d("TAG",String.valueOf(list.indexOf("300")));

But how to get the index of an element while using a helper Class? 但是如何在使用辅助类时获取元素的索引?

List<HelperClass> Arraylist= new ArrayList<>();

Arraylist.add(new HelperClass(name, email, phoneno));

Log.d("TAG", String.valueOf(new HelperClass(Arraylist.indexOf(name,email,phoneno))));

I searched everywhere for this but couldn't find. 我到处搜索了此内容,但找不到。 Can someone tell me how to find index of a particular item in arraylist while using modal to add data? 有人可以告诉我在使用模式添加数据时如何在arraylist中查找特定项目的索引吗?

Obviously what I have tried is wrong and it shows red line under the whole line but I just typed that code for your understanding of what I want to achieve. 显然,我尝试的方法是错误的,它在整个行下显示红线,但我只是键入该代码以帮助您理解我想要实现的目标。 Can someone give me a way please? 有人可以给我一种方法吗?

Helper 帮手

 @Override
public int hashCode() {
    int result = getName() != null ? getName().hashCode() : 0;
    result = 31 * result + (Email != null ? Emaail.hashCode() : 0);
    result = 31 * result + (PhoneNo!= null ? PhoneNo.hashCode() : 0);

    return result;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Helper)) return false;

    Helperthat = (Helper) o;

    if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null)
        return false;
    if (Email != null ? !Email.equals(that.Email) : that.Email != null)
        return false;
    if (PhoneNo != null ? !PhoneNo.equals(that.PhoneNo) : that.PhoneNo != null)
        return false;

}

ArrayList#indexOf uses the Object#equals comparison method. ArrayList#indexOf使用Object#equals比较方法。

If you want to be able to lookup a HelperClass instance inside a Collection , you need to provide your own, overridden, equals method, and possibly also the hashCode one, for use with other, specific, Collection implementations ( Map , Set , etc.). 如果您希望能够在Collection内部查找HelperClass实例,则需要提供自己的,重写的, equals方法,还可能需要提供hashCode一个,以与其他特定的Collection实现( MapSet等)一起使用。 )。

class HelperClass {
   ...

   @Override
   public boolean equals(final Object object) {
      if (object == this) {
        return true;
      }

      if (!(object instance of HelperClass)) {
         return false;
      }

      final HelperClass other = (HelperClass) object;
      return name.equals(other.name) &&
             email.equals(other.email) &&
             phone.equals(other.phone);
   }
}

You obviously need to have an appropriate HelperClass instance to find a match. 您显然需要具有适当的HelperClass实例才能找到匹配项。

final String name = "Name";
final String email = "Email";
final String phone = "Phone";

final HelperClass first = new HelperClass(name, email, phone);
final HelperClass second = new HelperClass(name, email, phone);

final List<HelperClass> helpers = new ArrayList<>(8);
helpers.add(first);

final int index = helpers.indexOf(second); // index = 0

indexOf requires the object as input. indexOf需要对象作为输入。 If it does not find the object you are passing in, it will return -1. 如果找不到要传递的对象,它将返回-1。 You need to pass the object whose location in the arraylist you are looking for as the input into the indexOf function. 您需要将要在数组列表中查找其位置的对象作为输入传递给indexOf函数。

Solution : create a HelperClass to pass into the indexOf method: 解决方案:创建一个HelperClass传递给indexOf方法:

.indexOf(new HelperClass(name, email, phoneno));

However that change by itself will still return -1. 但是,该更改本身仍将返回-1。 See the api doc for indexOf: 请参阅API文档以获取indexOf:

public int indexOf(Object o) public int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. 返回指定元素在此列表中首次出现的索引;如果此列表不包含该元素,则返回-1。 More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. 更正式地,返回最低索引i,使其(o == null?get(i)== null:o.equals(get(i))),或者如果没有这样的索引,则返回-1。

It's using equals to decide whether it's found a match. 它使用equals来确定是否找到匹配项。 You should have overridden the equals method on your HelperClass class, so it's using the default implementation in java.lang.Object, which compares the references, and only returns true if the two references HelperClass to the same object. 您应该在HelperClass类上重写了equals方法,因此该方法使用java.lang.Object中的默认实现,该实现将对引用进行比较,并且仅在两个对同一个对象的HelperClass引用均返回true。

Override equals and hashcode on your HelperClass class, like: HelperClass类上重写等于和哈希码,例如:

@Override public boolean equals(Object other) {
    if (!(other instanceof HelperClass)) {
        return false;
    }
    HelperClass otherHelperClass = (HelperClass)other;
    return otherHelperClass.x == this.x && otherHelperClass.y == this.y;
}

@Override public int hashCode() {
    return x + y; // same values should hash to the same number
}

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

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