This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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
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.
Joined: Apr 15, 2011
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.