【问题标题】:Creating an ArrayList which adds and removes items by a menu创建一个通过菜单添加和删除项目的 ArrayList
【发布时间】:2014-05-14 18:51:20
【问题描述】:

我想通过我创建的菜单和我设计的PetTest 类中的switch 语句向名为mainListArrayList 添加和删除元素。

但是,当我添加 2 只狗或猫(例如,一只名叫 Garfield 和另一只名叫小猫),并通过从 switch 语句中选择选项 6 来调用 listCats 方法时,它表明两只猫都有同名“小猫”。你知道为什么我的代码会出现这种情况吗?

宠物类:

public class Pet
    {
        private static String name;



        public String getName()
        {
            return name;
        }

        protected void setName(String newName)
        {
            name = newName;
        }

        public Pet(String petName) {
            name = petName;
        }



    }

狗类:

public class Dog extends Pet
{

    private Double weight;

    public double getWeight()
    {
        return weight;
    }

    protected void setWeight(double newWeight)
    {
        weight = newWeight;
    }

    public String toString() {
        return String.format("%s , %f", getName(), getWeight());
    }

    public Dog(String petName, double dogWeight)
    {
        super(petName);
        weight = dogWeight;

    }
}

猫类:

 public class Cat extends Pet
{

    private String coatColor;

    protected String getColor()
    {
        return coatColor;
    }

    protected void setColor(String newColor)
    {
        coatColor = newColor;
    }

    public String toString() {
        return String.format("%s , %s", getName(), getColor());
    }


    public Cat(String petName, String coatColor)
    {
        super(petName);
        this.coatColor = coatColor;
    }
}

宠物测试类:

import java.util.*;

public class PetTest
{
    static ArrayList<Pet> mainList = new ArrayList<Pet>();
    static Iterator<Pet> mainIter = mainList.iterator();
    static Scanner keyboard = new Scanner(System.in);


    public static void listDogs()
    {
            for(Pet pet: mainList)
            {
                if (pet instanceof Dog)
                {
                    System.out.println(pet.toString());
                }
            }//end of for loop
    }//end of listDogs method

    public static void listCats()
    {
            for(Pet pet: mainList)
            {
                if(pet instanceof Cat)
                {
                    System.out.println(pet.toString());
                }
            }//end of for loop

    }//end of listCats method

    public static void addDog(String dogName,Double dogWeight)
    {
        Pet dog = new Dog(dogName, dogWeight);
        mainList.add(dog);
    }//end of addDog method

    public static void addCat(String catName, String furColor)
    {
        Pet cat = new Cat(catName, furColor);
        mainList.add(cat);
    }//end of addCat method

    public static void removeDog(String dogName)
    {
        for(Pet pet : mainList)
            {
                if(pet.getName().equals(dogName))
                {
                    mainList.remove(pet);
                }//end of if statement
            }//end of for loop

    }//end of removeDog method


    public static void removeCat(String catName)
    {//java.lang.IllegalStateException you must call next method of iterator
            for(Pet pet : mainList)
            {
                if(pet.getName().equals(catName))
                {
                    mainList.remove(pet);

                }//end of if statement

            }//end of for loop

    }//end of removeCat method

    public static void showMenu()
    {
        System.out.println("1. Add dog ");
        System.out.println("2. Add cat");
        System.out.println("3. Remove dog");
        System.out.println("4. Remove cat");
        System.out.println("5. List dogs");
        System.out.println("6. List cats");
        System.out.println("7. List all pets");
        System.out.println("8. Show min, max and average weight of dogs");
        System.out.println("0. Quit");
        int action = keyboard.nextInt();

        Scanner parameter1 = new Scanner(System.in);
        Scanner parameter2 = new Scanner(System.in);

        while(action != 0)
        {
        switch(action)
        {
        case 1: 
            System.out.println("Type in the name of the dog that you want to add.");
            String dogName = parameter1.next();
            System.out.println("Type in the weight of the dog that you want to add.");
            Double dogWeight = parameter2.nextDouble();
            addDog(dogName,dogWeight);
            showMenu();

            break;

        case 2:
            System.out.println("Type in the name of the cat that you want to add.");
            String catName = parameter1.next();
            System.out.println("Type in the color of the cat that you want to add.");
            String furColor = parameter2.next();
            addCat(catName,furColor);
            showMenu();

            break;

        case 3:
            System.out.println("Type in the name of dog that you want to remove.");
            String dogToRemove = parameter1.next();
            removeDog(dogToRemove);
            showMenu();

            break;

        case 4:
            System.out.println("Type in the name of dog that you want to remove.");
            String catToRemove = parameter1.next();
            removeDog(catToRemove);
            showMenu();

            break;

        case 5:
            listDogs();
            showMenu();

            break;

        case 6:
            listCats();
            showMenu();
            break;


        }//end of switch statement

        }//end of while loop
    }//end of showMenu method

    public static void main(String[] args)
    {

        showMenu();



        }//end of main method



}//end of the class

【问题讨论】:

  • 谢谢,我没想到:D

标签: java arraylist switch-statement overriding


【解决方案1】:

您的 name 变量在 Pet 类中被定义为静态变量,这意味着它在所有 Pet 实例中都是相同的。删除静态修饰符,你应该会很好。

【讨论】:

  • 哦,谢谢我明白了!现在它工作正常谢谢:)
【解决方案2】:

所有宠物都同名,因为name 属性在Pet 类中是静态的。

每次name = petName; 行在Pet 构造函数上运行时,所有宠物的名字都会变成petName 的值。从属性声明中删除 static 关键字。

另外,我不禁注意到removeDogremoveCat 方法中的一个不相关的错误。他们将从 mainList 中删除具有给定名称的任何宠物,而我相信您打算 removeDog 仅删除狗,而 removeCat 删除猫。用户可以添加一只名为“Garfield”的猫,然后成功移除Garfield狗。

您可以通过在使用 instanceof 运算符检查宠物名称之前检查宠物是狗还是猫的实例来解决此问题。

public static void removeDog(String dogName)
{
    for(Pet pet : mainList)
        {
            if((pet instanceof Dog) && pet.getName().equals(dogName))
            {
                mainList.remove(pet);
            }//end of if statement
        }//end of for loop

}//end of removeDog method


public static void removeCat(String catName)
{
        for(Pet pet : mainList)
        {
            if((pet instanceof Cat) && pet.getName().equals(catName))
            {
                mainList.remove(pet);

            }//end of if statement

        }//end of for loop

}//end of removeCat method

【讨论】:

  • 但我最近意识到 removeDog 和 removeCat 方法中的错误我该如何解决?我试图将宠物分配给 mainIter.next() 但我仍然有异常......
  • 在检查宠物的名字之前使用if pet instance of Dog(或Cat)。我已经更新了我的答案以包含它。
  • 运算符为instanceof(无空格)
猜你喜欢
  • 2014-06-30
  • 1970-01-01
  • 2015-06-30
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
  • 1970-01-01
相关资源
最近更新 更多