Reputation: 1941
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? 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.
Upvotes: 1
Views: 2000
Reputation: 31339
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:
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:
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:
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.
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.
Upvotes: 2
Reputation: 15357
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
List<IAnimal> animals = new YourList<IAnimal>();
Upvotes: 1
Reputation: 26054
You only can add elements that implements
same interface or extends
same class.
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());
Upvotes: 1
Reputation: 916
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());
Upvotes: 0
Reputation: 62864
If Cat
and Dog
have nothing in common, you can parameterize the LinkedList
with Object
:
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.
Upvotes: 1