ARRAYS in c#
ARRAYS in c#
Ms. Nausheeda B S
Introduction
• An array is a group of contiguous or
related data items that share a common
name.
• Marks[10]
2
• The complete set of values are called as an
array
• The individual values are called as elements
• Arrays can be of any variable type
3
One-dimensional array
• A list of items can be given one variable name
using only one subscript – single-subscripted
variable or one-dimensional array
• Storage....
Number[0] = 35;
Number[1]=40;
Number[2]=20;
Number[3]=57;
Number[4]=19; 4
• The array variables can be used in programs
like any other C# variable
Number = number[0] + 10;
Number[4]= number[0] + number[2];
Number[2] = x[5] + y[10];
Value[6] = number[i] * 3;
5
Creating an array
1. Declaring the array
2. Creating memory locations
3. Putting values into memory locations
7
• Initialization of arrays: To put values into the array
Example:
Number[0]=35;
Number[1] = 40;
......
Number[4]=19;
Console.WriteLine(Number[0]);
8
Can also be initialized as
type[] arrayname = {list of values} ;
Console.WriteLine(Number[3]);
10
Two-dimensional Arrays
• Creation:
int[ , ] myArray;
myArray = new int[3, 4];
Or
int[ , ] myArray = new int[3,4];
11
• int[,] x= new int[2][3]
• int[ , ] x = new int[2, 3]{ {1, 2, 3}, {3, 4, 5} };
or
• int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Referring to a variable:
int value = table [1, 2]; Console.WriteLine(value); 13
Variable-size arrays
• Multidimensional arrays – arrays of arrays
int[] [] x = new int [3][, ];
X[0] = new int [2];
X[1]= new int [4];
X[2] = new int [3];
15
for (int i = 0; i < arr.Length; i++)
{
Console.Write("Row " + i + ": ");
foreach(int num in arr[i])
Console.Write(num + " ");
Console.WriteLine();
}
Console.WriteLine(arr[0][1]);
arr[0][1] = 10;
Console.WriteLine(arr[0][1]);
Console.WriteLine(arr[1][2]);
16
output
Row 0: 1 3 5 7 9
Row 1: 2 4 6 8
3
10
6
17
table[2,2,3]
• int[ , ,] table = { {{0,0,0}, {1,1,1}} , {{0,0,0}, {1,1,1}} }
or
• int[ , ,] table = {
{{0,0,0}, {1,1,1}},
{{0,0,0}, {1,1,1}}
};
Console.WriteLine(table[0,1,2]);
int[ , , ] numbers = {
{ { 1, 3, 5 }, { 2, 4, 6 } },
{ { 2, 4, 9 }, { 5, 7, 11 } }
};
Console.WriteLine(numbers[0,1,2]); 18
int[][] jaggedArray = new int[3][];
jaggedArray[0] = {1, 3, 5, 7, 9};
jaggedArray[1] = {0, 2, 4, 6};
jaggedArray[2] = {11, 22};
int[][] jaggedArray2 = {
{1, 3, 5, 7, 9},
{0, 2, 4, 6},
{11, 22}
};
// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray2[0][1] = 77;
// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray2[2][1] = 88; 19
int[][,] jaggedArray3 ={
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }
};
Console.Write("{0}", jaggedArray3[0][1, 0]);
Console.WriteLine(jaggedArray3.Length);
20
using System;
public class ABC
{
public static void Main(string[] args)
{
// Declaration and Initialization
// with Multidimensional Array
int[][,] arr = new int[2][,] {
new int[, ] { { 1, 3 }, { 5, 7 } },
new int[, ] { { 0, 2 }, { 4, 6 }, { 8, 10 } }
};
21
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].GetLength(0); j++)
{
for (int k = 0; k < arr[i].GetLength(1); k++)
{
Console.Write("arr[" + i + "][" + j
+ ", " + k + "] => "
+ arr[i][j, k] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
}
22
output
• arr[0][0, 0] => 1 arr[0][0, 1] => 3
• arr[0][1, 0] => 5 arr[0][1, 1] => 7
23
System.Array Class
• Every array is automatically derived from
System.Array class
• Defines methods and properties that can be
used to manipulate arrays more efficiently
24
using System;
class prgm {
static void Main(String[] args)
{
// Declare and initialize an array of integers
int[] arr = { 10, 20, 30, 40, 50 };
26
ArrayList Class
• System.Collections namespace defines a class
– ArrayList that can store a dynamically sized
array of objects
Default size = 4
28
Remove an element:
cities.RemoveAt(1);
Modify capacity
cities.Capacity = 20;
Actual number of objects present in list
Int n = cities . Count;
29
using System;
using System.Collections;
class program {
static void Main(string[] args)
{
ArrayList al = new ArrayList(); // Creating an ArrayList
class program {
static void Main(string[] args)
{
ArrayList aL = new ArrayList(); // Creating an ArrayList
32
class prgm{
public static void Main(String[] args)
{
ArrayList myList = new ArrayList(); // Creating an
ArrayList
// Adding elements to ArrayList
myList.Add("A");
myList.Add("B");
myList.Add("C");
myList.Add("D");
myList.Add("E");
myList.Add("F");
33
if (myList.Contains("E"))
Console.WriteLine("Yes, exists at index " + myList.IndexOf("E"));
else
Console.WriteLine("No, doesn't exists");
}
}
34