Search code examples
javaprintwritertype-mismatch

PrintWriter and LinkedList


I need to iterate through saved clients connections (on the server side) and send them messages (AS one people write a message - others connected should see it too).

I tried to do that:

for (PrintWriter out : connections) {
    out.println(message);
    out.flush();
}

connections is

LinkedList connections = new LinkedList();

But for the for loop I get the following error:

Type mismatch: cannot convert from element type Object to PrintWriter.

Could anyone help me or suggest another idea how to get done it. Thanks.


Solution

  • LinkedList connections = new LinkedList();
    

    is a raw type and thus it knows its elements as of the Type Object. You need to add a generic type paramter to it:

    LinkedList<PrintWriter> connections = new LinkedList<>();