उदाहरण 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> (जेनेरिक) प्राथमिकता दें।
- जग्ड एरे में प्रत्येक पंक्ति अलग एरे ऑब्जेक्ट है।