0

我正在使用 blueJ 来写这个。我要做的是在 userGroup 类中编写一个名为 removeUser() 的方法,该方法将 String 作为参数,这是我要删除的用户名。使用迭代器,遍历列表,直到找到具有该用户名的用户并将其删除。我的代码是:

package user; 
public class User{
    public enum UserType{                          
        ADMIN, EDITOR, USER;
    }

    private String id;                            
    private UserType userPermissions;              
    private String actualName;                     

    public User(String username, UserType userType, String name){
        id = username;
        userPermissions = userType;
        actualName= name;
    }

    public String getUsername(){
        return id;
    }

    public UserType getUserType(){
        return userPermissions;
    }       

    public String getName(){
        return actualName;
    }

    public void setUserType(UserType input){
        userPermissions = input;
    }
}

userGroup 类是:

package user;
import java.util.*;
import user.User.UserType; 

public class UserGroup{

    private ArrayList<User> people;

    public UserGroup(){
        people = new ArrayList<User>();
    }

    public void addSampleData(){
        people.add(new User("jar1g13", UserType.ADMIN,"Jonny"));
        people.add(new User("ao9", UserType.EDITOR,"Aniruddh"));
        people.add(new User("pe6", UserType.USER,"Peter"));
        people.add(new User("mat73", UserType.USER,"Matthew"));
        people.add(new User("ora69", UserType.EDITOR,"Oranthi"));
        people.add(new User("ben12", UserType.USER,"Benedict"));
        people.add(new User("cam30", UserType.ADMIN,"Cambyse"));
        people.add(new User("are20", UserType.USER,"Alex"));
        people.add(new User("lim19", UserType.USER,"Liam"));
        people.add(new User("ada13", UserType.EDITOR,"Adam"));
    } 

    public User getUser(int idx){
        return people.get(idx);
    }

    public void printUsernames(){
        for (User user: people){
            System.out.printf("%s %s\n", user.getUsername(), user.getUserType());
        }
    }

    public void removeFirstUser(){
        people.remove(0);
    }

    public void removeLastUser(){
        people.remove(people.size()-1);
    }

    public void removeUser(String username){
        people.remove(username);
    }

}

这一切都编译得很好,但是当我运行 removeUser 方法时,它似乎没有从数组中删除任何东西!

4

6 回答 6

4
public void removeUser(String username) {
     Iterator<User> itr = people.iterator();
     while (itr.hasNext()) {
          Users element = (User) itr.next();
          if (element.getUsername().equals(username)) {
               itr.remove(); // REMOVE THIS FROM Iterator
          }
     }
}
于 2013-11-10T13:57:43.837 回答
1

尝试删除用户而不是带有字符串用户名的节点,或者如果您真的想通过字符串删除用户,只需迭代直到找到具有相同字符串的用户,User.actualName.equals(username)

public void removeUser(String username) {
   Iterator<User> it = people.iterator();
   while(it.hasNext()) {
   if(it.next().getName().equals(username)) { it.remove(); break; }
   }
}
于 2013-11-10T13:48:32.787 回答
1

你想创建一个函数。说,getUserByName(String name);

如此:

public User getUserByName(String name){
    for(int i = 0 ; i < people.size(); i++){
        if(people.get(i).actualName.equals(name)){
            return people.get(i);
        }
    }
}

然后,您只需执行以下操作即可删除它们:

people.remove(getUserByName("Alex"));

于 2013-11-10T13:53:09.897 回答
0

我建议使用 Map ,其中键是昵称。然后删除看起来像

Map<String,User>myMap = new HashMap<String,User>();
myMap.put("username", new User(...)); 
myMap.remove("username")

这种方法提供了更好的性能,因为读取 Map 比迭代 Array 更有效,并且在代码中看起来更好

于 2013-11-10T13:51:28.900 回答
0

这里有一个线索:

public void removeUser(String username){
    people.remove(username);
}

这里,username是一个字符串。您正在尝试从仅包含用户而不包含字符串的 ArrayList 中删除字符串。

于 2013-11-10T14:06:53.310 回答
0
public int addProduct(Product item)

{  

    int id = 0;

    stock.add(item);

    numberOfProducts++;

    for(int i = 0 ; i < stock.size(); i++)
    {

        if(stock.get(i).getID() == item.getID())
        {

           id++;

        }

        if(id > 1)

        {

            stock.remove(i);

            numberOfProducts--;

        }

    }

    return id;

}

这非常适合修复具有相同 ID 的产品或物品!如果您对 var 名称搜索项目作业“BlueJ”12 年级有任何疑问。

于 2016-04-25T01:12:47.733 回答