📜  C#|替换SortedList对象中特定索引处的值

📅  最后修改于: 2021-05-29 21:51:12             🧑  作者: Mango

SortedList.SetByIndex(Int32,Object)方法用于替换SortedList对象中特定索引处的值。

句法:

public virtual void SetByIndex (int index, object value);

参数:

异常:如果索引超出给定SortedList对象的有效索引范围,则此方法将引发ArgumentOutOfRangeException。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# code for replacing the value at a
// specific index in a SortedList object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("First", "Ram");
        mylist.Add("Second", "Shyam");
        mylist.Add("Third", "Mohit");
        mylist.Add("Fourth", "Rohit");
        mylist.Add("Fifth", "Manish");
  
        // Before replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- Before Replacing -----");
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                              mylist.GetKey(i), mylist.GetByIndex(i));
        }
  
        Console.WriteLine();
  
        // After replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- After Replacing -----");
  
        // Replaces the values at
        // index 1 and index 3.
        mylist.SetByIndex(1, "Priyanka");
        mylist.SetByIndex(3, "Ritu");
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                              mylist.GetKey(i), mylist.GetByIndex(i));
        }
    }
}

输出:

----- Before Replacing -----
Index          Keys         Values
[0]        Fifth        Manish
[1]        First        Ram
[2]        Fourth        Rohit
[3]        Second        Shyam
[4]        Third        Mohit

----- After Replacing -----
Index          Keys         Values
[0]        Fifth        Manish
[1]        First        Priyanka
[2]        Fourth        Rohit
[3]        Second        Ritu
[4]        Third        Mohit

示例2:演示ArgumentOutOfRangeException可能发生的情况

// C# code giving ArgumentOutOfRangeException 
// specific index in a SortedList object
// but giving ArgumentOutOfRangeException 
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("h", "Hello"); 
        mylist.Add("g", "Geeks!"); 
        mylist.Add("w", "Welcome");
        mylist.Add("t", "to");
        mylist.Add("n", "Noida"); 
  
        // Before replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- Before Replacing -----");
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                            mylist.GetKey(i), mylist.GetByIndex(i));
        }
  
        Console.WriteLine();
  
        // After replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- After Replacing -----");
  
        // Replaces the values at
        // index 6 which is outside 
        // the range of valid indexes
        // here it will give an
        // ArgumentOutOfRangeException
        mylist.SetByIndex(6, null);
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                            mylist.GetKey(i), mylist.GetByIndex(i));
        }
    }
}

运行时错误:

笔记:

  • 索引序列基于排序序列。添加元素后,它会以正确的排序顺序插入SortedList中,索引也会相应调整。删除元素后,索引也会相应调整。因此,在从SortedList对象中添加和删除元素后,特定键/值对的索引可能会更改。
  • 此方法是O(1)操作。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist.setbyindex?view=netframework-4.7.2