例如字符串数组:
string[] arr = { "1","3-4-5-6-7", "2","3-4","3-4-5","3-4-5-6", "3", "6", "4", "6-1", "6-2", "5", "6-1-1","1-1","2-1", "1-2-2", "1-1-2", "2-2","1-1-1" };
目的想要树型结构化:

下面开始实现:
先定义一个TreeNode类
public class TreeNode
{
public string Value { get; set; }
public List<TreeNode> Children { get; set; }
public TreeNode(string value)
{
Value = value;
Children = new List<TreeNode>();
}
}
具体实现
TreeNode root = new TreeNode("root");
Dictionary<string, TreeNode> nodesMap = new Dictionary<string, TreeNode>();
foreach (var item in arr)
{
if (item.Contains("-"))
{
// 如果元素包含"-",则拆分并构建层级关系
string[] parts

最低0.47元/天 解锁文章
1442

被折叠的 条评论
为什么被折叠?



