码迷,mamicode.com
首页 > 编程语言 > 详细

Struct+ArrayList冒泡排序。例

时间:2015-04-19 17:36:23      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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();
        }
    }
}

 

Struct+ArrayList冒泡排序。例

标签:

原文地址:http://www.cnblogs.com/dlexia/p/4439380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!