0

どうすればこのループを通り抜けることができますか?これには、ルート2つのchldren privとnonprivがあり、priv:0,1,2、...、1023の子とNonprivの子:1024、..、65535があります。

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

つまり、0は別のノードにあります1は別のノードにあります2は別のノードにありますこれらのポートには親PRIVがあり
ますノードのリストを作成することも、文字列のリストに含めることもできますが、文字列のリストについて次に何をするかを決めることができず、ノードのリストの問題は、ノードを構築できなかった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 に答える