C#将可见节点添加到节点阵列

时间:2021-10-29 07:19:33

I'm trying to create a function that will add each visible node in a tree to a node array and then return it.

我正在尝试创建一个函数,将树中的每个可见节点添加到节点数组,然后返回它。

This is the code I have so far, but struggling to figure out how to add them.

这是我到目前为止的代码,但很难弄清楚如何添加它们。

Note: The tree has a maximum of 8 nodes.

注意:树最多有8个节点。

    private Node[] activeClients(AdvTree tree)
    {
        Node[] activeClients = new Node[8];

        foreach (Node client in tree.Nodes)
        {
            if (client.IsVisible)
            {
                //Add Visible Node to activeClients Node Array
            }
        }

        return activeClients;
    }

2 个解决方案

#1


1  

May be something like:

可能是这样的:

var visibleNodes = tree.Nodes.Where(client=>client.IsVisible)

especially if you are talking about small numbers (8 elements) and not compute intensive function, dynamic array (or vector) like List<T>, IEnumerable<T> is a right choice.

特别是如果你在谈论小数字(8个元素)而不是计算密集型函数,像List 这样的动态数组(或向量),IEnumerable 是一个正确的选择。

And in this way, your code also scales better in the future.

通过这种方式,您的代码将来也可以更好地扩展。

#2


0  

I actually figured out I didn't need a Node Array, but thanks for the help guys.

我实际上发现我不需要节点阵列,但感谢帮助人员。

I used NodeCollection instead and it worked perfect for my needs.

我使用了NodeCollection,它完美地满足了我的需求。

    private NodeCollection activeClients(AdvTree tree)
    {
        NodeCollection activeClients = new NodeCollection();

        foreach (Node client in tree.Nodes)
        {
            if (client.IsVisible)
            {
                //Add Visible Node to activeClients Node Array
                activeClients.Add(client, eTreeAction.Code);
            }
        }

        return activeClients;
    }

#1


1  

May be something like:

可能是这样的:

var visibleNodes = tree.Nodes.Where(client=>client.IsVisible)

especially if you are talking about small numbers (8 elements) and not compute intensive function, dynamic array (or vector) like List<T>, IEnumerable<T> is a right choice.

特别是如果你在谈论小数字(8个元素)而不是计算密集型函数,像List 这样的动态数组(或向量),IEnumerable 是一个正确的选择。

And in this way, your code also scales better in the future.

通过这种方式,您的代码将来也可以更好地扩展。

#2


0  

I actually figured out I didn't need a Node Array, but thanks for the help guys.

我实际上发现我不需要节点阵列,但感谢帮助人员。

I used NodeCollection instead and it worked perfect for my needs.

我使用了NodeCollection,它完美地满足了我的需求。

    private NodeCollection activeClients(AdvTree tree)
    {
        NodeCollection activeClients = new NodeCollection();

        foreach (Node client in tree.Nodes)
        {
            if (client.IsVisible)
            {
                //Add Visible Node to activeClients Node Array
                activeClients.Add(client, eTreeAction.Code);
            }
        }

        return activeClients;
    }