0

我想知道我怎样才能走过这个循环?这有一个根两个 chldren priv 和 nonpriv 以及它们的孩子 priv:0,1,2,...,1023 和 Nonpriv 的孩子:1024,..,65535

              |Any______________PORT|
                 |              |
             |Priv|          |Non-Priv|
            |||||...||||   ||||....|||||
            0123,.....1023 1024,...65535

我的意思是 0 在不同的节点中 1 在不同的节点中 2 在不同的节点中 这些端口有父 PRIV 我区分它们的原因是也许有一天 0 会有
他们可能在列表中的子列表和我们可以有一个节点列表,或者它们可以在一个字符串列表中,但是我无法决定接下来要对字符串列表做什么,并且节点列表的问题在于我无法构建节点的 for 循环

现在我无法配置 for 循环我应该如何更正我的代码?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IDS
{
    class Program
    {
        static void Main(string[] args)
        {
            Node root = new Node("Any_PORT");
            List<Node> portcat = new List<Node>();
            Node Priv = new Node("Priv");
            Node NonPriv=new Node("Non-Priv");
            root.setchildren(portcat);
            root.Thisisroot();
            root.setNodeinlist(Priv);
            root.setNodeinlist(NonPriv);
            Priv.setparent(root);
            NonPriv.setparent(root);
            List<Node> portsP = new List<Node>();
            Priv.setchildren(portsP);
            for (int i = 0; i < 1024; i++)
            {

            }
    //I tried this one but I can't fix between Nodes and list of a string        
    List<string> portsN = new List<string>();
            for (int i = 1024; i < 65535; i++)
            {
                portsN.Add(i.ToString());
            }
        }

        public class Node
        {
            public string Data;
            public List<Node> Children = new List<Node>();
            public Node parent;
            public Node(string r)
            {
                this.Data = r;
             }
            public Node Thisisroot()
            {
                this.parent = null;
                return this;
            }
            public void setchildren(List<Node> list)
            {
                this.Children = list;
            }
            public List<Node> getchildren()
            {
                return this.Children;
            }
            public void setparent(Node p)
            {
                this.parent = p;
            }
            public Node getparent()
            {
                return this.parent;
            }
            public void setNodeinlist(Node n)
            {
                this.Children.Add(n);
            }

        }
    }
}
4

4 回答 4

3

您应该使用集合来保存对象,例如列表:

List<Node> nodes = new List<Node>();
for (int i = 0; i < 1024; i++)
{
    nodes.Add(new Node(i.ToString()));    
}
于 2012-04-08T18:44:26.593 回答
1

你需要List<Node>而不是List<string>.

LINQ 寻求帮助:

IList<Node> nodes = Enumerable.Range(1024, 65535)
                              .Select(i => new Node(i.ToString))
                              .ToList();
于 2012-04-08T18:44:43.693 回答
1

如果您指定变量名称,它将保存最后一次迭代的值,并且不会在循环外可见。您需要有一个节点类型对象的列表,然后在该列表中您可以在循环内添加类型节点的实例

List<Node> nodeList = new List<Node>()

     for (int i = 0; i < 1024; i++)
                {
                nodeList.Add(new Node(i.ToString()));    
                }
于 2012-04-08T18:45:03.757 回答
0

也许您的意思是像这样手动构建一个列表:

Node first;
Node lastCreated = null;
for (int i = 0; i < 1024; i++)
{
    Node n = new Node(i.ToString());
    if(i == 0)
        first = n;
    n.parent = lastCreated;
    lastCreated = n;
}

你仍然需要在每个项目中有另一个参考,如下所示:

class Node{
    public string Data;
    public Node parent;
    public Node child;
    public Node(string r)
    {
        this.Data = r;
     }
}

然后

Node first;
Node lastCreated = null;
for (int i = 0; i < 1024; i++)
{
    Node n = new Node(i.ToString());
    if(i == 0)
        first = n;
    n.Parent = lastCreated;
    if(lastCreated != null)
        lastCreated.child = n;
    lastCreated = n;
}
于 2012-04-08T18:53:14.370 回答