zoukankan      html  css  js  c++  java
  • 集合-列表

    .net framework为动态列表提供了泛型类List<T>.继承了6个接口,其定义如下:

    public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

                List<int> list = new List<int>(8); //创建列表

                Console.WriteLine(list.Count.ToString());//列表元素数量

                Console.WriteLine(list.Capacity.ToString());//列表容量

                list.TrimExcess();// 去除不需要的容量,当数量达到容量90%时,不处理。

     

                var liststrs = new List<string> { "bj", "sh" };//可定义时初始化值

                liststrs.Add("wuhan");//Add添加元素

                liststrs.Insert(1, "tj");//Insert插入元素

                liststrs.ForEach(Console.WriteLine);//此方法相当于foreach循环

                liststrs.RemoveAt(0);//移除

            Console.WriteLine(liststrs[0]);//通过下标访问元素

    具体例子代码:

    namespace ConsoleApplication1

    {

        public class Racer : IFormattable, IComparable<Racer>

        {

            public int ID { get; private set; }

            public string FirstName { get; set; }

            public string LastName { get; set; }

            public string Country { get; set; }

            public int Winns { get; set; }

     

            public Racer(int id, string firstname, string lastname, string country = null, int winns = 0)

            {

                this.ID = id;

                this.FirstName = firstname;

                this.LastName = lastname;

                this.Country = country;

                this.Winns = winns;

            }

            public override string ToString()

            {

                return string.Format("{0}-{1}", FirstName, LastName);

            }

            public string ToString(string format, IFormatProvider formProvider)

            {

                if (format == null)

                {

                    format = "N";

                }

                switch (format.ToUpper())

                {

                    case "N":

                        return ToString();

                    case "F":

                        return FirstName;

                    case "L":

                        return LastName;

                    case "W":

                        return string.Format("{0},win:{1}", ToString(), Winns);

                    case "C":

                        return string.Format("{0},country:{1}", ToString(), Country);

                    case "A":

                        return string.Format("{0},win:{1},country:{2}", ToString(), Winns, Country);

                    default:

                        throw new FormatException(string.Format(formProvider, "字母{0}未定义", format));

                }

            }

     

            public string Tostring(string format)

            {

                return ToString(format, null);

            }

     

            public int CompareTo(Racer other)

            {

                int compare = this.LastName.CompareTo(other.LastName);

                if (compare == 0)

                {

                    compare = this.FirstName.CompareTo(other.FirstName);

                }

                return compare;

            }

        }

     

        public class RacerCompare : IComparer<Racer>

        {

            public enum RacerType

            {

                firstname,

                lastname,

                country,

                winns

            }

            private RacerType racerType;

            public RacerCompare(RacerType racertype)

            {

                this.racerType = racertype;

            }

     

            public int Compare(Racer x, Racer y)

            {

                if (x == null)

                {

                    throw new ArgumentNullException("x");

                }

                if (y == null)

                {

                    throw new ArgumentNullException("y");

                }

                int result;

                switch (racerType)

                {

                    case RacerType.firstname:

                        result = x.FirstName.CompareTo(y.FirstName);

                        break;

                    case RacerType.lastname:

                        result = x.LastName.CompareTo(y.LastName);

                        break;

                    case RacerType.country:

                        result = x.Country.CompareTo(y.Country);

                        break;

                    case RacerType.winns:

                        result = x.Winns.CompareTo(y.Winns);

                        break;

                    default:

                        throw new ArgumentException("未定义的类型");

                }

                return result;

            }

        }

     

        public class Person

        {

            private string name;

            public Person(String name)

            {

                this.name = name;

            }

            public override string ToString()

            {

                return name;

            }

        }

        class Program

        {

            static void Main(string[] args)

            {

                Racer zhang = new Racer(1, "zhang", "gong", "shangxiu", 0);

                Racer xi = new Racer(2, "xi", "feng", "china", 3);

                Racer mao = new Racer(3, "mao", "tai", "zhongguo", 6);

                List<Racer> recers = new List<Racer>(10) { zhang, xi, mao };

                recers.Add(new Racer(7, "wu", "liangye", "tangchao", 9));

                recers.AddRange(new Racer[] {

                new Racer(9, "du", "kang", "hanchao", 12),

                new Racer(10,"qing","shui","zhonghua",100)});

     

                recers.Sort(new RacerCompare(RacerCompare.RacerType.lastname));

                recers.ForEach(Console.WriteLine);

     

                List<Person> persons = recers.ConvertAll<Person>(p => new Person(p.FirstName + " " + p.LastName));

                persons.ForEach(Console.WriteLine);

                Console.Read();

            }

        }

    }

  • 相关阅读:
    js炫酷效果
    程序员的执着
    [心得]docker学习笔记
    [心得笔记]多线程之间的内存可见性问题
    Docker入门
    [心得体会]jvm
    redis学习总结
    [心得]redis集群环境搭建的错误
    Linux安装mysql5.7版本
    Cent OS下安装JDK11
  • 原文地址:https://www.cnblogs.com/hometown/p/3205263.html
Copyright © 2011-2022 走看看