我想从我将从 SQL 查询中导入的二维对象数组创建一个 JTree。这是 SQL 表的样子:
这是对象的示例:
Object[][] table = {
{1, 0, "Root"}, //i=0
{2, 1, "Node2"}, //i=1
{3, 1, "Node3"}, //i=2
{4, 1, "Node4"}, //i=3
{5, 4, "Node5"}, //i=4
{6, 4, "Node6"}, //i=5
{7, 4, "Node7"}, //i=6
{8, 1, "Node8"}, //i=7
{9, 1, "Node9"}, //i=8
{10, 9, "Node10"},}; //i=9
这是我用来对数组进行排序的逻辑:
for (int i = 0; i < table.length; i++) {
for (int j = i; j < table.length; j++) {
if (table[i][0] == table[j][1]) {
System.out.println(table[i][2].toString() + " is parent of " + table[j][2].toString());
}
}
}
这是上面在控制台中显示的内容:
Root is parent of Node2
Root is parent of Node3
Root is parent of Node4
Root is parent of Node8
Root is parent of Node9
Node4 is parent of Node5
Node4 is parent of Node6
Node4 is parent of Node7
Node9 is parent of Node10
我正在努力创建可用于创建 JTree 的 TreeModel、HashTable、Object 等。
我已经在这个问题上停留了一个多星期,现在我真的可以借鉴其他人的经验。