我想从字符串创建通用列表 [英] i want to create generic list from string

查看:93
本文介绍了我想从字符串创建通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符串创建泛型列表

输入字符串是(a(b,c,u),d,e(f),g(),我的课程是

pre> public class Node
{
public string Name; //方法名称
// public decimal Time; //在方法中花费的时间
public List< Node>儿童;

$ / code $ / pre

子节点代表in()。



示例:a是父节点,b,cu是子节点;将以与其父母相同的方式保存在 List< Node> 中,其中父母的j为孩子,k,l和m为孩子j。



它类似于树b
$ b

 <。> 
| ---< a>
| | --- b
| | --- c
| | --- u
| --- d
| ---< e>
| | --- f
| ---< g>
| --- h
| ---< i>
| | ---< J>
| | | --- k
| | | --- l
| | | ---< M>
| | | | --- n
| --- r


解决方案

对于你最终的结果Node数据结构最终会看起来像一棵树。要实现你想要的功能,你可以使用递归函数。

下面是这样一个函数的例子(带注释):

  //递归函数,用于从输入字符串中创建节点对象及其
//儿童列表
//返回:节点列表
//参数字符串输入:用于生成列表的输入字符串
//参数int index:开始循环字符串的索引
public List< Node> CreateNodeList(字符串输入,int索引)
{
List< Node> nodeList = new List< Node>();
Node currentNode = new Node();
StringBuilder nameBuilder = new StringBuilder();

//开始循环遍历字符串中的字符
//指定的索引
for(int i = index; i< array.size; i ++)
{
switch(input [i])
{
//如果我们看到一个开放的括号,我们需要使用
//继续列表来创建当前节点children
//我们通过递归调用该函数
//(将输入字符串和下一个索引作为
//参数传递)并将children属性设置为b
/ /返回值
case'(':
currentNode.Children = CreateNodeList(input,i + 1);
i = input.IndexOf(')',i);
休息;
//如果我们看到一个封闭的括号,我们创建一个新的基于节点的
//现有字符串,将它添加到列表中,然后
//返回列表
case')':
currentNode.Name = nameBuilder.ToString();
nodeList.Add(currentNode);
nameBuilder.Clear();
返回nodeList;
//如果我们看到一个逗号,我们创建一个基于
的新节点对象//关闭当前字符串并将其添加到列表中
case',':
currentNode.Name = nameBuilder.ToString();
nodeList.Add(currentNode);
nameBuilder.Clear();
currentNode = new Node();
休息;
//我们看到的任何其他字符都必须是节点名称
//,所以我们将它添加到字符串
// builder并继续循环
default:
nameBuilder.Append(input [i]);
}
}

//我们可能永远都不会到达这里,因为你的输入字符串
//通常以')'结尾,但如果我们完成返回
//列表
返回nodeList;


//如何使用这个递归函数的一个例子
public static void main()
{
//输入字符串
string input =(a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r);
//用输入字符串和1作为参数调用我们的函数
//我们用1跳过第一个'('这是一个索引0
List< Node> nodeList = CreateNodeList(输入,1);
//在这里做列表
}

此函数跟踪节点名称的字符,创建新的并在每次看到','或')'时将它们添加到列表中(在看到')'时返回List)),并且它还填充通过递归调用函数并使用它的返回值来看孩子的节点。一个主要的缺点是你要跟踪你所在的索引。



这个函数是自由编写的,但它与C#非常相似(你没有指定一种语言,所以我希望这有帮助。)



我希望这有助于你正在寻找的东西:)


i want to create generic list from string

input string is (a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r)

my class is

public class Node
{
    public string Name; // method name
  //  public decimal Time; // time spent in method
    public List<Node> Children;
}

child node is represent in ().

Example: a is parent node and b,c u are child nodes; will be saved in List<Node> in the same way as its parent which has j as child and k,l and m as children j.

it is similary like tree

<.>
 |---<a>
 |    |--- b
 |    |--- c
 |    |--- u
 |--- d
 |---<e>
 |    |--- f
 |---<g>
 |--- h
 |---<i>
 |    |---<j>
 |    |    |--- k
 |    |    |--- l
 |    |    |---<m>
 |    |    |    |--- n
 |--- r

解决方案

The end result for you Node data structure will end up looking similar to a tree. To achieve what you want you could use a recursive function.

Here is an example of such a function (with comments):

    //Recursive Function that creates a list of Node objects and their    
    //children from an input string
    //Returns: List of Nodes
    //Param string input: The input string used to generate the List
    //Param int index: The index to start looping through the string
    public List<Node> CreateNodeList(string input, int index)
    {
        List<Node> nodeList = new List<Node>();
        Node currentNode = new Node();
        StringBuilder nameBuilder = new StringBuilder();

        //Start looping through the characters in the string at the 
        //specified index
        for(int i = index; i < array.size; i++)
        {
            switch(input[i])
            {
                //If we see an open bracket we need to use the 
                //proceeding list to create the current nodes children
                //We do this by recursively calling this function 
                //(passing the input string and the next index as 
                //parameters) and setting the children property to b 
                //the return value
                case ‘(‘:
                    currentNode.Children = CreateNodeList(input, i+1);
                    i = input.IndexOf(‘)’, i);
                    break;
                //If we see a closed bracket we create a new Node based 
                //of the existing string, add it to the list, and then 
                //return the list
                case ‘)’:
                    currentNode.Name = nameBuilder.ToString();
                    nodeList.Add(currentNode);
                    nameBuilder.Clear();
                    return nodeList;
                //if we see a comma we create a new node object based 
                //off the current string and add it to the list
                case ‘,’:
                    currentNode.Name = nameBuilder.ToString();
                    nodeList.Add(currentNode);
                    nameBuilder.Clear();
                    currentNode = new Node();
                    break;
                //Any other character we see must be for the name   
                //of a node, so we will append it to our string 
                //builder and continue looping
                default:
                    nameBuilder.Append(input[i]);
            }
        }

        //We will probably never reach here since your input string 
        //usually ends in  ‘)’ but just in case we finish by returning 
        //the list
        return nodeList;
    }

    //An example of how to use this recursive function
    public static void main()
    {
        //Your input string
        string input = "(a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r)";
        //Call our function with the input string and 1 as arguments
        //We use 1 to skip the first ‘(‘ which is a index 0
        List<Node> nodeList = CreateNodeList(input, 1);
        //Do stuff with list here 
    }

This function keeps track of characters for the names of nodes, creating new ones and adding them to the list every time it sees a ',' or ')' (returning the List when seeing a ')')) and it also populates a Nodes children when it sees a '(' character by recursively calling the function and using its return value. The one major downside being you have keep track off the index you're on.

This function was written free hand but it's meant to be very similiar to C# (you didn't specify a language so I hope this helps.)

I hope this helps and is what your'e looking for :)

这篇关于我想从字符串创建通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆