将嵌套的 ArrayCollection 元素转换为另一个类
我有一个 ArrayCollection,每个元素都是 TreeNode 类(我制作的自定义类)的一个实例,它有一个“children”属性,它是更多 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 个参数:前两个现在不重要,但第三个是 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);
但我的问题是:我需要具有相同的“树”结构(它不需要是相同的变量),并填充另一个类的实例。我怎样才能做到这一点?
I have an ArrayCollection and each element is an instance of the TreeNode class (a custom class made by me) that has a "children" property which is an ArrayCollection of more TreeNode elements. That way, I have a tree of elements in an ArrayCollection structure:
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)]
]);
The TreeNode constructor has 3 parameters: The first two don't matter now but the third is the children property (an ArrayCollection) and if the TreeNode doesn't have any children, then that parameter must be set to null.
I wrote the following function to parse the "tree" structure recursively:
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);
But my problem is: I need to have the same "tree" structure (it doens't need to be the same variable) filled with instances of another class. How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我这样做了:
这样,所有的 TreeNode 都将转换为 Node(Node 是我制作的另一个自定义类)
I did it:
That way, all the TreeNodes will be converted to Nodes (Node is another custom class I made)