如何返回的ArrayList&LT指数;现场> [英] How to return index of ArrayList<Field>

查看:155
本文介绍了如何返回的ArrayList&LT指数;现场>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以检索名单的特定元素的索引:

 的ArrayList<外勤及GT;名单=新的ArrayList<外勤及GT;();
    list.addAll(profile.getFieldsList());    对象privacyName =隐私;
    INT I = list.indexOf(privacyName);
    布尔doesContain = list.contains(privacyName);

有是包含在列表中的隐私的一个领域,但我始终是-1,doesContain永远是假的。为什么这个搜索无法正常工作?


解决方案

 公众诠释的indexOf(对象o){
         如果(O == NULL){
             的for(int i = 0; I<大小;我++)
                 如果(elementData中[I] == NULL)
                     返回我;
         }其他{
             的for(int i = 0; I<大小;我++)
                如果(o.equals(elementData中[I]))
                    返回我;
        }
        返回-1;
   }

您需要重写等于逻辑平等或依靠默认引用相等。

在这种情况下, privacyName.equals(elementData中[I])永远是假的。

I want to retrive index of specific element of my list :

    ArrayList<Field> list = new ArrayList<Field>();
    list.addAll(profile.getFieldsList());

    Object privacyName = "privacy";
    int i = list.indexOf(privacyName);
    boolean doesContain = list.contains(privacyName);

There is a field containing "privacy" in the list but i is always -1 and doesContain is always false. Why this search doesn't work ?

解决方案

    public int indexOf(Object o) {
         if (o == null) {
             for (int i = 0; i < size; i++)
                 if (elementData[i]==null)
                     return i;
         } else {
             for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
   }

You need to override equals for logical equality or rely on default for reference equality.

In this case privacyName.equals(elementData[i]) is always false.

这篇关于如何返回的ArrayList&LT指数;现场&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆