gpt4 book ai didi

java - Java中的树结构?

转载 作者:行者123 更新时间:2023-12-01 12:48:34 24 4
gpt4 key购买 nike

我想实现一个树结构。树中的每个节点都包含我选择的一个对象,以及每个子节点(可能还有父节点)的链接。我想知道Java中是否存在这样的东西,例如:

T randomObject = new randomObject();
Node<T> root;
root.setObject(randomObject);
root.addChild( ...
....
root.getFirstChild().getObject().getObjectProperty();

我研究过 Node 结构(org.w3c.dom.Node),但它似乎无法存储对象,并且似乎更适合解析文档。

我还研究了像 Def​​aultMutableTreeNode、MutableTreeNode 和 TreeNode 这样的东西,但我还没有找到明确的用法示例。我在这些方面遇到了很多问题。例如,当将 DefaultMutableTreeNode 作为参数传递时(似乎不起作用),或者当获取 DefaultMutableTreeNode 的子节点时(它似乎仅返回一个 TreeNode),而我显然已将 DefaultMutableTreeNode 添加为子节点。

我是 Java 新手,所以任何信息都会非常有帮助。 Oracle 文档没有那么有用。我使用过 Vector 结构,它非常易于使用,可以存储对象并且易于访问,并且想知道是否可能存在类似的东西。谢谢。

最佳答案

查看https://github.com/poshjosh/bcuitreebuilder

一个轻量级的java库,用于从文档、文件等构建TreeNodes

public class ReadMe {

public static void main(String [] args) {

TreeBuilderFactory treeBuilderFactory = new TreeBuilderFactoryImpl();

// For displaying each JTree
//
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 300, 300);

// DocumentTreeBuilder
//
Document doc = loadDocument(
new File(System.getProperty("user.home")+"/Documents/Desktop/welcome.xml"));

DOMTreeBuilder domTreeBuilder = treeBuilderFactory.getDOMInstance();

Filter<Node> nodeFilter = null; // May be null

TreeNode docRootNode = domTreeBuilder.build(doc, nodeFilter);

JTree documentTree = new JTree(docRootNode);

// Display the JTree
//
scrollPane.setViewportView(documentTree);
JOptionPane.showMessageDialog(null, scrollPane);


// FileTreeBuilder
//
File dir = new File(System.getProperty("user.home")+"/Documents");

TreeBuilder<File> fileTreeBuilder =
treeBuilderFactory.getInstance(TreeBuilderFactory.FILE);
// This also works
//FileTreeBuilder fileTreeBuilder = treeBuilderFactory..getFileInstance();

Filter<File> fileFilter = new Filter<File>() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().endsWith(".docx");
}
};

TreeNode fileRootNode = fileTreeBuilder.build(dir, fileFilter);

JTree fileTree = new JTree(fileRootNode);

// Display the JTree
//
scrollPane.setViewportView(fileTree);
JOptionPane.showMessageDialog(null, scrollPane);


// MapTreeBuilder
//
MapTreeBuilder mapTreeBuilder = treeBuilderFactory.getMapInstance();

final HashMap map = new HashMap();
map.put("boolean", Boolean.TRUE);
map.put("number", 100);
map.put("List", new String[]{"1", "2", "3"});
HashMap grandChildren = new HashMap();
grandChildren.put("grandChild", "I am a grand child");
map.put("hasChildren", grandChildren);

Map.Entry rootEntry = mapTreeBuilder.createRootEntry(map);

TreeNode mapRootNode = mapTreeBuilder.build(rootEntry, null);

JTree mapTree = new JTree(mapRootNode);

// Display the JTree
//
scrollPane.setViewportView(mapTree);
JOptionPane.showMessageDialog(null, scrollPane);
}

private static Document loadDocument(File file) {
Document doc;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
doc = docBuilder.parse(file);
}catch (SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();
doc = null;
}
return doc;
}
}

关于java - Java中的树结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com