⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 c#开发宝典 光盘内容。本光盘主要为书中的源程序
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Example9_41
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义SortedList对象,并初始化
            SortedList mySortedList1;
            mySortedList1 = new SortedList();

            //定义SortedList对象,并初始化
            SortedList mySortedList2;
            mySortedList2 = new SortedList(5);

            //为mySortedList1中添加元素
            mySortedList1.Add(1, "a");
            mySortedList1.Add(2, "b");
            mySortedList1.Add(3, "c");
            mySortedList1.Add(4, "d");
            mySortedList1.Add(5, "e");

            //为mySortedList2中添加元素
            mySortedList2.Add(1, "a");
            mySortedList2.Add(2, "b");
            mySortedList2.Add(3, "c");
            mySortedList2.Add(4, "d");
            mySortedList2.Add(5, "e");

            //输出mySortedList1的容量及元素个数
            Console.WriteLine("mySortedList1的容量为:{0}", mySortedList1.Capacity);
            Console.WriteLine("mySortedList1的元素个数为:{0}", mySortedList1.Count);

            //输出mySortedList1的容量及元素个数
            Console.WriteLine("mySortedList1的容量为:{0}", mySortedList1.Capacity);
            Console.WriteLine("mySortedList1的元素个数为:{0}", mySortedList1.Count);

            //移除元素
            mySortedList1.Remove(1);
            Console.WriteLine("mySortedList1的容量为:{0}", mySortedList1.Capacity);
            Console.WriteLine("mySortedList1的元素个数为:{0}", mySortedList1.Count);

            //查找元素
            Console.WriteLine("mySortedList1是否包含键2:{0}", mySortedList1.ContainsKey(2));
            Console.WriteLine("mySortedList1是否包含值c:{0}", mySortedList1.ContainsValue("c")); 

            //清空元素
            mySortedList1.Clear();
            Console.WriteLine("mySortedList1的容量为:{0}", mySortedList1.Capacity);
            Console.WriteLine("mySortedList1的元素个数为:{0}", mySortedList1.Count);

            //遍历1
            Console.WriteLine("遍历1:");
            foreach( DictionaryEntry d in mySortedList2 )
            {
                Console.Write(d.Key);
                Console.Write("\t");
                Console.WriteLine(d.Value);
            }

            //遍历2
            Console.WriteLine("遍历2:");
            for (int i = 0; i < mySortedList2.Count; i++)
            {
                Console.Write(mySortedList2.GetKey(i));
                Console.Write("\t");
                Console.WriteLine(mySortedList2.GetByIndex(i));
            }

            Console.ReadLine();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -