不知道c#可不可以这么实现: List<string> roots = new List<string>(); roots 的长度未知,只知道第一个item是“a”。
这中情况下List<string> roots = new List<string>();
roots.add("a");然后我要动态继续将新取得的string[],给roots赋值,在“a”之后
这中情况下List<string> roots = new List<string>();
roots.add("a");然后我要动态继续将新取得的string[],给roots赋值,在“a”之后
roots.AddRange()
二:
roots.Insert(listString.Count-1,str)
那样你还是遍历roots,然后将string一个一个加在它之后foreach(string str in string[]的变量)
{
roots.Foreach(delegate(string obj){ obj+=str;});
}
结合下面的程序看吧。
private void InitTreeNodes(List<string> propertyKeys)
{
ShowTree.Nodes.Clear();
//TreeNodeCollection[] roots = { ShowTree.Nodes, null, null, null, null, null };
List<TreeNodeCollection> roots = new List<TreeNodeCollection>();
roots.Add(ShowTree.Nodes);
foreach (string key in propertyKeys)
{
string[] dirNames;
TreeNode node;
dirNames = key.Split('.'); for (int i = 0; i < dirNames.Length; i++)
{
if (roots[i].Find(dirNames[i], false).Length == 0)
{
node = new TreeNode();
node.Text = node.Name = dirNames[i];
roots[i].Add(node);
roots[i + 1] = node.Nodes;
}
}
}
}
之前有高手告诉我初始化treeview时用了以上函数,对于TreeNodeCollection[] roots = { ShowTree.Nodes, null, null, null, null, null };
我个人觉得不可预测性较大。所以我想动态生成,于是用了 List<TreeNodeCollection> roots = new List<TreeNodeCollection>();
并初始化roots.Add(ShowTree.Nodes);,此时roots[i + 1]出错~
我想知道这种情况下可以再动态么!?