English 中文(简体)
我如何将物体从贾瓦的阿雷拉派清除出去?
原标题:How do I remove an object from an ArrayList in Java?

我有<代码>ArrayList,其中载有一些物体,例如<编码>User,每个物体有<代码> 姓名/代码>和<编码><> 密码/编码> 财产。 我如何只删除<代码>。 用户 具体名称来自<条码>的物体 ArrayList

最佳回答

你们可以这样作:

           // If you are using java 8
           userList.removeIf(user-> user.getName().equals("yourUserName"));
           // With older version
           User userToRemove = null;
           for(User usr:userList) {
             if(usr.getName().equals("yourUserName")) {
                userToRemove = usr;
                break;
             }
           }
           userList.remove(userToRemove);
问题回答
Iterator<User> it = list.iterator();
while (it.hasNext()) {
  User user = it.next();
  if (user.getName().equals("John Doe")) {
    it.remove();
  }
}

另一种想法: 如果<代码>User 类别可由用户名独一无二地界定,如果你凌驾于<代码>上的平等<>/代码>,则有下列内容:

public boolean equals(Object arg0) {
    return this.name.equals(((user) arg0).name);
}

页: 1 用户,不通过清单加以更新。 你可以做:

 list.remove(new User("John Doe"))

页: 1 在试图将物体从<代码>上删除时,同时修改<>。 名单/编号。 对这一例外的解释是,<代码>ArrayList的代号是故障处理器。 例如,如果它发现在运行时期的收集工作已经修改,它将放弃一个例外(失败)。 解决这一问题的方法是使用<代码>Iterator。

这里是一个简单的例子,表明你如何能够通过<条码>加以利用。 清单,并在满足特定条件时删除:

List<User> list = new ...

for (Iterator<User> it = list.iterator(); it.hasNext(); ) {

    User user = it.next();
    if (user.getUserEmail().equals(currentUser.getUserEmail())) {
       it.remove();
    }
}

建议解决这一问题的方法是:

ArrayList<User> list = new ArrayList<User>();
list.add(new User("user1","password1"));
list.add(new User("user2","password2"));
list.add(new User("user3","password3"));
list.add(new User("user4","password4"));
Iterator<String> iter = list.iterator();
while (iter.hasNext()) 
{
    User user = iter.next();
    if(user.name.equals("user1"))
    {
        //Use iterator to remove this User object.
        iter.remove();
    }
}

Using Iterator to remove an object is more efficient than removing by simply typing ArrayList(Object) because it is more faster and 20% more time saving and a standard Java practice for Java Collections.

你可以:

  • loop over the list with an iterator
  • check if each item in your list is the right user (checking the name)
  • if it is, use the remove method of the iterator.

Just search through the ArrayList of objects you get from the user, and test for a name equal to the name you want to remove. Then remove that object from the list.

你们的法典可能认为:

List<User> users = new ArrayList<User>();

public void removeUser(String name){
    for(User user:users){
        if(user.name.equals(name)){
            users.remove(user);
        }
    }
}
ArrayList<User> userList=new ArrayList<>();
//load data

public void removeUser(String userName){
    for (User user: userList){
        if(user.getName()equals(userName)){
            userList.remove(user);
        }
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签