序列化和 RMI
我遇到以下问题:
class POJO implements Serializable{
int[] a = new int[2];
int b;
}
服务器:
printThere(ArrayList<POJO> list) throws RemoteException{
for(POJO obj:list)
System.out.println(obj.a[0]+" "+obj.a[1]+" "+obj.b);\\<-----THERE LINE
}
客户端:
main(){
POJO o1 = new POJO();
o1.a[0] =1;
o1.a[1] =2;
o1.b= 11;
POJO o2 = new POJO();
o2.a[0] =10;
o2.a[1] =20;
o2.b= 111;
ArrayList<POJO> list = new ArrayList<POJO>();
list.add(o1);
list.add(o2);
for(POJO obj:list)
System.out.println(obj.a[0]+" "+obj.a[1]+" "+obj.b);\\<-----HERE LINE
server = proxy.lookup("server");
server.printThere(list);
}
这里的行打印如下:
1 2 11
10 20 111
那里的行打印如下:
1 2 111
10 20 111
为什么我会丢失一个对象中 b
的值?我尝试在远程方法中使用 Vector
和 POJO[]
但得到了相同的结果。
I have the following problem:
class POJO implements Serializable{
int[] a = new int[2];
int b;
}
Server:
printThere(ArrayList<POJO> list) throws RemoteException{
for(POJO obj:list)
System.out.println(obj.a[0]+" "+obj.a[1]+" "+obj.b);\\<-----THERE LINE
}
Client:
main(){
POJO o1 = new POJO();
o1.a[0] =1;
o1.a[1] =2;
o1.b= 11;
POJO o2 = new POJO();
o2.a[0] =10;
o2.a[1] =20;
o2.b= 111;
ArrayList<POJO> list = new ArrayList<POJO>();
list.add(o1);
list.add(o2);
for(POJO obj:list)
System.out.println(obj.a[0]+" "+obj.a[1]+" "+obj.b);\\<-----HERE LINE
server = proxy.lookup("server");
server.printThere(list);
}
Here the lines print as follows:
1 2 11
10 20 111
There the lines print as follows:
1 2 111
10 20 111
Why am I losing the value of b
in one object? I tried using a Vector<POJO>
and a POJO[]
in the remote method but got the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到问题所在。也就是说,我认为您很快就能弄清真相。我的建议是:
逐渐省略程序中的某些部分。每个步骤之后运行代码并查看问题是否仍然存在。每个这样的“实验”都会为您提供有关问题原因的有价值的详细信息。最终您希望能够检测代码中的哪个元素导致了问题。
以下是在此过程中需要采取的一些步骤:
o2
,是否会出现问题?o2
但未将其添加到list
中,是否会出现问题?POJO
类中删除a
字段,是否会出现问题?a
更改为单元素数组 (a = new int[1]
) 是否会出现问题?a
从数组更改为(例如)整数,是否会出现问题?b
的类型从 int 更改为其他基元,是否会出现问题?b
的类型从 int 更改为其他某种非基本类型(例如:String
或java.util.Date)?
o1
和o2
的顺序(即先 add(o2) 然后 add(o1)),是否会出现问题?通常,在应用诸如此类的几个“转换”步骤之后,我会收集足够的信息来确定根本原因。最重要的是,我尝试缩小范围。如果我发现即使省略
o2
也会出现问题,那么我会保留它,继续使用更容易理解的较小程序。I don't see the problem. That said, I think you will be able to get to the bottom of it quite quickly. Here's my suggestion:
Gradually omit pieces from your program. After each step run the code and see if the problem persists. Each such "experiment" will give you valuable details about the cause of the problem. Eventually you want to be able to detect what element in your code is causing the problem.
Here are some steps to take along the way:
o2
from your code?o2
but do not add it tolist
?a
field from thePOJO
class?a
into a single element array (a = new int[1]
)?a
from an array to a (say) Integer?b
's type from int to some other primitive?b
's type from int to some other non-primitive type (say:String
orjava.util.Date
)?o1
ando2
inside the list (i.e., add(o2) and then add(o1)) ?POJO
parameter (instead of a single ArrayList)?Usually, after applying several "transformation" steps such as these, I gather enough information to identify the root cause. On top of that I try to reduce the scope. If I find that the problem occurs even if
o2
is omitted then I keep it away, proceeding with a smaller program that is more easy to understand.我认为 b 的值会被覆盖。
试试这个代码:
I think that the value of b would be overwritten.
try this code :