Java internal class this $0 field produces a bug

Source: Internet
Author: User

First, check the following code. I pointed out the problem code. The reader first thinks about the problem with this code. This is the code that uses the clone method to completely copy a binary Heap Structure. The two-item heap contains an internal class BinomialHeapEntry. The object of this internal class is each node in the Two-item heap. Besides containing the key word corresponding to the node, it also records the parent node parent, the next sibling node sibling and the first child node child are three pointers. The root table of the two items heap is linked through the sibling pointer of each two item root node. The cloneBinomialTree (BinomialHeapEntry root) method Recursively copies the entire two-item tree under a root node (including the root node). The clone method traverses each root node in the root table and copies the corresponding two-item tree, then assign the new head pointer to the new heap after the copy. Public class BinomialHeap <E> implements Cloneable {transient BinomialHeapEntry head; // The first element int size in the root table; // the size of the entire two-item heap (number of knots) private class BinomialHeapEntry {E element; transient BinomialHeapEntry parent = null, child = null, sibling = null; transient int degree = 0; private BinomialHeapEntry (E element) {this. element = element;} // get the child node's iterator private Iterable <BinomialHeapEntry> children {...}} @ Override public BinomialHeap <E> clone () {// TODO Auto-generated method stub BinomialHeap <E> clone; try {clone = (BinomialHeap <E>) super. clone ();} catch (CloneNotSupportedException e) {// TODO Auto-generated catch block e. printStackTrace (); throw new InternalError ();} BinomialHeapEntry newHead = null, curRoot = null; // traverse the root table Iterator <HeapEntry <E> rootIt = this. roots (). iterator (); while (roo TIt. hasNext () {BinomialHeapEntry root = (BinomialHeapEntry) rootIt. next (); // copy the entire binary tree under the root node <br> <span style = "color: # ff0000; ">/// where the BUG is introduced </span> BinomialHeapEntry newRoot = cloneBinomialTree (root); if (curRoot = null) {newHead = newRoot;} else {curRoot. sibling = newRoot;} curRoot = newRoot;} clone. head = newHead; return clone;} // copy the entire subtree private BinomialHeapEntry cloneBinomialTree (BinomialHeapEntry root) {BinomialHeapEntry newRoot = new BinomialHeapEntry (root. element (); BinomialHeapEntry curChild = null; // traverse all child nodes of the root node Iterator <HeapEntry <E> childIt = root. children (). iterator (); while (childIt. hasNext () {BinomialHeapEntry child = (BinomialHeapEntry) childIt. next (); // recursively copy the subtree of the child node BinomialHeapEntry newChild = cloneBinomialTree (child); newChild. parent = newRoot; if (curChil D = null) {newRoot. child = newChild;} else {curChild. sibling = newChild;} curChild = newChild;} newRoot. degree = root. degree; return newRoot;} first, let's review some knowledge about Java internal classes. Non-static Java internal classes, as a member of an external class, can only be accessed through external class objects, to create an internal class object, you also need to create it through an external class object, that is, outerObject. new InnerClass (). At this time, the created internal class object will generate a field named this $0, which stores the reference of the external class object that created the internal class object, that is, the outerObject just now. This reference allows an internal class object to freely access the member variables of the external class. Looking back at the above two heap copy codes, when calling the cloneBinomialTree method to copy the two trees, we try to create a new node through new BinomialHeapEntry, the new node is used as the node in the new two-item heap, but in fact we actually call this. new BinomialHeapEntry (), that is, in the newly created node, this $0 points to the old two-item heap (this) instead of the new two-item heap (clone) being copied ). This allows us to obtain the new two items heap. When we access and modify the information of the entire heap through any node of the new two items heap, the modified information is the old two items heap, finally, a series of strange results were generated. To fix this bug, modify BinomialHeapEntry newRoot = cloneBinomialTree (root) in the clone method to BinomialHeapEntry newRoot = clone. cloneBinomialTree (root ). In this way, all the nodes created in the cloneBinomialTree method are created through clone objects, and the problem is solved. The problem is actually relatively simple, which is indeed worth noting in future programming: when copying an internal class object, consider whether the Copied object this $0 points to the external class object you want to point.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.