我有一个ArrayList
对象,我试图返回对象中的arrayList
对象.getName() == target
if(arrayList.contains(target)){
System.out.print(arrayList.get(target));
}
假设您的对象是类型Foo
:
for (Foo item : arrayList) {
if (item.getName().equals(target)) return item;
}
你可以试试这个:
int index = list.indexOf(elementToBeMatched);
if (index != -1) {
// Match found. Use this index
} else {
// match not found
}
for (int i = 0; i < arrayList.size(); ++i) {
if (arrayList.get(i).equals(target))
return i;
}
或更好:
arrayList.indexOf(target)
用这个
arrayList.get(arrayList.indexOf(target))
您需要检查 -1 条件。
if(arrayList.contains(target)){
System.out.print(arrayList.get(arrayList.indexOf(target)));
}