gpt4 book ai didi

java - 如何在 Java 中将对象发送到套接字数组

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:44 26 4
gpt4 key购买 nike

例子:

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");
}

最佳答案

你需要使用泛型。

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

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

我假设列表中对象的类型是Socket。然后你需要在 for 循环中使用正确的类型:

for (Socket arraySocket1 : arraySocket) {

关于java - 如何在 Java 中将对象发送到套接字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com