Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में SortedList तत्वों को ऐरे ऑब्जेक्ट में कॉपी करना

SortedList.CopyTo() मेथड सॉर्टेड लिस्ट के तत्वों (DictionaryEntry ऑब्जेक्ट्स के रूप में) को एक-आयामी ऐरे में कॉपी करता है। आप लक्ष्य ऐरे में प्रारंभिक इंडेक्स निर्दिष्ट कर सकते हैं।

उदाहरण 1: इंडेक्स 2 से कॉपी करना (पहले दो स्थान खाली)

using System;
using System.Collections;

public class Demo {
    public static void Main(string[] args) {
        SortedList list = new SortedList();
        list.Add("1", "AB");
        list.Add("2", "CD");
        list.Add("3", "EF");
        list.Add("4", "GH");
        list.Add("5", "IJ");
        list.Add("6", "JK");
        list.Add("7", "KL");
        list.Add("8", "LM");

        Console.WriteLine("SortedList तत्व...");
        foreach (DictionaryEntry entry in list) {
            Console.WriteLine("Key = " + entry.Key + ", Value = " + entry.Value);
        }

        // 10 आकार का ऐरे बनाएं, इंडेक्स 2 से कॉपी करें
        DictionaryEntry[] dictArr = new DictionaryEntry[10];
        list.CopyTo(dictArr, 2);

        Console.WriteLine("\nऐरे ऑब्जेक्ट में कॉपी किया गया (इंडेक्स 2 से)...");
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

आउटपुट

SortedList तत्व...
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = JK
Key = 7, Value = KL
Key = 8, Value = LM

ऐरे ऑब्जेक्ट में कॉपी किया गया (इंडेक्स 2 से)...
Key = , Value = 
Key = , Value = 
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = JK
Key = 7, Value = KL
Key = 8, Value = LM

उदाहरण 2: इंडेक्स 0 से कॉपी करना (ऐरे आकार = लिस्ट आकार)

using System;
using System.Collections;

public class Demo {
    public static void Main(string[] args) {
        SortedList list = new SortedList();
        list.Add("1", "Katie");
        list.Add("2", "Andy");
        list.Add("3", "Mark");
        list.Add("4", "Gary");
        list.Add("5", "Sam");

        Console.WriteLine("SortedList तत्व...");
        foreach (DictionaryEntry entry in list) {
            Console.WriteLine("Key = " + entry.Key + ", Value = " + entry.Value);
        }

        // सटीक आकार का ऐरे, इंडेक्स 0 से कॉपी करें
        DictionaryEntry[] dictArr = new DictionaryEntry[5];
        list.CopyTo(dictArr, 0);

        Console.WriteLine("\nऐरे ऑब्जेक्ट में कॉपी किया गया (इंडेक्स 0 से)...");
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

आउटपुट

SortedList तत्व...
Key = 1, Value = Katie
Key = 2, Value = Andy
Key = 3, Value = Mark
Key = 4, Value = Gary
Key = 5, Value = Sam

ऐरे ऑब्जेक्ट में कॉपी किया गया (इंडेक्स 0 से)...
Key = 1, Value = Katie
Key = 2, Value = Andy
Key = 3, Value = Mark
Key = 4, Value = Gary
Key = 5, Value = Sam

महत्वपूर्ण नोट्स

  • CopyTo(Array, index) - ऐरे DictionaryEntry[] प्रकार का होना चाहिए।
  • लक्ष्य ऐरे में पर्याप्त जगह होनी चाहिए (index + Count ≤ ऐरे लंबाई)।
  • कॉपी किए गए तत्व की-सॉर्टेड क्रम में रहते हैं।
  • खाली ऐरे स्लॉट्स में डिफ़ॉल्ट DictionaryEntry (null की/वैल्यू) होते हैं।
  1. - C# में दो-आयामी सरणी से तत्वों तक कैसे पहुंचें

    2-आयामी सरणी को एक टेबल के रूप में सोचा जा सकता है, जिसमें x संख्या में पंक्तियां और y संख्या में कॉलम होते हैं। 2-आयामी सरणी में एक तत्व को सबस्क्रिप्ट्स का उपयोग करके एक्सेस किया जाता है। यानी, सरणी की पंक्ति इंडेक्स और कॉलम इंडेक्स। int x = a[1,1]; Console.WriteLine(x); आइए एक उदाहरण देखें जो द

  1. - C# में SortedList Class की Item Property क्या है?

    Sorted List, Array और Hash Table का Combination है। इसमें Items की List होती है जिन्हें Key या Index का उपयोग करके Access किया जा सकता है।SortedList में Specific Key से Associated Value को Gets और Sets करता है।नए Elements Add करने के लिए भी Item Property का उपयोग कर सकते हैं।यदि Key Exist नहीं करती,

  1. - C# में जॅग्ड सरणी के तत्वों का प्रकार क्या है?

    एक जॅग्ड सरणी सरणियों की एक सरणी है, और इसलिए इसके तत्व रेफरेंस प्रकार हैं और null से प्रारंभ होते हैं।आइए देखें कि जॅग्ड सरणी के साथ कैसे काम करें:एक जॅग्ड सरणी घोषित करें:int[][] marks;अब, इसे प्रारंभ करें, जहां marks 5 पूर्णांकों की सरणियों की एक सरणी है:int[][] marks = new int[][]{new int[]{ 40,