【问题标题】:Remove element from ArrayList using user input and iterator使用用户输入和迭代器从 ArrayList 中删除元素
【发布时间】:2019-01-05 10:02:28
【问题描述】:

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

我尝试过使用迭代器,但在使用时只使用 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();
    }
}

【问题讨论】:

  • 更改此行:if (removeDogList.equals(dogsIterator)) {。而else 分支中的break 也可能是错误的。

标签: java arraylist


【解决方案1】:

您比较 StringIterator,正如 JB Nizet 所说:

if (removeDogList.equals(dogsIterator)) {

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

应该没问题:

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");
}

【讨论】:

  • @Dorian Gray。同意。没见过
  • 好的,但是当我尝试将迭代器更改为 Iterator 时,我仍然收到错误消息。我看过文档,但我真的不明白如何解决这个问题
  • @Hiwa_X 那是因为您正在迭代 ArrayList&lt;Dog&gt;。你有没有尝试上面的代码?
  • @DorianGray 是的,我已经尝试过代码,我的编辑器无法解析字符串 getName。我的 Dog 类中确实有一个 getName 方法,即返回名称
  • @davidxxx @Hiwa_X 好像应该是if (removeDogList.equals(dog.getName())),就是调用你提到的那个方法。你不能自己解决吗?
【解决方案2】:

Iterator&lt;Dog&gt; 不可能等于 String:它们甚至没有相同的类型。

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

您想要获取迭代器的next 值,它是一个Dog。然后你想将狗的名字与字符串输入进行比较。

然后你想删除狗,使用迭代器的remove() 方法。阅读javadoc of Iterator

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 2017-01-11
    • 2019-11-03
    • 2013-11-25
    • 1970-01-01
    • 2014-01-14
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多