An array is a set of similar types of values, that are stored in sequential order. An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.
In C#, an array index starts at zero. That means the first item of an array starts at the zero position. The position of the last item on an array will total a number of items -1. So if an array has 10 items, the last 10th item is at 9th position.
In c# array can be declared as fixed-length or dynamic, a fixed-length array can store a predefined number of items while the size of dynamic arrays increases as you add, number of items/new items to the array.
foreach loopIn C#, an array index starts at zero. That means the first item of an array starts at the zero position. The position of the last item on an array will total a number of items -1. So if an array has 10 items, the last 10th item is at 9th position.
In c# array can be declared as fixed-length or dynamic, a fixed-length array can store a predefined number of items while the size of dynamic arrays increases as you add, number of items/new items to the array.
- To access an element we can use the array name and an index value of the concern element.
- In C# .Net Arrays are Reference Types
C# .Net will support 3 types of arrays.
- One-dimensional Array
- Multi-dimensional Array
- Jagged Array
Array Class
This class is defined under the System Namespace, which contains a set of properties, methods using which we can perform operations on an Array.
Properties of Array:
Length:-Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Rank: Gets the r4ank(number of dimestensions) of the Array. For example, a one-dimensional array returns 1, a two-dimesnational array returns 2, and so on.
Methods of Array:
Clear():-Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
Copy(Array, Array, Int32):-Copies a range of elements from an Array starting at the first element and pastes them into another array starting at the first elements. The length is specified as a 32- bit integer.
GetLength():- Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
IndexOf(Array, Object):-Searches for the specified object and returns the index of the first occurrence within the entire one dimensional Array
LatIndexOfd(Array, Object):- Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.
Reverse(Array):- Reverse the sequence of the elements in the entire one dimensional, Array.
Sort(Array):-Sort s the elements in an entire one - dimensional Array using the Incomparable implantation of each element of the Array.
ToString ():- Returns a string that represents the current object(Inherited from Object)
One-dimensional Array:
Arranging a collection of elements in a single row can be called a One-Dimensional Array. And it will be declared as follows.
syntax:
<type>[] <name>=new <type>[size]:
int[] arr = new int[4];
or
int[] arr;
arr=new int[4];
or
int[] arr = { list of values };
Example:
A simple program using one-dimensional Array:
OneArraySample.cs:
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
int i;
for (i = 0; i < 10; i = i + 1)
{
arr[i] = i;
}
for (i = 0; i < 10; i = i + 1)
{
Console.WriteLine("arr[" + i + "]:" + arr[i]);
}
Console.ReadLine();
}
}
}
Output:
arr[0]: 0
arr[1]: 1
arr[2]: 2
arr[3]: 3
arr[4]: 4
arr[5]: 5
arr[6]: 6
arr[7]: 7
arr[8]: 8
arr[9]: 9
While processing the value of an Array or Collection using a foreach loop for each iteration of the loop. One value of the array is returned to you in sequential order.
Difference between for and foreach
In the case of a loop, the variable of the loop refers to the index of your array, whereas in case of foreach loop the variable of the loop refers to the values of the array.
In the case of for loop the variable should always be 'int' only, but in case of foreach loop, the loop variable type will be the same as the type of values in the Array.
Note: - An array is a reference type because it gives us the option to initialize an array after declaration also.
Array class
Under the System namespace, we are provided with a class known as Array, which provides a set of Methods and Properties that can be used for Manipulating an Array.
- Sort (array)
- Reverse (array)
- Copy (src, dest, n)
- GetLength (index)
- Length
Eg:-
class SDArray2
{
static void Main()
{
int[] arr = { 34, 90, 29, 15, 62, 78, 32, 4, 72, 94, 89, 5, 16 }
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
Array.sort(arr);
foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Array.Reverse(arr);
foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine();
int[] brr = new int[10];
Array.Copy(arr, brr, 5);
foreach (int i in brr)
Console.Write(i + " ");
}
}
We can take input into a program in two different ways.
1) Using the ReadLine method of Console class
2) Using CommandLine Parameters
In the second case, while executing a program from the command prompt, we can supply values to that program from the Command Prompt, where all the values we supply will be taken into the "String array" of the Main method, provided is present.
To check the process writes the following program
class ParasDemo
{
static void Main(string[] args)
{
foreach (string str in args)
Console.WriteLine(str);
}
}
After compiling the program execute a program from the command prompt and check the result.Eg: - C:\CSharp6>ParasDemo 10 Hello True 3.14
Eg: -
class AddParams
{
static void Main(string[] args)
{
int sum = 0;
foreach (string str in args)
sum + = int.Parse(str);
Console.WriteLine(sum);
}
}
These arrays store the data in the form of Rows and Columns.
Syntax:-
<type>[,] <name> = new<type>[rows, cols];
int[,] arr = new int[3, 4];
Or
int[,] arr;
arr = new int[2, 3];
or
int[,] arr = (list of values);
Eg:-
using System;
class TDArray
{
static void Main()
{
int[,] arr = new arr[4, 5];
int x = 5;
//Assigning values to 2DArray:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = x;
x + = 5;
}
}
//Printing values of 2DArray:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); J++)
Console.Write(arr[i, j] + " ");
Console.WriteLine();
}
}
}
Assigning values to 2DArray at the time of execution
int[,] arr =
{
{11, 12, 13, 14},
{21, 22, 23, 24},
{19, 20, 21, 22},
};
Jagged Arrays
These are also similar to Two Dimensional arrays, which contains data in the form of Rows and Columns. But here the number of columns to each row will be varying.
These are also called as Array of Arrays because Multiple single dimensional arrays are combined together to form a new Array.
Syntax:-
<type>[][]<name> = new <type>[rows][];
int[][] arr = new[3][];
Or
int[][] arr = {list of values};
In the initial declaration of a jagged array, we can also specify the number of rows to the array, but not columns because the column size is not fixed.
After specifying the number of Row to the Array now pointing to each row, we need to specify the number of columns we want for the Row.
Eg:-
int [][] arr = new int[4][];
arr [0] = new int[5];
arr [1] = new int[6];
arr [2] = new int[8];
arr [3] = new int[4];
Eg:-
class JaggedDemo
{
static void Main()
{
int[][] arr = new int[4][];
arr[0] = new int[5];
arr[1] = new int[6];
arr[2] = new int[8];
arr[3] = new int[4];
//Assigning values to jagged array:
for (int i = 0; i < arr[i].Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
arr[i][j] = j + 1;
}
//Printing values of jagged array:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr[i].Length; j++)
Console.Write(arr[i][j] + " ");
Console.WriteLine();
}
}
}
We can print values of a jagged array like this also
for(int i= 0;i<arr.GetLength(0); i++)
{
foreach (int x in arr[i])
Console.Write(x + " ");
Console.WriteLine();
}
Assigning values to Jagged array at the time of execution
int[]
[]
arr = {
new int[3] { 11, 12, 13 }
new int[5] { 21, 22, 23, 24, 25 }
new int[4] { 31, 32, 33, 34 }
new int[2] { 41, 42 }
}
No comments:
Post a Comment