In
.Net has a separate class for Array known as Array. This class is present in
System namespace and the class will provide methods to perform required
operations with the Arrays
Methods with System. Array class
1.Copy(Source
Array, Destination Array, int Length)
(Or)
Copy(Sourece Array Name,int Sourceindex.Destination ArrayName,Destination ArrayName)
Copy(Sourece Array Name,int Sourceindex.Destination ArrayName,Destination ArrayName)
2.Reverse(Array
name)
(Or)
Reverse(Array name, int index, int Length)
3.Sort
(Array name)
(Or)
Sort(Array name, int index, int Length)
4.BinarySearch(Array
name, object value)
(Or)
BinarySearch(Array name, int index, int Length, object value)
5.Clear(Array
Name, int index, int Length)
6.Resize(ref
Array Name,int New Size)
Copy( )
Used
To copy the elements from One Array to another Array
Consider
2 Arrays Like
A 0 1 2 3 4 5
1
|
9
|
19
|
29
|
39
|
49
|
B 0
1 2 3 4
5 6 7 8 9 10
11
10
|
20
|
30
|
40
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
Array.Copy(A,B,6)
1
|
9
|
19
|
29
|
39
|
49
|
0
|
0
|
0
|
0
|
0
|
0
|
It overwrites the existing element in B
Array.Copy(A,1,B,4,4)
B
10
|
20
|
30
|
40
|
9
|
19
|
29
|
39
|
0
|
0
|
0
|
0
|
Reverse Method:- Used to reverse the element of Array
Array.Reverse (A)
49
|
39
|
29
|
19
|
9
|
1
|
To
Reverse few elements use Array.Reverse(A,3,2)
A
1
|
9
|
19
|
39
|
29
|
49
|
Sort( ): - Used to sort the elements of Array in ascending order
Array.Sort(A)
A
1
|
9
|
19
|
29
|
39
|
49
|
To
Sort only few elements use Array.Sort(A,2,3)
A
1
|
9
|
19
|
29
|
39
|
49
|
To sort in descending order:-
For
this first sort the Array and then reverse the Array
Binary Search ( )
First Sort the Array
First Sort the Array
A 0 1 2 3 4 5
1
|
9
|
19
|
29
|
39
|
49
|
And
this will return zero or positive integer i.e. index value of the element if
elements is found. Otherwise returns negative value, if element is not found
To
find element 40 within the Array use
Array.BinarySearch (A,9)
-- 1
To
search only few elements of the Array first sort the Array
A 0
1 2 3 4 5
1
|
9
|
19
|
29
|
39
|
49
|
Use Array.BinarySearch(A,2,3,39) -- 4
If
we use Array.BinarySearch(A,25) --- -3(because
25 lies between 2 &3 It is >19<29)
Clear( )
This
method is used to store default value in the Array locations
A 0
1 2 3 4 5
1
|
9
|
19
|
29
|
39
|
49
|
Array.Clear(A,1,3)
A 0
1 2 3 4 5
1
|
0
|
0
|
0
|
39
|
49
|
Resize( )
This method is used to
change the Array size during the program
execution time.
A
1
|
9
|
19
|
29
|
39
|
49
|
Resize(ref
A,9)
1
|
9
|
19
|
29
|
39
|
49
|
0
|
0
|
0
|
Example:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
CAArrays
{
class ArrayClas
{
static
void Main()
{
int[]
A = new int[6]
{1,9,19,29,39,49};
int[]
B = new int[12]
{10,20,30,40,0,0,0,0,0,0,0,0};
Console.WriteLine("Elements of Array A are:-");
foreach
(int i in A)
{
Console.Write(i+" ");
}
Console.WriteLine("\nElements of Array B Are:-");
foreach
(int i in B)
{
Console.Write(i
+ "
");
}
Array.Copy(A,1,B,4,4);
Console.WriteLine("\nElements of Array B After Copying :-");
foreach
(int i in B)
{
Console.Write(i+" ");
}
Array.Reverse(A);
Console.WriteLine("\nElements of Array A After Reversing:- ");
foreach
(int i in A)
{
Console.Write(i+" ");
}
Array.Sort(A);
Console.WriteLine("\nElements of Array A After Sorting :- ");
foreach
(int i in A)
{
Console.Write(i+" ");
}
Console.WriteLine("\nEnter the Element to be serched ? :- ");
int
s = Convert.ToInt32(Console.ReadLine());
int
f = Array.BinarySearch(A, s);
if
(f >= 0)
{
Console.WriteLine("Array Found at {0} location:- ",
(f+1));
}
else
{
Console.WriteLine("Element Not Found");
}
Array.Clear(A,
1, 3);
Console.WriteLine("\nElements of Array A After Clearing :- ");
foreach
(int i in A)
{
Console.Write(i+" ");
}
Console.ReadLine();
}
}
}
|
No comments:
Post a Comment