使用列表的列表递归填充 JTree
Recursively populate JTree, using list of list
这道题在
中有相反的过程
我想实现流程的方法优化,我将这个流程简化(乘法和除法),就像所示的树一样。
底部和粗体矩形(在线工具不要让我更改颜色)表示 N 分子和 D[=40 之间的总乘法=] 每个列表的分母。
现在,我正在 Java 中创建列表列表,以计算每个列表(过程)的总成本。
我的第一步是创建一个 class...
public class Pair {
private int num;
private int den;
public Pair(int num, int den) {
this.num = num;
this.den = den;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getDen() {
return den;
}
public void setDen(int den) {
this.den = den;
}
@Override
public String toString() {
return "Pair{" + "num=" + num + ", den=" + den + '}';
}
}
第二步是...
public class 测试对 {
public static void main(String... args) {
ArrayList<ArrayList<Pair>> listOfListPair = new ArrayList<>();
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));
javax.swing.JTree myTree = new javax.swing.JTree();
listOfListPair.stream().forEachOrdered(listPair -> {
//HOW POPULATE THE myTree?
});
}
}
对我来说重要的问题:JTree, Optimization algorithm, Java
关于递归填充我的树的一些线索?、
我的答案稍作改动...
public class TestPair {
public static void main(String... args) {
List<List<Pair>> listOfListPair = new ArrayList<>();
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));
final JTree tree = new JTree();
tree.setRootVisible(false);
removeAllTreeNodes(tree);
listOfListPair.stream().forEachOrdered(listPair -> {
addPairToTree(tree, listPair);
});
JFrame frame = new JFrame("Optimization Pair");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
frame.add("Center", scrollPane);
frame.pack();
frame.setSize(new Dimension(600, 600));
frame.setVisible(true);
}
private static void addPairToTree(JTree tree, List<Pair> listPair) {
try {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
DefaultMutableTreeNode pairTreeNode = rootTreeNode;
final Pair leafPair = new Pair(1, 1);
for (Pair pair : listPair) {
pairTreeNode = getDefaultMutableTreeNode(pairTreeNode, pair, true);
leafPair.setDen(leafPair.getDen() * ((Pair) pairTreeNode.getUserObject()).getDen());
leafPair.setNum(leafPair.getNum() * ((Pair) pairTreeNode.getUserObject()).getNum());
}
getDefaultMutableTreeNode(pairTreeNode, leafPair, false);
treeModel.reload(rootTreeNode);
} catch (Exception e) {
throw e;
}
}
private static DefaultMutableTreeNode getDefaultMutableTreeNode(DefaultMutableTreeNode parent, Pair newChild, Boolean isLeaf) {
if (parent != null) {
DefaultMutableTreeNode child;
for (int i = 0; i < parent.getChildCount(); i++) {
child = (DefaultMutableTreeNode) parent.getChildAt(i);
if (child.toString().equals(newChild.toString())) {
return child;
}
}
child = new DefaultMutableTreeNode(newChild, isLeaf);
parent.add(child);
return child;
} else {
return null;
}
}
private static void removeAllTreeNodes(JTree tree) {
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
if (rootTreeNode != null) {
rootTreeNode.removeAllChildren();
}
reloadTree(tree);
}
private static void reloadTree(JTree tree) {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
treeModel.reload(rootTreeNode);
}
}
这道题在
我想实现流程的方法优化,我将这个流程简化(乘法和除法),就像所示的树一样。
底部和粗体矩形(在线工具不要让我更改颜色)表示 N 分子和 D[=40 之间的总乘法=] 每个列表的分母。
现在,我正在 Java 中创建列表列表,以计算每个列表(过程)的总成本。
我的第一步是创建一个 class...
public class Pair {
private int num;
private int den;
public Pair(int num, int den) {
this.num = num;
this.den = den;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getDen() {
return den;
}
public void setDen(int den) {
this.den = den;
}
@Override
public String toString() {
return "Pair{" + "num=" + num + ", den=" + den + '}';
}
}
第二步是...
public class 测试对 {
public static void main(String... args) {
ArrayList<ArrayList<Pair>> listOfListPair = new ArrayList<>();
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));
javax.swing.JTree myTree = new javax.swing.JTree();
listOfListPair.stream().forEachOrdered(listPair -> {
//HOW POPULATE THE myTree?
});
}
}
对我来说重要的问题:JTree, Optimization algorithm, Java
关于递归填充我的树的一些线索?、
我的答案稍作改动...
public class TestPair {
public static void main(String... args) {
List<List<Pair>> listOfListPair = new ArrayList<>();
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
listOfListPair.add((List<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));
final JTree tree = new JTree();
tree.setRootVisible(false);
removeAllTreeNodes(tree);
listOfListPair.stream().forEachOrdered(listPair -> {
addPairToTree(tree, listPair);
});
JFrame frame = new JFrame("Optimization Pair");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
frame.add("Center", scrollPane);
frame.pack();
frame.setSize(new Dimension(600, 600));
frame.setVisible(true);
}
private static void addPairToTree(JTree tree, List<Pair> listPair) {
try {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
DefaultMutableTreeNode pairTreeNode = rootTreeNode;
final Pair leafPair = new Pair(1, 1);
for (Pair pair : listPair) {
pairTreeNode = getDefaultMutableTreeNode(pairTreeNode, pair, true);
leafPair.setDen(leafPair.getDen() * ((Pair) pairTreeNode.getUserObject()).getDen());
leafPair.setNum(leafPair.getNum() * ((Pair) pairTreeNode.getUserObject()).getNum());
}
getDefaultMutableTreeNode(pairTreeNode, leafPair, false);
treeModel.reload(rootTreeNode);
} catch (Exception e) {
throw e;
}
}
private static DefaultMutableTreeNode getDefaultMutableTreeNode(DefaultMutableTreeNode parent, Pair newChild, Boolean isLeaf) {
if (parent != null) {
DefaultMutableTreeNode child;
for (int i = 0; i < parent.getChildCount(); i++) {
child = (DefaultMutableTreeNode) parent.getChildAt(i);
if (child.toString().equals(newChild.toString())) {
return child;
}
}
child = new DefaultMutableTreeNode(newChild, isLeaf);
parent.add(child);
return child;
} else {
return null;
}
}
private static void removeAllTreeNodes(JTree tree) {
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
if (rootTreeNode != null) {
rootTreeNode.removeAllChildren();
}
reloadTree(tree);
}
private static void reloadTree(JTree tree) {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
treeModel.reload(rootTreeNode);
}
}