0% found this document useful (0 votes)
3 views

ARRAYS in c#

The document provides a comprehensive overview of arrays in programming, detailing their structure, types (one-dimensional, two-dimensional, jagged), and methods for declaration, initialization, and manipulation. It also introduces the System.Array class and the ArrayList class, highlighting their functionalities and methods for handling dynamic arrays. Key concepts include array length, element access, and various operations such as sorting and removing elements.

Uploaded by

24250155nikesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ARRAYS in c#

The document provides a comprehensive overview of arrays in programming, detailing their structure, types (one-dimensional, two-dimensional, jagged), and methods for declaration, initialization, and manipulation. It also introduces the System.Array class and the ArrayList class, highlighting their functionalities and methods for handling dynamic arrays. Key concepts include array length, element access, and various operations such as sorting and removing elements.

Uploaded by

24250155nikesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

ARRAYS

Ms. Nausheeda B S
Introduction
• An array is a group of contiguous or
related data items that share a common
name.

• A particular value is indicated by writing a


number called index number or subscript
in brackets after the array 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

• Declaration : Type [] arrayname;


• Example:
– Int[] counter;
– Float[] marks;
– Int[] x, y;
6
• Creation of arrays: Using new operator only
Arrayname = new type[size];
Example:
number = new int[5];
average = new float[10];

int[] Number = new int[5];

7
• Initialization of arrays: To put values into the array

• Using array subscripts


Arrayname[subscript] = value;

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} ;

int[] Number = {35,40,20,57,19};


or
int[] Number = new int [5] {35,40,20,57,19};

Console.WriteLine(Number[3]);

int[] Number1 = {1, 2,3};


int[] Number2;
Number2=Number1 ; //valid
9
Array Length
• In C#, all arrays are class-based and store the
allocated size in a variable named Length
• Int asize = a.Length;

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 } };

// access first element from first row


• Console.WriteLine(x[0,0]); // returns 1

// access third element rom second row


12
• x[1, 2]; // returns 5
• Initialization:
int[ , ] table = { {0,0,0}, {1,1,1} }
or
int[ , ] table = {
{0,0,0},
{1,1,1}
};
Console.WriteLine(table[1,2]);

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];

Also known as jagged arrays


X[1][1] = 10;
int y = X[2][2]; 14
Jagged arrays
• Declare the array of two elements.
datatype[][] name_of_array = new datatype[rows][]
int[][] arr = new int[2][];

• Initialize the elements.


arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };

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

• arr[1][0, 0] => 0 arr[1][0, 1] => 2


• arr[1][1, 0] => 4 arr[1][1, 1] => 6
• arr[1][2, 0] => 8 arr[1][2, 1] => 10

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 };

// Print each element of the array


Console.WriteLine("Array elements are:");
foreach(int i in arr) {
Console.WriteLine(i);
}
}
} 25
Method / Purpose
Property
Clear() Sets a range of elements to empty values

CopyTo() Copies elements from source array into destination array

GetLength() Gives the number of elements in a given dimension of


array
GetValue() Gets the value for a given index in the array

Length Gives the length of an array

SetValue() Sets the value for a given index in the array

Reverse() Reverses the contents of one-dimensional array

Sort() Sorts the elements in a one-dimensional array

26
ArrayList Class
• System.Collections namespace defines a class
– ArrayList that can store a dynamically sized
array of objects

• Includes a number of methods to support


operations like sorting, removing and
enumerating its contents

• Has properties Count and Capacity – modify /


read capacity 27
using System;
using System.Collection
• Similar to array + grow dynamically
ArrayList cities = new ArrayList(30);
• Add elements using the Add() method
cities.Add(“Mumbai”);
cities.Add(“Goa”);

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

// Adding elements to the ArrayList


al.Add(1);
al.Add(2);
al.Add(3);

foreach(var item in al) {


Console.WriteLine(item);
}
}
} 30
Some important ArrayList methods and properties
Methods / Property Purpose
Add() Adds an object to a list
Clear() Removes all elements from the list
Contains() Determines if an element is in the list
CopyTo() Copies a list to another
Insert() Inserts an element into the list
Remove() Removes the first occurrence of an element
RemoveAt() Removes the element at the specified place
RemoveRange() Removes a range of elements
Sort() Sorts the elements
Capacity Gets or sets the number of elements in the list
Count Gets the number of elements currently in the list
31
using System;
using System.Collections;

class program {
static void Main(string[] args)
{
ArrayList aL = new ArrayList(); // Creating an ArrayList

// Adding elements to ArrayList


aL.Add("Hello");
aL.Add("World");

Console.WriteLine("Total number of elements in an ArrayList : " + aL.Count);


Console.WriteLine("Capacity of an ArrayList : " + aL.Capacity);
}
}

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

You might also like