Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有可能在一个循环中构建和新建一个结构?_C# - Fatal编程技术网

C# 是否有可能在一个循环中构建和新建一个结构?

C# 是否有可能在一个循环中构建和新建一个结构?,c#,C#,我想知道我怎样才能穿过这个循环? 这是有根源的 两个孩子分别是私生子和非私生子 还有他们的孩子 priv:0,1,2,…,1023 以及非RIV儿童: 1024,…,65535 |Any______________PORT| | | |Priv| |Non-Priv| |||||...|||| ||||....|||||

我想知道我怎样才能穿过这个循环? 这是有根源的 两个孩子分别是私生子和非私生子 还有他们的孩子 priv:0,1,2,…,1023 以及非RIV儿童: 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);
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间ID
{
班级计划
{
静态void Main(字符串[]参数)
{
节点根=新节点(“任何_端口”);
List portcat=新列表();
Node Priv=新节点(“Priv”);
Node NonPriv=新节点(“非Priv”);
根。setchildren(portcat);
root.Thisisroot();
root.setNodeinlist(Priv);
root.setNodeinlist(非驱动);
Priv.setparent(根);
非驱动setparent(根);
List portsP=新列表();
Priv.setchildren(portsP);
对于(int i=0;i<1024;i++)
{
}
//我尝试了这个方法,但无法修复节点和字符串列表之间的问题
List portsN=新列表();
对于(int i=1024;i<65535;i++)
{
portsN.Add(i.ToString());
}
}
公共类节点
{
公共字符串数据;
public List Children=new List();
公共节点父节点;
公共节点(字符串r)
{
这个。数据=r;
}
公共节点thisiroot()
{
this.parent=null;
归还这个;
}
公共无效集合子对象(列表)
{
这个.Children=列表;
}
公共列表getchildren()
{
把这个还给我,孩子们;
}
公共void setparent(节点p)
{
这个。父=p;
}
公共节点getparent()
{
将此文件返回给父对象;
}
公共无效setNodeinlist(节点n)
{
本.Children.Add(n);
}
}
}
}

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

List<Node> nodes = new List<Node>();
for (int i = 0; i < 1024; i++)
{
    nodes.Add(new Node(i.ToString()));    
}
List节点=新建列表();
对于(int i=0;i<1024;i++)
{
添加(新节点(i.ToString());
}

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

List<Node> nodes = new List<Node>();
for (int i = 0; i < 1024; i++)
{
    nodes.Add(new Node(i.ToString()));    
}
List节点=新建列表();
对于(int i=0;i<1024;i++)
{
添加(新节点(i.ToString());
}
您需要的是
列表
,而不是
列表

LINQ寻求帮助:

IList<Node> nodes = Enumerable.Range(1024, 65535)
                              .Select(i => new Node(i.ToString))
                              .ToList();
IList节点=可枚举的范围(102465535)
.Select(i=>newnode(i.ToString))
.ToList();
您需要的是
列表
,而不是
列表

LINQ寻求帮助:

IList<Node> nodes = Enumerable.Range(1024, 65535)
                              .Select(i => new Node(i.ToString))
                              .ToList();
IList节点=可枚举的范围(102465535)
.Select(i=>newnode(i.ToString))
.ToList();

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

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

     for (int i = 0; i < 1024; i++)
                {
                nodeList.Add(new Node(i.ToString()));    
                }
List nodeList=new List()
对于(int i=0;i<1024;i++)
{
添加(新节点(i.ToString());
}

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

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

     for (int i = 0; i < 1024; i++)
                {
                nodeList.Add(new Node(i.ToString()));    
                }
List nodeList=new List()
对于(int i=0;i<1024;i++)
{
添加(新节点(i.ToString());
}

也许您的意思是手动创建一个列表,如下所示:

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;
     }
}
然后

节点优先;
节点lastCreated=null;
对于(int i=0;i<1024;i++)
{
Node n=新节点(i.ToString());
如果(i==0)
第一个=n;
n、 父项=上次创建的;
if(lastCreated!=null)
lastCreated.child=n;
lastCreated=n;
}

也许您的意思是手动创建一个列表,如下所示:

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;
     }
}
然后

节点优先;
节点lastCreated=null;
对于(int i=0;i<1024;i++)
{
Node n=新节点(i.ToString());
如果(i==0)
第一个=n;
n、 父项=上次创建的;
if(lastCreated!=null)
lastCreated.child=n;
lastCreated=n;
}

看起来您想使用一个
列表
并添加到该列表中。另外,我鼓励您在提问之前先浏览C#教程。您需要的是
列表
,而不是
列表
,请参见我的答案。@Negin Nicki:您收到的实际错误消息是“错误,我是傻瓜吗?!”?如果没有,您能发布实际的错误消息吗?如果您阅读错误,修复错误通常会容易得多,而我们只能在您发布实际消息的情况下才能做到这一点。
List nodes=new List()
或者您所说的new It?不,这不是错误!我在这里尝试了Node I,它显示了此错误!:无法在此范围内声明名为i的局部变量,因为它将赋予已在父级或当前范围内使用的i不同的含义,以表示您希望使用
列表
并添加到该列表中的其他内容。另外,我鼓励你在提问之前先浏览C#教程。你需要
List
rath