Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

ArrayList questions

Thread view: 
Joe Fox - 15 Jun 2006 11:34 GMT
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!
TechBookReport - 15 Jun 2006 12:47 GMT
> 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
[quoted text clipped - 27 lines]
> 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...
chris brat - 15 Jun 2006 13:14 GMT
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();

?

> > 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
[quoted text clipped - 35 lines]
> 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...
Joe Fox - 15 Jun 2006 14:38 GMT
> Surely that should be:
>
[quoted text clipped - 3 lines]
> 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.
Thomas Hawtin - 15 Jun 2006 13:02 GMT
> 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
[quoted text clipped - 8 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Joe Fox - 15 Jun 2006 14:47 GMT
> Unemployed English Java programmer
> http://jroller.com/page/tackline/

Well, if I lived in Britain, and had a job for you, *I'd* hire you.
Alex Whitney - 15 Jun 2006 13:04 GMT
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

> 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
[quoted text clipped - 27 lines]
> seems to me that they should? I'm only a newbie, and don't really know
> anything, so any help is appreciated. Thanks!
Joe Fox - 15 Jun 2006 14:48 GMT
> More info here: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Excellent link, very explanatory.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.