简体   繁体   English

通用链表-如何添加不同的对象

[英]Generic linked list - how to add different objects

I have created a linked list, but I can't seem to understand how to insert different objects into the same list. 我已经创建了一个链表,但似乎无法理解如何将不同的对象插入同一链表。 Let's say I have a list which I want to contain both objects of type cat and dog, is this possible? 假设我有一个要包含cat和dog类型的对象的列表,这可能吗? I know how to insert only cats, or only dogs, but I can't seem to understand how to insert both into the same list. 我知道如何只插入猫,也只能插入狗,但是我似乎无法理解如何将它们都插入同一列表。

You want to use their "lowest" common class, or a common interface, so you can tell as much as possible (and what you need) about list's elements. 您想使用它们的“最低”公共类或公共接口,以便可以尽可能多地(以及您需要)有关列表元素的信息。

If both classes Dog and Cat extend the class Animal , and you want to represent a list of animals, you can do: 如果DogCat 扩展了 Animal ,并且您想表示动物列表,则可以执行以下操作:

List<Animal> animals = new YourList<Animal>();
for (Animal animal: animals){
    // use some Animal method
    animal.eat();
}

If both of them implement the interface TailOwner and you want to use operations from that interface on members of the list, you can do: 如果它们两个都实现了接口 TailOwner并且您想在列表的成员上使用该接口上的操作,则可以执行以下操作:

List<TailOwner> tailOwners = new YourList<TailOwner>();
for (TailOwner tailOwner: tailOwners){
    // use some TailOwner method
    tailOwner.wiggle();
}

If you're not sure, you can always fall back to Object , as all Java classes extend it: 如果不确定,可以随时使用Object ,因为所有Java类都对其进行了扩展:

List<Object> objects = new YourList<Object>();
for (Object object: objects){
    // use some Object method
    System.out.println(tailOwner.toString());
}

Usually, when you have two objects in the same list, they do have something in common. 通常,当您在同一列表中有两个对象时,它们确实有一些共同之处。

Either way, you're giving up any Cat -specific or Dog -specific methods (before casting back to either) when you put them in the list. 无论哪种方式,当您将它们放入列表中时,都将放弃任何特定于Cat或特定于Dog方法(在强制返回之前)。

Having a good hierarchy can prevent casting, and promote polymorphism, when you use objects from the list. 使用列表中的对象时,具有良好的层次结构可以防止强制转换并促进多态。 All you can tell about objects retrieved from the list at compile time, is that they at least comply to the list's generic type. 关于编译时从列表中检索到的对象,您所能知道的就是它们至少符合列表的泛型类型。

If Cat and Dog have nothing in common, you can parameterize the LinkedList with Object : 如果CatDog没有什么共同点,则可以使用Object参数化LinkedList

List<Object> list = new LinkedList<Object>();

This way you will be able to add both Cat and Dog , but please note that this may force you to check the types every time you retrieve the objects from the list. 这样,您就可以同时添加CatDog ,但是请注意,这可能会迫使您每次从列表中检索对象时都要检查类型。

You only can add elements that implements same interface or extends same class. 您只能添加implements相同接口或extends相同类的元素。

class Animal {}

class Cat extends Animal{}

class Dog extends Animal{}

List<Animal> animals = new LinkedList<Animal>;
animals.add(new Cat());
animals.add(new Dog());

OR 要么

interface Animal {}

class Cat implements Animal{}

class Dog implements Animal{}

List<Animal> animals = new LinkedList<Animal>;
animals.add(new Cat());
animals.add(new Dog());

Since your list probably contains elements that are somehow related, it would be best to make an interface and use that as list object type. 由于您的列表可能包含某种程度上相关的元素,因此最好创建一个接口并将其用作列表对象类型。

Derive both Cat and Dog from IAnimal and make the list of type 从IAnimal派生Cat和Dog并列出类型

List<IAnimal> animals = new YourList<IAnimal>();

I would be cautious when using the word generic, especially since cat and dog are not generic. 在使用通用一词时,我会保持谨慎,尤其是因为猫和狗不是通用的。

LinkedList<Object> linkedlist = new LinkedList<Object>();
linkedList.add(new Dog());
linkedList.add(new Cat());

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

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