首页 > 编程语言 > 详细

SortedList 对键排序的键/值对集合

时间:2017-03-03 14:00:35      阅读:193      评论:0      收藏:0      [点我收藏+]

定义

System.Collections.SortedList类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问。

注意:键不能为null,并且不能相同,但值可以。

键相同出错提示如下:其他信息: 已添加项。字典中的关键字:“First”所添加的关键字:“First”

注意:using System.Collections;


例子

using System;

using System.Collections;

public class SamplesSortedList

{


    public static void Main()

    {


        // Creates and initializes a new SortedList.

        SortedList mySL = new SortedList();

        mySL.Add("Third", "!");

        mySL.Add("Second", "World");

        mySL.Add("First", "Hello");

        mySL.Add("First", "my");


        // Displays the properties and values of the SortedList.

        Console.WriteLine("mySL");

        Console.WriteLine("  Count:    {0}", mySL.Count);

        Console.WriteLine("  Capacity: {0}", mySL.Capacity);

        Console.WriteLine("  Keys and Values:");

        PrintKeysAndValues(mySL);

    }



    public static void PrintKeysAndValues(SortedList myList)

    {

        Console.WriteLine("\t-KEY-\t-VALUE-");

        for (int i = 0; i < myList.Count; i++)

        {

            Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i));

        }

        Console.WriteLine();

    }

}

/*

This code produces the following output.


mySL

  Count:    3

  Capacity: 16

  Keys and Values:

    -KEY-    -VALUE-

    First:    Hello

    Second:    World

    Third:    !

*/


SortedList 对键排序的键/值对集合

原文:http://fengyuzaitu.blog.51cto.com/5218690/1902907

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!