How to send objects to socket arrays in Java

Example:

ArrayList arraySocket=new ArrayList();
...
arraySocket.add(client);
...
for (Object arraySocket1 : arraySocket) {
      DataOutputStream dataToClient = new DataOutputStream(arraySocket1.getOutputStream()); // it's wrong. But i don't kNow how to correct it.
      dataToClient.writeBytes(cData.getData()+"\n");
}

Solution

You need to use generics

ArrayList<Socket> arraySocket = new ArrayList<>(); // works in Java 7 and above

ArrayList<Socket> arraySocket = new ArrayList<Socket>(); // for versions 6 and below

I assume that the object type in the list is socket Then you need to use the correct type in the for loop:

for (Socket arraySocket1 : arraySocket) {
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>