0

ArrayCollection이 있고 각 요소는 더 많은 TreeNode 요소의 ArrayCollection 인 "children"속성을 가진 TreeNode 클래스 (사용자 정의 클래스)의 인스턴스입니다.중첩 된 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 개 개의 매개 변수가 있습니다 : 처음 두 이제 상관 없어하지만 제 아이들 속성 (AN있는 ArrayCollection)이며, 경우에 그 방법, 나는있는 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); 

을하지만 내 문제가 : 나는 같은 "트리"구조를 가질 필요가 (이 동일해야 넣은 사람은 아니다 변수)를 다른 클래스의 인스턴스로 채 웁니다. 어떻게하면 될까요?

답변

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); 

그런 식으로, 모두의 TreeNode가

(노드가 내가 만든 다른 사용자 정의 클래스) 노드로 변환됩니다
관련 문제