GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes What type should i cast? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What type should i cast?" Watch "What type should i cast?" New topic
Author

What type should i cast?

Ugluth Papadopoulos
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Hello, I have been reading the head first java book for some time and just recently i stumbled upon a problem with the ready baked code.
It concerns chapter 15 with the chat application. I have created an ArrayList clientOutputStreams; as the book suggested but then i get this error:
VerySimpleChatServer.java:43: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
clientOutputStreams.add(writer);

I think the solution to this problem is properly casting the arraylist to a type. I am not very sure if that is the problem, and even if it is, I'm not very sure on how to fix it.
Anyway here's the code I have:


If somebody could help out with this one I would be really thankful. Thank you in advance
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Try changing the ArrayList type in lines 6 and 36 to ArrayList<PrintWriter>
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Hi Ugluth. Welcome to The Ranch!

You're getting the warning because (as of Java 5) ArrayList takes a generic type, but you aren't using one. It will still work without one, but you lose the type safety that generics provide.

See the Java Generics Tutorial for more information if you haven't come across them before. Generally speaking, you should always use generics except in the rare situation where you have to interact with legacy libraries that don't use them.

Luigi's told you part of what you need to do to use them. There's another step as well: line 53 needs to contain Iterator<PrintWriter>. Once you've gone that, you can remove the cast from line 56 - the cast becomes unnecessary because the Iterator now just returns PrintWriter references.
Ugluth Papadopoulos
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Thank you very much for your replies they really were helpful

Here's the changes i made in case someone else ever comes up with this problem:
Line 6: ArrayList<PrintWriter> clientOutputStreams;
Line 36: clientOutputStreams = new ArrayList<PrintWriter>();
Line 53: Iterator<PrintWriter> it = clientOutputStreams.iterator();

Thank you very much for your help, I somehow understood more or less what I did there, even though i need some more practice with java.

Anyway thank you very much once again
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You're welcome!

If you give your Iterator a generic type, you can remove the (PrintWriter) cast in line 56, because the generic type ensures that it contains only PrintWriters.

You could also get rid of the Iterator altogether with an enhanced-for loop:

 
GeeCON Prague 2014
 
subject: What type should i cast?