ArrayList questions

J

Joe Fox

I'm reading a book called "Head First Java," and there is an exercise
in the book (p. 576) which asks if some code will compile, and there
appears to be some errors in the given answers, because my compiler
(Eclipse) doesn't agree with the book's answers (The answers assume a
5.0 compiler, which is in fact what I'm using.). I'm interested in
getting feedback from others about whether the book's answers are
incorrect or Eclipse is somehow wrong.

The questions are based on the following classes:
abstract class Animal {
// irrelevant
}
class Dog extends Animal {
// irrelevant
}

The book claims that the following are all legal declarations; Eclipse
seems to think that none of them are:
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog> ();
ArrayList<Object> animals = new ArrayList<Dog> ();

I find this confusing, because I thought that the second two should
compile but the first one shouldn't; Eclipse won't let any of them
compile; and the book says all of them should compile. Sigh. It
shouldn't be this difficult.

So, there are two issues here: 1) why is the book disseminating
incorrect information, and 2) why don't the last two compile when it
seems to me that they should? I'm only a newbie, and don't really know
anything, so any help is appreciated. Thanks!
 
T

TechBookReport

Joe said:
I'm reading a book called "Head First Java," and there is an exercise
in the book (p. 576) which asks if some code will compile, and there
appears to be some errors in the given answers, because my compiler
(Eclipse) doesn't agree with the book's answers (The answers assume a
5.0 compiler, which is in fact what I'm using.). I'm interested in
getting feedback from others about whether the book's answers are
incorrect or Eclipse is somehow wrong.

The questions are based on the following classes:
abstract class Animal {
// irrelevant
}
class Dog extends Animal {
// irrelevant
}

The book claims that the following are all legal declarations; Eclipse
seems to think that none of them are:
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog> ();
ArrayList<Object> animals = new ArrayList<Dog> ();

I find this confusing, because I thought that the second two should
compile but the first one shouldn't; Eclipse won't let any of them
compile; and the book says all of them should compile. Sigh. It
shouldn't be this difficult.

So, there are two issues here: 1) why is the book disseminating
incorrect information, and 2) why don't the last two compile when it
seems to me that they should? I'm only a newbie, and don't really know
anything, so any help is appreciated. Thanks!
Surely that should be:

ArrayList<? extends Animal> animals1 = new ArrayList<Dog>();
ArrayList<? extends Object> animals = new ArrayList<Dog>();

I don't have my copy of the book to hand, so can't check. There's
nothing on the errata page listed at the O'Reilly site. Strange...
 
T

Thomas Hawtin

Joe said:
I'm reading a book called "Head First Java," and there is an exercise
in the book (p. 576) which asks if some code will compile, and there

I don't see anything in the errata about that (although it is an errata
for an unspecified print). Sure you have read it correctly?

http://www.oreilly.com/catalog/hfjava/errata/hfjava.confirmed
abstract class Animal {
// irrelevant
}
class Dog extends Animal {
// irrelevant
}

The book claims that the following are all legal declarations; Eclipse
seems to think that none of them are:
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog> ();
ArrayList<Object> animals = new ArrayList<Dog> ();

None of them should compile. ArrayList<Dog> is not a type of
ArrayList<Object>. I could add a Cat to ArrayList<Object>. If that
ArrayList<Object> was in fact an ArrayList<Dog>, then I would have a Cat
that was a Dog.

You can use wildcards:

ArrayList<? extends Animal> animals1 = new ArrayList<Dog> ();
ArrayList<?> animals = new ArrayList<Dog> ();

Wildcarded in that way, you can get, remove and query the list, but not add.

Tom Hawtin
 
A

Alex Whitney

1) I don't have an answer. It was probably written without complete
understanding of the topic.

2) Even though Object and Animal are supertypes of Dog, the
Collection<Object> and Collection<Animal> are not. Having a list of
Animals is not the same thing as having a list of Dogs, even though
the dogs may be animals. It is counter-intuitive, but the confusion is
needed in order preserve safety.

More info here: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
 
C

chris brat

Following your directions I changed the examples as follows :

ArrayList<? extends Animal> dogs1 = new ArrayList<Animal>();
ArrayList<? extends Animal> animals1 = new ArrayList<Dog> ();
ArrayList<? extends Object> animals = new ArrayList<Dog> ();

The compiler seems happy about it too.



What is the difference between

ArrayList<? extends Animal> animals1 = new ArrayList<Dog> ();

and

List<Animal> moreAnimals = new ArrayList();


?
 
J

Joe Fox

Surely that should be:

ArrayList<? extends Animal> animals1 = new ArrayList<Dog>();
ArrayList<? extends Object> animals = new ArrayList<Dog>();

I don't have my copy of the book to hand, so can't check. There's
nothing on the errata page listed at the O'Reilly site. Strange...

I copied the code from the book exactly for this post. That's why it
was all so strange to me. I didn't think they would make mistakes like
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top