我正在使用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方法时,它似乎没有从数组中删除任何内容!
答案 0 :(得分: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
}
}
}
答案 1 :(得分:1)
尝试删除用户而不是带有字符串username的节点,或者如果您确实想要通过字符串删除用户,只需迭代直到找到具有相同字符串的用户,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; }
}
}
答案 2 :(得分: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"));
答案 3 :(得分:0)
我建议使用Map,其中key是昵称。然后删除看起来像
Map<String,User>myMap = new HashMap<String,User>();
myMap.put("username", new User(...));
myMap.remove("username")
这种方法提供了更好的性能,因为读取Map比迭代Array更有效,并且它在代码中看起来更好
答案 4 :(得分:0)
这是一个线索:
public void removeUser(String username){
people.remove(username);
}
这里,username
是一个字符串。您正在尝试从仅包含Users而不包含字符串的ArrayList中删除String。
答案 5 :(得分: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有任何疑问。