gpt4 book ai didi

java - 从 Bd 中分割并创建树

转载 作者:行者123 更新时间:2023-12-02 11:32:52 26 4
gpt4 key购买 nike

我有一个具有以下结构的 SQL 表,例如

name     father
-----------------
alex kmal jury salama

我尝试过这段代码...但它不起作用

con=DriverManager.getConnection("jdbc:ucanaccess://C:/bd/Base8.accdb");
java.sql.Statement stmt= con.createStatement();
ResultSet result= stmt.executeQuery("SELECT name,father FROM Base1");
while(result.next())
splitword(result.getString("father"),result.getString("name"));
Node.print(root);




public static void splitword(String father,String son) {
ArrayList<String> result = new ArrayList<>(Arrays.asList(father.split(" ") ));
Collections.reverse(result);
result.add(son);
System.out.println(result);


root2=new Node(result.get(0));
result.remove(0);

CreateTree(result,root2);


}
private static void CreateTree(ArrayList<String> result,Node tree) {
if(result!=null) {
Node child=new Node(result.get(0));
System.out.println(result.get(0));
result.remove(0);
tree.addChild(child);
CreateTree(result,child);
}

我的类节点:

public class Node {
private String data;
private Node parent;
private List<Node> children;


public Node(String data) {
this.data = data;
parent = null;
children = new ArrayList<Node>(); //Empty list of children
}

如何创建如下所示的树

          salama
|
jury
|
kmal
|
alex

萨拉马是陪审团之父,也是克马尔的陪审团之父,就像这样......这只是实现一棵大树的示例谢谢你

最佳答案

检查这个希望对你有帮助

private static void createTree(ArrayList<String> result, Node tree) {
if (result != null && !resultat.isEmpty()) {
Node child = new Node(result.get(0));
tree.addChild(child);

createTree(result.subList(1, result.size()), child);
}

关于java - 从 Bd 中分割并创建树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49169058/

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