本文介绍了如何从Java中的ArrayList中删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ArrayList
,它包含一些对象,例如 User
,每个对象都有一个名称
和密码
属性。如何从此 ArrayList
中仅删除具有特定名称的用户
对象?
I have an ArrayList
that contains some object, such as User
, and each object has a name
and password
property. How can I delete only the User
object that has a specific 'name' from this ArrayList
?
推荐答案
Iterator<User> it = list.iterator();
while (it.hasNext()) {
User user = it.next();
if (user.getName().equals("John Doe")) {
it.remove();
}
}
这篇关于如何从Java中的ArrayList中删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!