简体   繁体   English

在ArrayList中存储不同类的对象

[英]Storing objects of different classes inside an ArrayList

I have a abstract animal class and other subclass like reptiles which is further inherited. 我有一个抽象的动物类和其他子类,例如爬行动物,它们进一步被继承了。
I have created array to initialize the animal as shown below: 我创建了一个数组来初始化动物,如下所示:

public void initializeArray()
{
    zooAnimal = new Animal[10];         // We will make 10 animals:

    // Polymorphism allows us to assign an object of a subclass to an 
    // object reference to a superclass/ancestor.
    zooAnimal[0] = new Kookaburra("Kelly",5);       // A 5kg kookaburra
    zooAnimal[1] = new Lizard("Lizzy",2,3);         // A 2kg, 3-year-old lizard
    zooAnimal[2] = new Crocodile("Colin", 200, 7);      // a 7-yo 200kg croc.
    zooAnimal[3] = new Rosella("Katie", 2, "Crimson");      // a 2-yo    Crimson Rosella
    zooAnimal[4] = new Rosella("Rosie", 4, "Green");        // a 4-yo Green Rosella
    zooAnimal[5] = new Snake("Boris","Brown",15,3); // A Brown Snake, 15kg, 3 years
    zooAnimal[7] = new Snake("Rita","Rattle",22,1); // A Rattle Snake, 22kg, 1 years
    zooAnimal[6] = new Dolphin("Dolly", 142, 6);    // A heavy, 6-yo dolphin.
    zooAnimal[8] = new Kookaburra("Kenneth",4);     // A 4kg kookaburra
    zooAnimal[9] = new Rosella("Yippy", 1, "Yellow");       // a 1-yo Yellow Rosella
}

But I want to achieve the same using an ArrayList instead of array. 但是我想使用ArrayList而不是array实现相同的目的。
How this can be done? 如何做到这一点?

My Animal class and sub-classes look like this: 我的动物类和子类如下所示:

Animal class 动物类

public abstract class Animal
{
    private int weight;
    private int age;
    private String name;

    protected Animal(String name, int weight, int age)
    {
        this.name = name;
        this.weight = weight;
        this.age = age;
    }

    public final int getWeight() { return weight; }

    public final int getAge() { return age; }

    public final String getName() { return name; }

    public abstract void makeNoise();       // Must be implemented by a subclass

    /** Provides a default description of the animal.
    * Sub-classes should override. */
    public String toString()
    {
        return "Animal Object: [name=" + name + ", age=" + age + ", weight=" + weight + "]";
    }
}

And I have a Bird class (sub-class of Animal class), a Kookabura class (sub-class of Animal ), Reptile class (sub-class of Animal class) and a Lizard subclass (sub-class of Reptile class) and so on!! 我有一个Bird类( Animal类的子类), Kookabura类( Animal子类), Reptile类( Animal类的子类)和Lizard子类( Reptile类的子类)等上!!

You just need to declare an ArrayList<Animal> , and use the add(Animal) method instead of assignments (polimorphism allows you to do so): 您只需要声明一个ArrayList<Animal> ,并使用add(Animal)方法代替赋值(策略性允许您这样做):

private ArrayList<Animal> zooAnimals;

public void initializeArray() {
    // this 10 is optional, but it's good to specify it 
    // when you know the final length of your list in advance
    zooAnimals = new ArrayList<>(10);

    zooAnimals.add(new Kookaburra("Kelly", 5)); // A 5kg kookaburra
    zooAnimals.add(new Lizard("Lizzy", 2, 3)); // A 2kg, 3-year-old lizard
    zooAnimals.add(new Crocodile("Colin", 200, 7)); // a 7-yo 200kg croc.
    zooAnimals.add(new Rosella("Katie", 2, "Crimson")); // a 2-yo    Crimson Rosella
    zooAnimals.add(new Rosella("Rosie", 4, "Green")); // a 4-yo Green Rosella
    zooAnimals.add(new Snake("Boris", "Brown", 15, 3)); // A Brown Snake, 15kg, 3 years
    zooAnimals.add(new Snake("Rita", "Rattle", 22, 1)); // A Rattle Snake, 22kg, 1 years
    zooAnimals.add(new Dolphin("Dolly", 142, 6)); // A heavy, 6-yo dolphin.
    zooAnimals.add(new Kookaburra("Kenneth", 4)); // A 4kg kookaburra
    zooAnimals.add(new Rosella("Yippy", 1, "Yellow")); // a 1-yo Yellow Rosella
}

You can do like this: 您可以这样:

public void initializeList() {
        List<Animal> animals = new ArrayList<Animal>();
        animals.add(new Kookaburra("Kelly",5));
        animals.add(new Lizard("Lizzy",2,3));
        animals.add(new Crocodile("Colin", 200, 7));
        animals.add(new Rosella("Katie", 2, "Crimson"));
        animals.add(new Rosella("Rosie", 4, "Green"));
        animals.add(new Snake("Boris","Brown",15,3));
        animals.add(new Snake("Rita","Rattle",22,1));
        animals.add(new Dolphin("Dolly", 142, 6));
        animals.add(new Kookaburra("Kenneth",4));
        animals.add(new Rosella("Yippy", 1, "Yellow"));
    }

The java API provides special class to store and manipulate group of objects. Java API提供了特殊的类来存储和操作对象组。 One Such Class is Arraylist 这样的类就是Arraylist

Note that Arraylist class is in java.util.ArrayList 请注意,Arraylist类位于java.util.ArrayList中

Create an ArrayList as you would any objects. 就像创建任何对象一样,创建一个ArrayList。

import java.util.ArrayList;
//..
ArrayList ajay = new ArrayList();

Here 这里

  • ArrayList -> Class ArrayList->类

  • ajay -> object 阿贾伊->对象

You can optionally specify the capacity and type of object the ArrayList will hold on: 您可以选择指定ArrayList将保留的对象的容量和类型:

  ArrayList ajay<String> = new ArrayList<String>(10);

The ArrayList provides various methods for manipulating objects.The add() method adds object to list and The remove() method removes object from the list. ArrayList提供了各种操作对象的方法。add ()方法将对象添加到列表中,而remove()方法将对象从列表中删除。

Sample code: 样例代码:

import java.util.ArrayList;

public class MyClass {
    public static void main(String[ ] args) {
        ArrayList<String> ajay = new ArrayList<String>();
        ajay.add("Red");
        ajay.add("Blue");
        ajay.add("Green");
        ajay.add("Orange");
        ajay.remove("Green");

        System.out.println(colors);
}
}

I recommend you for the Solution of your Code has been, 我建议您使用代码解决方案,

public void initializeList() {
        List<Animal> animals = new ArrayList<Animal>();
        animals.add(new Kookaburra("Kelly",5));
        animals.add(new Lizard("Lizzy",2,3));
        animals.add(new Crocodile("Colin", 200, 7));
        animals.add(new Rosella("Katie", 2, "Crimson"));
        animals.add(new Rosella("Rosie", 4, "Green"));
        animals.add(new Snake("Boris","Brown",15,3));
        animals.add(new Snake("Rita","Rattle",22,1));
        animals.add(new Dolphin("Dolly", 142, 6));
        animals.add(new Kookaburra("Kenneth",4));
       animals.add(new Rosella("Hippy",1,"Yellow");
}

In your Code, Animal is a class and animals is an object of a class.. 在代码中, 动物是一类动物是一个类的对象..

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

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