0

我有一个ArrayList对象,我试图返回对象中的arrayList对象.getName() == target

if(arrayList.contains(target)){
    System.out.print(arrayList.get(target));
}
4

5 回答 5

5

假设您的对象是类型Foo

for (Foo item : arrayList) {
    if (item.getName().equals(target)) return item;    
}
于 2013-03-15T18:24:35.300 回答
1

你可以试试这个:

int index = list.indexOf(elementToBeMatched);
    if (index != -1) {
        // Match found. Use this index
    } else {
        // match not found
    }
于 2013-03-15T18:26:34.877 回答
0
for (int i = 0; i < arrayList.size(); ++i) {
  if (arrayList.get(i).equals(target))
    return i;
}

或更好:

arrayList.indexOf(target)
于 2013-03-15T18:22:46.477 回答
0

用这个

arrayList.get(arrayList.indexOf(target))

您需要检查 -1 条件。

于 2013-03-15T18:23:58.307 回答
0
if(arrayList.contains(target)){
   System.out.print(arrayList.get(arrayList.indexOf(target)));
}
于 2013-03-15T18:24:14.337 回答