使用java中包含对象的属性获取数组列表的索引 [英] Get index of an arraylist using property of an contained object in java

查看:54
本文介绍了使用java中包含对象的属性获取数组列表的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象类型列表.因为我有一个字符串属性 idNum.现在我想通过传递 idNum 来获取列表中对象的索引.

I'm having an list of Object type. In that I have one String property idNum. Now I want to get the index of the object in the list by passing the idNum.

List<Object1> objList=new ArrayList<Object1>();

不知道怎么给objList.indexOf(//这里不知道怎么给);

是否可以在不迭代列表的情况下执行此操作.我只想使用 indexOf() 方法.

Is it possible to do this without iterating the list. I want to use indexOf() method only.

推荐答案

编写一个小辅助方法.

 private int getIndexByProperty(String yourString) {
        for (int i = 0; i < objList.size(); i++) {
            if (object1 !=null && object1.getIdNum().equals(yourString)) {
                return i;
            }
        }
        return -1;// not there is list
    }

如果没有找到,不要忘记返回-1.

Do not forget to return -1 if not found.

这篇关于使用java中包含对象的属性获取数组列表的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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