本文介绍了class as tree(包含其类型列表的类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有这个类 StudentsGroup ,它还包含 HashSet< StudentsGroup> public class StudentsGroup { public string 名称; public int StudentsCount; public HashSet< StudentsGroup>子群; public StudentsGroup() { SubGroups = new HashSet< StudentsGroup>(); Name = no_name; } public StudentsGroup( string name, int studens_count = 0 ): this () {姓名=姓名; StudentsCount = studens_count; } } 如何将元素添加到子组及其子组以分层方式。 例如我可以使用此代码添加项目 // ex AddGroup(新的StudentsGroup(第一年,第一部分,第1组,第1组),0,0,0 ); 此 .SubGroups。 ElementAt( 0 )。子组。 ElementAt( 0 )。子组。 ElementAt( 0 )。子组。 添加(新 StudentsGroup( 第一年,第1部分,第1组,第1组)); 现在我该如何实现函数 AddGroup 采用元素实例和父索引,如果父元素不存在则返回错误 public void AddGroup(StudentsGroup group , params int [] ParentIndex) { // 方法体 } 我的意思 比较这两种情况 StudentsGroup school = new StudentsGroup( 学校); // 每年添加三年包含两个班级 school.SubGroups。添加( new StudentsGroup( year1 )); school.SubGroups.Add( new StudentsGroup( YEAR2\" )); school.SubGroups.Add( new StudentsGroup( year3\" )); school.SubGroups.ElementAt( 0 )。SubGroups.Add( new StudentsGroup( class1)); school.SubGroups.ElementAt( 0 )。SubGroups.Add( new StudentsGroup( class2)); school.SubGroups.ElementAt( 1 )。SubGroups.Add( new StudentsGroup( class1)); school.SubGroups.ElementAt( 1 )。SubGroups.Add( new StudentsGroup( class2)); school.SubGroups.ElementAt( 2 )。SubGroups.Add( new StudentsGroup( class1)); school.SubGroups.ElementAt( 2 )。SubGroups.Add( new StudentsGroup( class2)); // 如果我实施AddGroup(组,父索引) school。 AddGroup( new StudentsGroup( year1 ), 0 ); school.AddGroup( new StudentsGroup( year2), 0 ); school.AddGroup( new StudentsGroup( year3), 0 ); school.AddGroup( new StudentsGroup( class1), 0 , 0 ); school.AddGroup( new StudentsGroup( class2), 0 , 0 ); school.AddGroup( new StudentsGroup( class1), 0 , 1 ); school.AddGroup( new StudentsGroup( class2), 0 , 1 ); school.AddGroup( new StudentsGroup( class1), 0 , 2 ); school.AddGroup( new StudentsGroup( class2), 0 , 2 ); //以及当我们有10级子组时,动态添加使用方法将有助于代码编写解决方案 public void AddGroup(StudentsGroup group , params int [] ParentIndex) { HashSet< StudentsGroup> last_subGroups = this .SubGroups; StudentsGroup current = this ; for ( int i = 1 ; i < ParentIndex.Length; i ++) { last_subGroups = current.SubGroups; current = current.SubGroups.ElementAt(ParentIndex [i]); } current.SubGroups.Add( group ); } I have this class StudentsGroup that also contain HashSet<StudentsGroup> public class StudentsGroup{ public string Name; public int StudentsCount; public HashSet<StudentsGroup> SubGroups; public StudentsGroup() { SubGroups = new HashSet<StudentsGroup>(); Name = "no_name"; } public StudentsGroup(string name,int studens_count=0):this() { Name = name; StudentsCount = studens_count; }}how can I add element to SubGroups and its SubGroups in hierarchical way.for example i can add item using this code//ex AddGroup(new StudentsGroup("first year, section one,group 1,subgroup 1"),0,0,0); this.SubGroups. ElementAt(0).SubGroups. ElementAt(0).SubGroups. ElementAt(0).SubGroups. Add(new StudentsGroup("first year , section one , group 1 , subgroup 1"));Now how can I implement function AddGroup that take element instance and parent index,and if parent not exist return errorpublic void AddGroup(StudentsGroup group, params int[] ParentIndex){ //method body}what I meancompare these two caseStudentsGroup school = new StudentsGroup("school");//add three years each year contain two classesschool.SubGroups.Add(new StudentsGroup("year1")); school.SubGroups.Add(new StudentsGroup("year2")); school.SubGroups.Add(new StudentsGroup("year3"));school.SubGroups.ElementAt(0).SubGroups.Add(new StudentsGroup("class1"));school.SubGroups.ElementAt(0).SubGroups.Add(new StudentsGroup("class2"));school.SubGroups.ElementAt(1).SubGroups.Add(new StudentsGroup("class1"));school.SubGroups.ElementAt(1).SubGroups.Add(new StudentsGroup("class2"));school.SubGroups.ElementAt(2).SubGroups.Add(new StudentsGroup("class1"));school.SubGroups.ElementAt(2).SubGroups.Add(new StudentsGroup("class2"));//if i implement AddGroup(group,parent index)school.AddGroup(new StudentsGroup("year1"), 0);school.AddGroup(new StudentsGroup("year2"), 0);school.AddGroup(new StudentsGroup("year3"), 0);school.AddGroup(new StudentsGroup("class1"), 0,0);school.AddGroup(new StudentsGroup("class2"), 0, 0);school.AddGroup(new StudentsGroup("class1"), 0, 1);school.AddGroup(new StudentsGroup("class2"), 0, 1);school.AddGroup(new StudentsGroup("class1"), 0, 2);school.AddGroup(new StudentsGroup("class2"), 0, 2);//and how about when we have 10 level of subGroups , dynamic add using method will facilitate code writing 解决方案 public void AddGroup(StudentsGroup group, params int[] ParentIndex){HashSet<StudentsGroup> last_subGroups = this.SubGroups;StudentsGroup current = this;for (int i = 1; i < ParentIndex.Length; i++){last_subGroups = current.SubGroups;current = current.SubGroups.ElementAt(ParentIndex[i]);}current.SubGroups.Add(group);} 这篇关于class as tree(包含其类型列表的类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 08:19