Because I am writing a small tool, used to swing the JTree components, the node implementation class for Defaultmutabletreenode, but encountered a bit of a problem, and then look at the source code found the answer, I would like to record. The following is a piece of code designed for this problem:
public static void Main (string[] args) {
Defaultmutabletreenode root = new Defaultmutabletreenode ("root");//root node
defaultmutabletreenode persons = new Defaultmutabletreenode ("persons");//Level two node with four leaf nodes
//leaf
nodes under it Persons.add (New Defaultmutabletreenode ("Zhangsan"));
Persons.add (New Defaultmutabletreenode ("Lisi"));
Persons.add (New Defaultmutabletreenode ("Wangwu"));
Persons.add (New Defaultmutabletreenode ("Zhaoliu"));
Root.add (persons);
For example, there is a requirement that, after removing the level two node persons, place its child nodes on its parent node, which is also the root node
//Hence the following code
enumeration< Defaultmutabletreenode> enumeration = Persons.children ();
while (Enumeration.hasmoreelements ()) {
Defaultmutabletreenode next = enumeration.nextelement ();
System.out.println (next);//Just to make it easier to see how many child nodes were added, the result was only two times added, only to Zhangsan and Wangwu
Root.add (next);
}
Persons.removefromparent ()///Remove persons node from root child node
//Output result is 2,root only two child nodes
System.out.println ( Root.getchildcount ());
}
The strange thing is that there are 4 sub nodes clearly under the persons node, why only two are added to the iteration process. The answer is in the Defaultmutabletreenode Add method, to see the source code:
public void Add (Mutabletreenode newchild) {
if (newchild!= null && newchild.getparent () = = this)
insert ( Newchild, Getchildcount ()-1);
else
Insert (newchild, Getchildcount ());
In this method, when you get to the index that you want to insert, call the Insert method:
public void Insert (Mutabletreenode newchild, int childindex) {
if (!allowschildren) {
throw new IllegalStateException ("node does not allow children");
else if (newchild = = null) {
throw new illegalargumentexception ("New child is null");
} else if (Isnodeancestor (NE Wchild)) {
throw new illegalargumentexception ("New child being an ancestor");
}
Mutabletreenode oldparent = (mutabletreenode) newchild.getparent ();
Notice here to determine whether the child node to be added has a parent node, and if so, remove the child node from the parent node
//And the example above is exactly the case where the parent node was originally
(Oldparent!= null) {
Oldparent.remove (newchild);
}
Newchild.setparent (this);
if (children = = null) {
children = new Vector ();
}
Children.insertelementat (newchild, Childindex);
}
The following is the Remove method source code:
public void Remove (Mutabletreenode achild) {
if (Achild = null) {
throw new illegalargumentexception ("Argument I s null ");
}
if (!isnodechild (Achild)) {
throw new IllegalArgumentException ("argument is isn't a child");
}
Remove (GetIndex (achild)); Linear Search
} public
void remove (int childindex) {
Mutabletreenode child = (Mutabletreenode) Getchildat (childindex);
Children.removeelementat (Childindex);//Call removal element method
child.setparent (null);
}
A vector object is used to hold the list of child nodes in Defaultmutabletreenode, and the Elementcount member variable is updated in the vector's Removeelementat method, that is, the number of elements in the vector:
Public synchronized void Removeelementat (int index) {
modcount++;
if (index >= elementcount) {
throw new ArrayIndexOutOfBoundsException (index + ">=" +
elementcount);
}
else if (Index < 0) {
throw new arrayindexoutofboundsexception (index);
}
int j = elementcount-index-1;
if (J > 0) {
system.arraycopy (elementdata, index + 1, elementdata, index, j);
}
Elementcount--;//elementcount self minus 1
elementdata[elementcount] = null; * To let GC does its work */
}
Now take a look at Defaultmutabletreenode's children method:
Public enumeration Children () {
if (children = null) {return
empty_enumeration;
} else {return
childre N.elements ()//Call vector elements () method
} public
enumeration<e> elements () {return
new Enumeration<e> () {
int count = 0;
public boolean hasmoreelements () {
return count < elementcount;//depends on the value of Elementcount when deciding whether to have the next element
} Public
E nextelement () {
synchronized (vector.this) {
if (count < Elementcount) {
return Elementdata (count++);
}
throw new Nosuchelementexception ("Vector enumeration");
}
We should be able to understand why, when Root adds the first node Zhangsan, the Zhangsan node is removed from the persons, then the Lisi node becomes the node in the persons with an index of 0. Because the vector updated the Elementcount,elementcount value to 3,enumeration member Count value is 1, the WANGWU (index 1) is obtained when Root adds the next node returned by enumeration. This occurs because the count value changes to the 2,elementcount value to 2, and false when the next call to the hasMoreElements method is returned.
The solution to the above problem is to call the Java.util.Collections list method to convert enumeration to a list object, because the number of elements in the list object is not invoked before the list method, because the Defaultmutabletreenode Add method is not called is correct, then the Add method that calls Defaultmutabletreenode when traversing the list is no longer dependent on the vector's elementcount value, so there is no further problem.