C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のSortedListオブジェクトで指定された値のインデックスを取得する


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("SortedList1 elements...");
      foreach(DictionaryEntry d in list1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.Write("Index at value 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\nSortedList2 elements...");
      foreach(DictionaryEntry d in list2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.Write("Index at value Books = "+list2.IndexOfValue("Books"));
   }
}

出力

これにより、次の出力が生成されます-

SortedList1 elements...
Eight  8
Five 5
Four 4
Nine 9
One 1
Seven  7
Six 6
Ten 10
Three  3
Two  2
Index at value 7 = 5
SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Index at value Books = 1

別の例を見てみましょう-

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("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nIndex at value Marketing = "+list.IndexOfValue("Marketing"));
      Console.WriteLine("Index at value Purchase = "+list.IndexOfValue("Purchase"));
      Console.WriteLine("Index at value Sales = "+list.IndexOfValue("Sales"));
      Console.WriteLine("Index at value IT = "+list.IndexOfValue("IT"));
      Console.WriteLine("Index at value Operations = "+list.IndexOfValue("Operations"));
   }
}

出力

これにより、次の出力が生成されます-

SortedList elements...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing
Index at value Marketing = 5
Index at value Purchase = 1
Index at value Sales = 4
Index at value IT = 3
Index at value Operations = 0

  1. C#のSortedListクラスとは何ですか?

    ソートされたリストは、配列とハッシュテーブルの組み合わせです。キーまたはインデックスを使用してアクセスできるアイテムのリストが含まれています。インデックスを使用してアイテムにアクセスする場合、それはArrayListであり、キーを使用してアイテムにアクセスする場合、それはハッシュテーブルです。アイテムのコレクションは常にキー値で並べ替えられます。 SortedList-に4つのキーと値のペアを追加した例を見てみましょう。 例 using System; using System.Collections; namespace Demo {    class Program

  2. PythonPandas-NaN値をIndexオブジェクトの指定された値で埋めます

    NaN値をIndexオブジェクトで指定された値で埋めるには、 index.fillna()を使用します パンダのメソッド。まず、必要なライブラリをインポートします- import pandas as pd import numpy as np いくつかのNaN値も使用してPandasインデックスを作成する- index = pd.Index([50, 10, 70, np.nan, 90, 50, np.nan, np.nan, 30]) パンダのインデックスを表示する- print("Pandas Index...\n",index) NaNに特定の値を入力します