使用Java中的所含对象属性获取一个ArrayList指数 [英] Get index of an arraylist using property of an contained object in java

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

问题描述

我有对象类型的列表。在那我有一个String属性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.

推荐答案

编写小的helper方法。

Write small helper method.

 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 nor forget to return -1 if not found.

这篇关于使用Java中的所含对象属性获取一个ArrayList指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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