Struct+ArrayList冒泡排序。例
namespace ConsoleApplication22 { class Program { struct s { public string name; public int age; } static void Main(string[] args) { s s1=new s(); s1.name ="Kira"; s1.age = 18; s s2 = new s(); s2.name = "Lacus"; s2.age = 19; s s3 = new s(); s3.name = "Arthran"; s3.age = 25; s s4 = new s(); s4.name = "Asuka"; s4.age = 16; s s5 = new s(); s5.name = "Rei"; s5.age = 13; ArrayList al = new ArrayList(); al.Add(s1); al.Add(s2); al.Add(s3); al.Add(s4); al.Add(s5); s temp=new s(); for (int i = 0; i < al.Count; i++) { for (int j = i; j < al.Count-1; j++) { if (((s)al[i]).age<((s)al[j+1]).age)//强制转换成struct s类型:定义的名字为s { temp = (s)al[i]; al[i] = al[j + 1]; al[j + 1] = temp; } } } for (int i = 0;i<al.Count; i++) { Console.WriteLine(((s)al[i]).name+"\t"+((s)al[i]).age); } Console.ReadLine(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。