您的位置:首页 > 博客中心 > 编程语言 >

ArrayList集合排序

时间:2022-03-25 01:15

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ArrayList集合排序
{
    class Program
    {
        struct Player
        {
            public string name;
            public int mark;
          
        }
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            Player a = new Player();
            Console.WriteLine("请输入你们班的人数");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("请输入姓名:");
                a.name = Console.ReadLine();
                Console.WriteLine("请输入成绩");
                a.mark = Convert.ToInt32(Console.ReadLine());
                al.Add(a); 
            }
            for (int i = 1; i < al.Count; i++)
            {
                for (int j = 1; j <= al.Count-i; j++)
                {
                    if(((Player)al[j]).mark<((Player)al[j-1]).mark)
                    {
                        Player temp ;
                        temp = (Player)al[j -1];
                        al[j -1] = (Player)al[j];
                        al[j] = temp;
                    }
                }
            }
            for (int i = 0; i < al.Count; i++)
            {
                Console.WriteLine(((Player)al[i]).name+((Player)al[i]).mark);
            }
          
            Console.ReadLine();
        } //static函数的花括号
    }
}

本类排行

今日推荐

热门手游