SQLクエリからインポートする2次元オブジェクト配列から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などの作成に苦労しています。
私はこの問題に1週間以上悩まされており、今は他の人の経験を実際に利用することができました。