繁体   English   中英

无法从Arraylist中删除对象用户

[英]can't remove object user from Arraylist

有人可以帮我提供我的代码吗? 我是一个初学者,这个叫java的东西真的让我感到困惑:)我的问题是我必须删除用户,但是我的输出始终是找不到用户名...在此先感谢

public void removeUser() {
    java.util.Scanner input = new java.util.Scanner(System.in);
    int checks = 1;
    if (checks == 1) {
        for (int i = 0; i < userList().size(); i++) {

            System.out.println("Input user name for the account you want to be deleted");
            userName = input.next();

            if (userList.equals(userName)) {
                userList.get(i);
                userList.remove(userName);
                System.out.println("You succesfully removed user acount");
                System.out.println("If you want to exit press 0, if you want to continue press 1");
                checks = input.nextInt();

            } else {
                System.out.println("User name not found");
            }
        }
    }
    if (checks == 0) {

        administrator();
    }

}

您为什么认为这行得通?

if (userList.equals(userName)) 

??

也许只是尝试将其删除

boolean removed =  userList.remove(userName);

if (removed) {
       System.out.println("You succesfully removed user acount");
}                

无需循环

参见https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(java.lang.Object)

所以你的代码看起来像

java.util.Scanner input = new java.util.Scanner(System.in);
int checks = 1;
while (checks == 1) {
        System.out.println("Input user name for the account you want to be deleted");
        userName = input.next();

        if (userList.remove(userName)) {
            System.out.println("You succesfully removed user acount");
        }
        else {
            System.out.println("User name not found");
        }
        System.out.println("If you want to exit press 0, if you want to continue press 1");
        checks = input.nextInt();

}

您在此处执行代码的第一件事是将if (checks == 1)更改为while循环while(checks == 1) ,因为if只能执行一次。

第二件事if (userList.equals(userName))永远不会为true ,因此不会执行if子句。 首先,您需要从列表中获取用户名,例如: String name userList.get(i); 然后现在您可以检查它是否相等

if(name.equals(userName)) //or userList.contains(userName){
    // userList.remove(userName);
    // OR
    // userList.remove(i);
}

开斋节:

您可以按以下方式更改代码,也许对您有用

        List<String> userList = new ArrayList<>();
        userList.add("AA");
        userList.add("BB");
        userList.add("CC");
        java.util.Scanner input = new java.util.Scanner(System.in);
        int checks = 1;
        while (checks == 1) {
            for (int i = 0; i < userList.size(); i++) {

                System.out.println("Input user name for the account you want to be deleted");
                String userName = input.next();

                if(userList.remove(userName)) {
                    System.out.println("You scornfully removed user account");
                } else {
                    System.out.println("User name not found");
                }
                System.out.println("If you want to exit press 0, if you want to continue press 1");
                checks = input.nextInt();
            }
        }
        if (checks == 0) {
            administrator();
        }
public static void main(String[] args) {

    List<String> customerNames = new ArrayList<String>();
    customerNames.add("john");
    customerNames.add("lily");
    customerNames.add("druid");

    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Please enter 1 ");
    if(input.nextInt() != 1){
        System.out.println("break checks ... ...");
        return;
    }
    for(int i = 0; i < customerNames.size(); i++) {
        System.out.println("Input user name for the account you want to be deleted");
        if (customerNames.get(i).equals(input.next())) {
            customerNames.remove(i);
            System.out.println("You succesfully removed user acount");
            System.out.println("If you want to exit press 0 ... ...\n");
            if(input.nextInt() == 0){   //break
                break;
            }
        } else {
            System.out.println("User name not found... ...\n");
        }
    }
}
public static void main(String[] args) {

    List<String> customerNames = new ArrayList<String>();
    customerNames.add("john");
    customerNames.add("lily");
    customerNames.add("druid");

    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Please enter 1: ");
    if(input.nextInt() != 1){
        System.out.println("break checks ... ...");
        return;
    }

    System.out.println("========= start =========");
    System.out.println("Please enter 1: ");
    while(input.nextInt() != 0){
        System.out.println("Input user name for the account you want to be deleted... ...");
        System.out.println("enter name:");
        String _name = input.next(); 
        for(int i = 0; i < customerNames.size(); i++) {
            if(_name.equals(customerNames.get(i))){
                customerNames.remove(_name);
                System.out.println("You succesfully removed user acount");
                break;
            }
        }
        System.out.println("If you want to exit press 0 ... ...\n");
        System.out.println("input numeric:");
    }
    System.out.println("========= end =========");

    //break
    if(customerNames.size() == 0){return;}
    for(String name : customerNames){//print names
        System.out.println(name);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM