【发布时间】:2020-01-07 23:03:06
【问题描述】:
这里是代码有什么方法可以通过调用列表中的方法从列表中获取对象的索引。
例如这样的:
class A{
String a="";
String b="";
}
List<A> alist= new ArrayList();
for (A a : alist) {
a.getIndexInList();
}
【问题讨论】:
-
不,没有办法调用对象的方法并获取索引。您可以改为遍历索引,这可能是最好的方法。
-
是的,您可以在
A类中执行hashCode和equals方法。然后使用List<A> alist= new ArrayList(); for (A a : alist) { alist.indexOf(a);// This will give you the index of element }
标签: java list for-loop arraylist counter