在套接字编程中将列表作为输入
我想接收一个列表作为输入,Java 没有显示任何错误:
List<String> A;
ObjectOutputStream ins = new ObjectOutputStream(soc.getOutputStream());
ins.writeObject(A);
上面的代码执行时没有错误,但是这不是:
List<String> ls;
ObjectInputStream in = new ObjectInputStream(soc.getInputStream());
in.readObject(ls);
如果我可以发送列表<>,为什么我不能接受它?输出代码有什么原因和建议吗?
I want to receive a List as input, Java is showing me no error in:
List<String> A;
ObjectOutputStream ins = new ObjectOutputStream(soc.getOutputStream());
ins.writeObject(A);
The above code executes without error, however this doesn't:
List<String> ls;
ObjectInputStream in = new ObjectInputStream(soc.getInputStream());
in.readObject(ls);
If I can send a List<>, why cant I accept it? Any reasons and suggestions for the output code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
readObject
不带任何参数。你想要:(当然,这并不是真正的类型安全,编译器至少应该发出警告。如何处理这是一个单独的问题。)
readObject
doesn't take any parameters. You want:(Of course, this isn't really type-safe, and the compiler should at least raise warnings. How you deal with that is a separate matter.)
没有 readObject 方法带输入参数。
您需要将 readObject 转换为您尝试读取的对象的类型。
There is no readObject method with input parameter.
You need to cast the readObject to type of the object you are trying to read.