简体   繁体   中英

How to swap fields of 2 objects in java?

I have two objects of class TreeNode. Those are called a and b with the following fields:

value
index
parent
leftChild 
rightChild

I want to create a function

swap(a,b)

That swaps fields of a to fields of b and vice-versa.

I tried to create such function but It's been rather lengthy and inefficient since the values don't get changed as expected. Any easy ways to do this ?

I assume that the types of the fields are:

  • value is String
  • index is int
  • parent is TreeNode
  • leftChild is TreeNode
  • rightChild is TreeNode


So use the classic method of swapping values for each of the fileds:

public static void swap(TreeNode a, TreeNode b) {
    String tempValue = a.value;
    a.value = b.value;
    b.value = tempValue;

    int tempIndex = a.index;
    a.index = b.index;
    b.index = tempIndex;

    TreeNode tempNode = a.parent;
    a.parent = b.parent;
    b.parent = tempNode; 

    tempNode = a.leftChild;
    a.leftChild = b.leftChild;
    b.leftChild = tempNode;    

    tempNode = a.rightChild;
    a.rightChild = b.rightChild;
    b.rightChild = tempNode;    
}

Copy values from b to temporary variables, set values from a to b , copy values from temporary variables to a . That's all.

In Java, you can't really do that (at least if you mean your question literally).

What's your situation? Something similar to:

    // This is the aTreeNode instance.
    TreeNode a = new TreeNode("ValueA", 4711, parentA, leftChildA, rightChildA);

    // This is the aTreeNode instance.
    TreeNode b = new TreeNode("ValueB", 4712, parentB, leftChildB, rightChildB);

You have two variables a and b , referencing each a TreeNode instance (which I called aTreeNode and bTreeNode , just to give them names I can talk about). And these instances have different values in their fields. Let's depict it:

    a -> aTreeNode instance
         ------------------
         "ValueA"
         4711
         -> parentA instance
         -> leftChildA instance
         -> rightChildA instance

    b -> bTreeNode instance
         ------------------
         "ValueB"
         4712
         -> parentB instance
         -> leftChildB instance
         -> rightChildB instance

If you call a method like swap(a, b) , the method gets the references to the aTreeNode and the bTreeNode instances. As the method doesn't know where these references came from (in our case, the variables a and b , but it might as well be an array element or the result of some other method call), the best thing it can do is to swap the contents inside these instances:

    a -> aTreeNode instance
         ------------------
         "ValueB"
         4712
         -> parentB instance
         -> leftChildB instance
         -> rightChildB instance

    b -> bTreeNode instance
         ------------------
         "ValueA"
         4711
         -> parentA instance
         -> leftChildA instance
         -> rightChildA instance

So, after the swap(a, b) call, a still references the aTreeNode instance, and b the bTreeNode , they only swapped their contents. And such radical changes of the contents of an instance don't fit well into the Java philosophy, bringing you into trouble with many library classes, eg all kinds of collections and maps.

So, you'd better swap the two variables without a method call:

    TreeNode a = new TreeNode("ValueA", 4711, parentA, leftChildA, rightChildA);
    TreeNode b = new TreeNode("ValueB", 4712, parentB, leftChildB, rightChildB);

    TreeNode temp = a;
    a = b;
    b = temp;

It's much faster, cleaner, and the result is:

    a -> bTreeNode instance
         ------------------
         "ValueB"
         4712
         -> parentB instance
         -> leftChildB instance
         -> rightChildB instance

    b -> aTreeNode instance
         ------------------
         "ValueA"
         4711
         -> parentA instance
         -> leftChildA instance
         -> rightChildA instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM