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

C# में एरे के अच्छे उदाहरण (कॉमन एलिमेंट, डायनामिक एरे, जग्ड एरे)


उदाहरण 1: तीन सॉर्टेड एरे में कॉमन एलिमेंट ढूंढना

using System;

class Demo
{
    static void CommonElements(int[] one, int[] two, int[] three)
    {
        int i = 0, j = 0, k = 0;
        while (i < one.Length && j < two.Length && k < three.Length)
        {
            if (one[i] == two[j] && two[j] == three[k])
            {
                Console.Write(one[i] + " ");
                i++; j++; k++;
            }
            else if (one[i] < two[j]) i++;
            else if (two[j] < three[k]) j++;
            else k++;
        }
    }

    static void Main()
    {
        int[] one = {20, 35, 57, 70};
        int[] two = {9, 35, 57, 70, 92};
        int[] three = {25, 35, 55, 57, 67, 70};
        Console.Write("Common elements: ");
        CommonElements(one, two, three);
        // आउटपुट: 35 57 70
    }
}

उदाहरण 2: ArrayList से डायनामिक एरे बनाना

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList al = new ArrayList();
        al.Add(99); al.Add(47); al.Add(64);
        Console.WriteLine("Count: {0}", al.Count);
        Console.Write("List: ");
        foreach (int i in al) Console.Write(i + " ");
        // आउटपुट: 99 47 64
    }
}

उदाहरण 3: जग्ड एरे और एलिमेंट एक्सेस

using System;

namespace ArrayApplication
{
    class MyArray
    {
        static void Main(string[] args)
        {
            int[][] points = new int[][] {
                new int[] {10,5},
                new int[] {30,40},
                new int[] {70,80},
                new int[] {60,70}
            };

            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 2; j++)
                    Console.WriteLine("a[{0}][{1}] = {2}", i, j, points[i][j]);

            int x = points[0][1]; // 5
            Console.WriteLine(x);
        }
    }
}

नोट

  • आधुनिक C# में ArrayList के बजाय List<T> (जेनेरिक) प्राथमिकता दें।
  • जग्ड एरे में प्रत्येक पंक्ति अलग एरे ऑब्जेक्ट है।
  1. - C# में ऐरे क्लास क्या है

    Array क्लास C# में सभी ऐरे का बेस क्लास है। यह System नेमस्पेस में परिभाषित है। Array क्लास ऐरे के साथ काम करने के लिए विभिन्न प्रॉपर्टीज़ और मेथड्स प्रदान करती है। Array क्लास की प्रॉपर्टीज़ निम्नलिखित हैं - नीचे दी गई टेबल Array क्लास की सबसे अधिक उपयोग की जाने वाली प्रॉपर्टीज़ का वर्णन करती है।

  1. - C# में नेमस्पेसेस क्या हैं

    नेमस्पेस नामों के एक सेट को दूसरे से अलग रखने का एक तरीका प्रदान करने के लिए है। नेमस्पेस डेफिनिशन namespace कीवर्ड से शुरू होती है जिसके बाद नेमस्पेस नाम आता है - namespace namespace_name { // code declarations } नेमस्पेस डिफाइन करें - namespace namespace_name { // code declarations } निम्न

  1. - C# में ऐरे क्लास की प्रॉपर्टीज़ क्या हैं

    Array क्लास C# में सभी ऐरे का बेस क्लास है। यह System नेमस्पेस में परिभाषित है। ऐरे क्लास की प्रॉपर्टीज़ निम्नलिखित हैं - यहां Array क्लास की प्रॉपर्टीज़ हैं - Sr.No प्रॉपर्टी और विवरण 1 IsFixedSizeएक मान प्राप्त करता है जो दर्शाता है कि Array का साइज़ फिक्स्ड है। 2 IsReadOnlyएक मान प्राप्त