2012-04-08 50 views
0

我想知道我该如何走过这个循环? 这有一个根 2 chldren私法和他们的nonpriv 和儿童 私法:0,1,2,...,1023 和Nonpriv的孩子: 1024,...,65535是否有可能在循环中构建并新建一个结构?

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

我的意思是0是在不同的节点 1是在不同的节点 2是在不同的节点 这些端口具有父PRIV 我的理由distincting他们的是,也许有一天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); 
      } 

     } 
    } 
} 
+1

看起来像是想使用'List '并添加到该列表中。另外,我鼓励您在提问之前先阅读C#教程。 – BrokenGlass 2012-04-08 18:44:31

+0

您需要'列表'而不是'列表',请参阅我的答案。 – sll 2012-04-08 18:49:56

+0

@Negin Nicki:是“错误,我是一个愚蠢的?!”您收到的实际错误消息?如果没有,你可以发布实际的错误信息吗?如果您阅读它们,修复错误通常会更容易,并且只有在发布实际消息时我们才能做到这一点。 – 2012-04-08 18:51:29

回答

3

你应该使用集合持有你的对象,如列表:

List<Node> nodes = new List<Node>(); 
for (int i = 0; i < 1024; i++) 
{ 
    nodes.Add(new Node(i.ToString()));  
} 
1

您需要List<Node>而非List<string>

LINQ求助:

IList<Node> nodes = Enumerable.Range(1024, 65535) 
           .Select(i => new Node(i.ToString)) 
           .ToList(); 
+0

谢谢你的帮助对不起,我得到你的时间 – 2012-04-08 19:31:05

+0

Np欢迎您! – sll 2012-04-08 19:45:15

1

如果指定变量名,将举办最后一次迭代的价值,并不会是外循环可见。你需要有节点类型对象的列表,然后在列表中,您可以添加循环内部节点类型的实例

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

    for (int i = 0; i < 1024; i++) 
       { 
       nodeList.Add(new Node(i.ToString()));  
       } 
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; 
} 
相关问题