0

私は ArrayCollection を持っており、各要素は TreeNode クラス (私が作成したカスタム クラス) のインスタンスであり、より多くの TreeNode 要素の ArrayCollection である「子」プロパティがあります。そうすれば、ArrayCollection 構造に要素のツリーがあります。

tree = new ArrayCollection([
    [new TreeNode(param1, param2, new ArrayCollection([
        [new TreeNode(param1, param2, null)],
        [new TreeNode(param1, param2, new ArrayCollection([
            [new TreeNode(param1, param2, null)],
            [new TreeNode(param1, param2, new ArrayCollection([
                [new TreeNode(param1, param2, null)],
                [new TreeNode(param1, param2, null)]
            ]))],
            [new TreeNode(param1, param2, new ArrayCollection([
                [new TreeNode(param1, param2, null)],
                [new TreeNode(param1, param2, null)]
            ]))]
        ]))]
    ]))],
    [new TreeNode(param1, param2, null)]
]);

TreeNode コンストラクターには 3 つのパラメーターがあります。最初の 2 つは重要ではありませんが、3 番目は children プロパティ (ArrayCollection) であり、TreeNode に子がない場合は、そのパラメーターを null に設定する必要があります。

「ツリー」構造を再帰的に解析する次の関数を作成しました。

private function parse(obj:Object):void {
    for (var i:int = 0; i < obj.length; i++) {
        if (obj[i] is TreeNode) {
            if (obj[i].children != null) {
                parse(obj[i].children);
            }
        } else {
            parse(obj[i]);
        }
    }
}
parse(tree);

しかし、私の問題は次のとおりです。別のクラスのインスタンスで満たされた同じ「ツリー」構造 (同じ変数である必要はありません) が必要です。どうすればそれを達成できますか?

4

1 に答える 1

0

やったよ:

private function parse(obj:Object, ancestor:Node):void {
    for (var i:int = 0; i < obj.length; i++) {
        if (obj[i] is TreeNode) {

            var node:Node = new Node(obj[i].param1, obj[i].param2);
            node.ancestor = ancestor;

            if (ancestor != null) {
                ancestor.children.push(node);
            }

            if (obj[i].children != null) {
                parse(obj[i].children, node);
            }

            obj[i] = node;
        } else {
            parse(obj[i], ancestor);
        }
    }
}
parse(tree, null);

そうすれば、すべての TreeNodes が Nodes に変換されます (Node は私が作成した別のカスタム クラスです)

于 2011-02-02T10:57:14.740 に答える