【问题标题】:OOP-Java. Adding Animal objects to an Arraylist in the Class from the main面向对象Java。从主目录将 Animal 对象添加到 Class 中的 Arraylist
【发布时间】:2018-09-21 20:52:40
【问题描述】:

添加第二个动物,将动物 Arraylist 重置为索引 0。因此它保存最后输入的动物。我还将 UML 设计放在下面,所以我正在尝试的内容可能更容易理解。

public class Cage {
    private int placeNr;
    private ArrayList<Animal> animals = new ArrayList<Animal>();

    public Cage(int place) {
        this.placeNr = place;
    }

    public int getCageNr() {
        return this.placeNr;
    }


    public void putAnimal(Animal animal) throws DuplicateNameException {
        if(!duplicatedAnimal(animal.getName())) {
            animals.add(animal);
        }
    }
}

此时我很困惑。如何在位于 Cage 的 Arraylist 中添加 Animal 类并将我的 Animals 堆叠在其中。

private static void addAnimal() {
    Animal newAnimal;
    Cage cage;
    try {
        System.out.println("Name: ");
        String name = sc.next();
        System.out.println("Type: ");
        String type = sc.next();
        System.out.println("birthYear: ");
        int birthYear = sc.nextInt();
        System.out.println("Cage number: ");
        int cageNumber = sc.nextInt();

        cage = new Cage(cageNumber);
        newAnimal = new Animal(name, type, birthYear);
        cage.putAnimal(newAnimal);
    } catch (DuplicateNameException dne) {
        dne.getMessage();
    } catch (InputMismatchException ime) {
        System.out.println("Wrong input");
    }
}

如果有人能把我推向正确的方向,那就太棒了!

UML: enter image description here

【问题讨论】:

  • 您面临的错误/问题到底是什么?
  • "如何在 Arraylist 中添加 Animal 类" 你做 not 添加 class @987654325 @ 但它的 instaces
  • cage.putAnimal(newAnimal); 会报错吗?不良行为?您的帖子根本不清楚...
  • "添加第二个 Animal,将 animals Arraylist 重置为索引 0" - 请提供 MCVE。从您目前所展示的情况来看,这不应该发生。
  • 方法 duplicatedAnimal 看起来如何?

标签: java oop object arraylist


【解决方案1】:

每次调用 addAnimal 时,都会创建一个新的 Cage 对象并将新动物添加到该笼子中。要将多个动物添加到同一个笼子中,addAnimal 方法不能是静态的,笼子需要是某个类的成员(AnimalKeeper?)

像这样的

public class AnimalKeeper {
  private Cage cage;

  public Cage getCage() {
      return cage;
  }

  public addAnimalToCage(Animal animal) {
      //the same method as above minus the user input
      //which needs to be done somewhere else
  }
}

【讨论】:

  • 谢谢,我试试看:)
【解决方案2】:

我尽力弄清楚你的意思,把你的代码重构成这样

class DuplicateNameException extends Exception {

    private String message;

    public DuplicateNameException(String message){
        this.message = message;
    }

    public String getMessage(){
        return this.message;
    }

}

class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


class Cage {
    private int placeNr;

    private ArrayList<Animal> animals = new ArrayList<Animal>();

    public Cage(int place) {
        this.placeNr = place;
    }

    public int getCageNr() {
        return this.placeNr;
    }


    public void putAnimal(Animal animal) throws DuplicateNameException {
        if(!duplicatedAnimal(animal.getName())) {
            animals.add(animal);
        } else {
            throw new DuplicateNameException(animal.getName());
        }
    }

    private boolean duplicatedAnimal(String name) {
        for (Animal animal : animals)
            if (animal.getName().equals(name))
                return true;

        return false;
    }

    public static void main(String[] args) {
        Animal newAnimal;
        Cage cage;
        try {
            cage = new Cage(1);
            newAnimal = new Animal("Dog");
            cage.putAnimal(newAnimal);
            cage.putAnimal(new Animal("not dog"));
            cage.putAnimal(new Animal("Dog"));
        } catch (DuplicateNameException dne) {
            System.out.println(dne.getMessage());
        }
    }
} 

注意:使用此代码,您需要考虑动物名称以小写和大写形式编写的情况(equals 在这种情况下将返回false)使用toLowerCase()toUpperCase() 将名称标准化为常见地面

您可以采取的另一个选择是使用HashMap 对象,这种结构类型具有内置的重复消除功能,您必须覆盖AnimalhashCode() 函数,因此map 将知道如何散列每个对象,如

@Override
public int hashCode() {
  return this.name.hashCode()
}

【讨论】:

  • 我的代码看起来对这个概念很熟悉。但我的主要方法位于 Zoo.java 类中。如果我是对的,这 (cage = new Cage(1)) 会将所有动物放在同一个笼子里,对吗?学校问的问题是这样的。 getAnimal 返回具有特定名称的 Animal,如果这样的 Animal 不在 Cage 中,则返回 null。
  • @C.Khan cage = new Cage(1) 这一行创建一个新笼子并将其分配给数字 1,您使用 cage.putAnimal(...) 添加的所有动物都将添加到此笼子中,我没有实现 @987654333 @因为我没有在您提供的代码中看到它...但是您可以相当容易地实现它您的评论中还有其他问题吗?
猜你喜欢
  • 2021-03-19
  • 2013-10-29
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 2013-10-30
  • 2017-12-12
  • 2016-06-28
相关资源
最近更新 更多