• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What type should i cast?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try changing the ArrayList type in lines 6 and 36 to ArrayList<PrintWriter>
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
reply
    Bookmark Topic Watch Topic
  • New Topic