我想從將從SQL查詢中導入的二維對象數組創建JTree。這是SQL表的樣子:由SQL查詢填充的Java Jtree
這裏是對象的樣品:
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
我很努力創建TreeModel,HashTable,對象等,我可以用來創建JTree。
我一直在這個問題上停留了一個多星期,我現在可以真正使用他人的經驗。
將代碼['Outline'](http://codereview.stackexchange.com/a/4447/6692)是利益? – trashgod 2012-04-04 16:00:32