使用用户输入和迭代器从ArrayList中删除元素

时间:2019-01-05 10:02:28

标签: java arraylist

我需要根据用户输入从ArrayList中删除一个元素。所以我有一个ArrayList,用户可以在其中注册狗。然后,如果用户想要删除一只狗,则他/她应该能够通过使用“删除狗”命令以及狗的名字来做到这一点。

我尝试使用迭代器,但是使用它时,仅使用else语句,并且在屏幕上打印出“什么都没有发生”。

import java.util.Iterator;
import java.util.Scanner;
import java.util.ArrayList;


public class DogRegister {
    ArrayList<Dog> dogs = new ArrayList<>();

    private Scanner keyboard = new Scanner(System.in);

    public static void initialize() {
        System.out.println("Welcome to this dog application");
    }


    private boolean handleCommand(String command) {
        switch (command) {
            case "One":
                return true;
            case "register new dog":
                registerNewDog();
                break;
            case "increase age":
                increaseAge();
                break;
            case "list dogs":
                listDogs();
                break;
            case "remove dog":
                removeDog();
                break;
            default:
                System.out.println("Error: Unknown command");
        }
        return false;
    }


    private void registerNewDog() {
        System.out.print("What is the dog's name? ");
        String dogNameQuestion = keyboard.nextLine().toLowerCase().trim();


        System.out.print("Which breed does it belong to? ");
        String dogBreedQuestion = keyboard.nextLine().toLowerCase().trim();

        System.out.print("How old is the dog? ");
        int dogAgeQuestion = keyboard.nextInt();

        System.out.print("What is its weight? ");
        int dogWeightQuestion = keyboard.nextInt();


        keyboard.nextLine();

        Dog d = new Dog(dogNameQuestion, dogBreedQuestion, dogAgeQuestion, 
dogWeightQuestion);
        dogs.add(d);

        System.out.println(dogs.get(0).toString());
    }


private void removeDog() {
        System.out.print("Enter the name of the dog ");
        String removeDogList = keyboard.nextLine();
        for (Iterator<Dog> dogsIterator = dogs.iterator(); 
dogsIterator.hasNext();) {
            if (removeDogList.equals(dogsIterator)) {
                System.out.println("The dog has been removed ");
                break;
            } else {
                System.out.println("Nothing has happened ");
                break;
            }
        }
    }


    public void closeDown() {
        System.out.println("Goodbye!");
    }

    public void run() {
        initialize();
        runCommandLoop();
    }


    public static void main(String[] args) {
        new DogRegister().run();
    }
}

2 个答案:

答案 0 :(得分:2)

您比较JB Nizet所说的StringIterator

if (removeDogList.equals(dogsIterator)) {

它将永远不会返回true。 此外,即使在迭代器上调用next()也不能解决问题,因为String也不能等于Dog对象。 取而代之的是,在使用String并调用String来有效删除当前迭代元素时,将equals()Iterator.remove()进行比较。

应该没问题:

private void removeDog() {
    System.out.print("Enter the name of the dog ");
    String removeDogList = keyboard.nextLine();

    for (Iterator<Dog> dogsIterator = dogs.iterator();dogsIterator.hasNext();) {   
        Dog dog = dogsIterator.next();
        if (removeDogList.equals(dog.getName())) {
            dogsIterator.remove(); 
            System.out.println("The dog has been removed");
            return;
        } 
     }

     System.out.println("Nothing has been removed");
}

答案 1 :(得分:1)

str3 = malloc(1+strlen("/") + strlen(path)+ strlen(dir->d_name) ); 不可能等于Iterator<Dog>:它们甚至没有相同的类型。

只有一个字符串可以等于一个字符串。

您要获取迭代器的String值,即Dog。然后,您想将狗的名字与String输入进行比较。

然后您要使用迭代器的next方法删除狗。阅读javadoc of Iterator