Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Lấy chỉ số của giá trị chỉ định trong SortedList C#

Giới thiệu

Để lấy chỉ số của một giá trị cụ thể trong SortedList, bạn sử dụng phương thức IndexOfValue(object value). Phương thức trả về chỉ số của lần xuất hiện đầu tiên của giá trị (theo thứ tự sắp xếp theo khóa), hoặc -1 nếu không tìm thấy.

Ví dụ 1: Hai SortedList

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        SortedList list1 = new SortedList();
        list1.Add("One", 1);
        list1.Add("Two ", 2);
        list1.Add("Three ", 3);
        list1.Add("Four", 4);
        list1.Add("Five", 5);
        list1.Add("Six", 6);
        list1.Add("Seven ", 7);
        list1.Add("Eight ", 8);
        list1.Add("Nine", 9);
        list1.Add("Ten", 10);

        Console.WriteLine("Các phần tử của SortedList1...");
        foreach (DictionaryEntry d in list1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.Write("Chỉ số tại giá trị 7 = " + list1.IndexOfValue(7));

        SortedList list2 = new SortedList();
        list2.Add("A", "Accessories");
        list2.Add("B", "Books");
        list2.Add("C", "Smart Wearable Tech");
        list2.Add("D", "Home Appliances");

        Console.WriteLine("\n\nCác phần tử của SortedList2...");
        foreach (DictionaryEntry d in list2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.Write("Chỉ số tại giá trị Books = " + list2.IndexOfValue("Books"));
    }
}

Kết quả

Các phần tử của SortedList1...
Eight  8
Five   5
Four   4
Nine   9
One    1
Seven  7
Six    6
Ten    10
Three  3
Two    2
Chỉ số tại giá trị 7 = 5
Các phần tử của SortedList2...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Chỉ số tại giá trị Books = 1

Ví dụ 2: Tìm chỉ số nhiều giá trị

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        SortedList list = new SortedList();
        list.Add("One", "Finance");
        list.Add("Two", "Marketing");
        list.Add("Three", "Sales");
        list.Add("Four", "Purchase");
        list.Add("Five", "Operations");
        list.Add("Six", "IT");

        Console.WriteLine("Các phần tử của SortedList...");
        foreach (DictionaryEntry d in list)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nChỉ số tại giá trị Marketing = " + list.IndexOfValue("Marketing"));
        Console.WriteLine("Chỉ số tại giá trị Purchase = " + list.IndexOfValue("Purchase"));
        Console.WriteLine("Chỉ số tại giá trị Sales = " + list.IndexOfValue("Sales"));
        Console.WriteLine("Chỉ số tại giá trị IT = " + list.IndexOfValue("IT"));
        Console.WriteLine("Chỉ số tại giá trị Operations = " + list.IndexOfValue("Operations"));
    }
}

Kết quả

Các phần tử của SortedList...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing
Chỉ số tại giá trị Marketing = 5
Chỉ số tại giá trị Purchase = 1
Chỉ số tại giá trị Sales = 4
Chỉ số tại giá trị IT = 3
Chỉ số tại giá trị Operations = 0

Lưu ý: Nếu nhiều cặp khóa/giá trị có cùng giá trị, IndexOfValue trả về chỉ số của cặp đầu tiên theo thứ tự sắp xếp theo khóa.