开发者

Serialization and RMI

开发者 https://www.devze.com 2023-03-02 01:53 出处:网络
I have the following problem: class POJO implements Serializable{ int[] a = new int[2]; int b; } Server: printThere(ArrayList<POJO> list) throws RemoteException{

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);
   开发者_如何学JAVA 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.


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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消