Array Class :
There is a pre-defined class called "Array". C# recognizes every array as an object for the "System.Array" class. As an advantage of this, every array can access the members for that Array class. For common programming needs, the Array class offers few properties and few methods, which are useful for general manipulations of arrays.Here, you can see the available properties of array class in this table:
Manipulations on Arrays
Copying arrays:
Syn: arrayname.CopyTo(DestinationArray, StartIndex);
Ex: a.CopyTo(b, 0);
Note: The size and dimensions of both a and b arrays, should be same. It copies all the elements into the destination array.
Sorting arrays:
Syn: Array.Sort(arrayname);
Ex: Array.Sort(a);
Note: Sort is a static method of Array class. It sorts all the array elements in ascending order.
Reversing arrays:
Syn: Array.Reverse(arrayname);
Ex: Array.Reverse(a);
Note: Reverse‖ is a static method of Array class. It exactly reverses the all the array elements.
Searching arrays for an element:
Syn: Array.IndexOf(arrayname, value);
Ex: Array.IndexOf(a, 50);
Note: IndexOf‖ is a static method of Array class. It searches the array for the given value, and returns it‘s index if it is found; otherwise it returns -1;
Program for Single Dim Array
namespace App_46_Array_Class
{
class Program
{
static void Main(string[] args)
{
//single dimensional array
Console.WriteLine("Single dimensional array:");
int[] a = { 643, 32948, 39, 23094, 112 };
foreach (int i in a)
Console.Write(i + " ");
Console.WriteLine("\nLength: " + a.Length);
Console.WriteLine("Rank: " + a.Rank);
//double dimensional array
Console.WriteLine("\nDouble dimensional array:");
int[,] b = { { 56, 1238, 8812 }, { 99, 2784, 2892 } };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
Console.Write(b[i, j] + " ");
Console.WriteLine();
}
Console.WriteLine("Length: " + b.Length);
Console.WriteLine("Rank: " + b.Rank);
//string array
Console.WriteLine("\nString array:");
string[] s = { "Java", "Corba", "C++", "Perl", "AJAX", "JavaScript" };
foreach (string i in s)
Console.Write(i + " ");
//copying arrays
Console.WriteLine("\n\nSingle dim array after copy:");
int[] c = new int[5];
a.CopyTo(c, 0);
foreach (int i in c)
Console.Write(i + " ");
//sorting arrays
Array.Sort(c);
Array.Sort(s);
Console.WriteLine("\n\nSingle dim array after sorting:");
foreach (int i in c)
Console.Write(i + " ");
Console.WriteLine("\n\nString array after sorting:");
foreach (string i in s)
Console.Write(i + " ");
//reversing arrays
Array.Reverse(c);
Array.Reverse(s);
Console.WriteLine("\n\nSingle dim array after reversing:");
foreach (int i in c)
Console.Write(i + " ");
Console.WriteLine("\n\nString array after reversing:");
foreach (string i in s)
Console.Write(i + " ");
Console.WriteLine("\n");
//searching an element
Console.WriteLine("Index of 'Java' in the string array is : " + Array.IndexOf(s,
"Java"));
Console.Read();
}
}
}
Download Above Code for Demo of Single Dim Array : Download Links
No comments:
Post a Comment