序列化和 RMI

发布于 2024-11-04 01:29:03 字数 1031 浏览 4 评论 0原文

我遇到以下问题:

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 的值?我尝试在远程方法中使用 VectorPOJO[] 但得到了相同的结果。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-11-11 01:29:03

我没有看到问题所在。也就是说,我认为您很快就能弄清真相。我的建议是:

逐渐省略程序中的某些部分。每个步骤之后运行代码并查看问题是否仍然存在。每个这样的“实验”都会为您提供有关问题原因的有价值的详细信息。最终您希望能够检测代码中的哪个元素导致了问题。
以下是在此过程中需要采取的一些步骤:

  • 如果从代码中删除 o2 ,是否会出现问题?
  • 如果您创建 o2 但未将其添加到 list 中,是否会出现问题?
  • 如果从 POJO 类中删除 a 字段,是否会出现问题?
  • 如果将 a 更改为单元素数组 (a = new int[1]) 是否会出现问题?
  • 如果将 a 从数组更改为(例如)整数,是否会出现问题?
  • 如果将 b 的类型从 int 更改为其他基元,是否会出现问题?
  • 如果将 b 的类型从 int 更改为其他某种非基本类型(例如:Stringjava.util.Date)?
  • 如果更改列表中 o1o2 的顺序(即先 add(o2) 然后 add(o1)),是否会出现问题?
  • 如果您更改 printThere() 的签名,使其采用(例如)POJO 参数(而不是单个 ArrayList),是否会出现问题?
  • 如果更改打印中字段的顺序(例如,首先打印 b,然后打印 a[1],然后打印 a[0]),是否会出现问题?
  • ...

通常,在应用诸如此类的几个“转换”步骤之后,我会收集足够的信息来确定根本原因。最重要的是,我尝试缩小范围。如果我发现即使省略 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:

  • Does the problem occur if you remove o2 from your code?
  • Does the problem occur if you create o2 but do not add it to list?
  • Does the problem occur if you remove the a field from the POJO class?
  • Does the problem occur if you change a into a single element array (a = new int[1])?
  • Does the problem occur if you change a from an array to a (say) Integer?
  • Does the problem occur if you change b's type from int to some other primitive?
  • Does the problem occur if you change b's type from int to some other non-primitive type (say: String or java.util.Date)?
  • Does the problem occur if you change the order of o1 and o2 inside the list (i.e., add(o2) and then add(o1)) ?
  • Does the problem occur if you change the signature of printThere() such that it takes (say) to POJO parameter (instead of a single ArrayList)?
  • Does the problem occur if you change the order of fields in the print (e.g., first print b, then a[1], then a[0])?
  • ...

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.

执手闯天涯 2024-11-11 01:29:03

我认为 b 的值会被覆盖。

试试这个代码:

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;

I think that the value of b would be overwritten.

try this code :

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文