TreeSet ( sortedNodes
) 和 ArrayList ( nodes
) 有一个奇怪的问题。在我的程序中,我有一个从事件调度线程(来自ActionListener
)调用的方法,这些行:
System.out.println("nodes: "+nodes.size());
sortedNodes.addAll(nodes);
System.out.println("sortedNodes: "+sortedNodes.size());
问题是,在某些集合上sortedNodes.size()
返回的数字低于nodes.size()
(在这 3 行上,因此 的内容没有变化nodes
)。然后,当我打印 的内容时sortedNodes
,除了不包含它应该包含的所有对象之外,它甚至都没有排序。奇怪的是 - 如果我再次调用整个方法,它会解决问题。我不明白 - 在相同的集合上执行相同的代码,但第一次不起作用,第二次起作用。有任何想法吗?
编辑:如果我的问题不是很清楚,这应该会有所帮助
exportTree();
exportTree();
在输出上打印:
nodes: 7
sortedNodes: 4
b2[23,57]a[46,97]b[65,77]c[43,43]
nodes: 7
sortedNodes: 7
a[46,97]b[65,77]b1[55,89]b2[23,57]b3[20,20]c[43,43]c1[99,88]
比较器:
public class NodeComparator implements Comparator<Node>{
public int compare(Node o1, Node o2) {
return o1.getLabel().compareTo(o2.getLabel());
}
}
节点:
public class Node {
private int order;
private String label;
private Integer[] keys;
private Node[] pointers;
private Node parent;
public Node(int order, String label, Integer[] keys, Node[] pointers) {
this.order = order;
this.label = label;
this.parent = null;
if (pointers == null) {
this.pointers = new Node[order+1];
} else {
this.pointers = pointers;
}
if (keys == null) {
this.keys = new Integer[order];
} else {
this.keys = keys;
}
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public Integer[] getKeys() {
return keys;
}
public void setKeys(Integer[] keys) {
this.keys = keys;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public Node[] getPointers() {
return pointers;
}
public void setPointers(Node[] pointers) {
this.pointers = pointers;
}
public Node getPointer(int i) {
return pointers[i];
}
public void setPointer(int i, Node node) {
pointers[i] = node;
}
public Integer getKey(int i) {
return keys[i];
}
public void setKey(int i, Integer key) {
keys[i] = key;
}
}
整体方法:
public void exportTree() {
String graphInText = "";
if (nodeShapes.isEmpty()) {
graphInText = "empty";
} else {
char c = 'a';
ArrayList<Node> nodes = new ArrayList<Node>();
sortedNodes.clear();
System.out.println("nodeShapes: " + nodeShapes.size());
// populate the collection of nodes from their 2d representation(nodeShapes)
// and label every root with different character
for (NodeShape n : nodeShapes) {
nodes.add(n.getNode());
if (n.getParentLink() == null) {
n.getNode().setLabel(c + "");
c++;
}
}
System.out.println("nodes: " + nodes.size());
// copy all the nodes (currently every node except roots has label "0")
sortedNodes.addAll(nodes);
System.out.println("sortedNodes: " + sortedNodes.size());
// check labels of every node; if it contains any of the characters
// that were assigned to roots, use this label for every child of
// this node and add number of the pointer at the end of the string;
// when this is done, remove those nodes, which children have been
// labeled;
// repeat whole procedure while there is no node left - and this will
// happen, since every node is connected to a root or is a root;
while (!nodes.isEmpty()) {
ArrayList<Node> nodesToBeRemoved = new ArrayList<Node>();
for (Node n : nodes) {
for (char c2 = 'a'; c2 <= c; c2++) {
if (n.getLabel().contains(c2 + "")) {
for (int i = 1; i <= n.getOrder(); i++) {
Node child = n.getPointer(i);
if (child != null) {
child.setLabel(n.getLabel() + i);
}
}
nodesToBeRemoved.add(n);
}
}
}
if (!nodesToBeRemoved.isEmpty()) {
nodes.removeAll(nodesToBeRemoved);
}
}
Node[] nodesA = sortedNodes.toArray(new Node[sortedNodes.size()]);
for (Node n : nodesA) {
String nodeInText;
nodeInText = n.getLabel() + "[";
for (int i = 1; i < n.getOrder() - 1; i++) {
nodeInText += n.getKey(i) + ",";
}
nodeInText += n.getKey(n.getOrder() - 1) + "]";
graphInText += nodeInText;
}
}
System.out.println(graphInText);
label.setText(graphInText);
}
我还更改了程序,因此每次创建/删除 NodeShape 时,也会将 Node 添加/删除到新集合中,我在 exportTree() 中使用这个新集合而不是 nodeShapes - 但它的工作原理相同,所以 nodeShapes 没有问题. 它只是 TreeSet .. 当我不使用它时,一切正常(但我没有对节点进行排序)。