0% found this document useful (0 votes)
10 views7 pages

Array

Uploaded by

yosefmuluye42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Array

Uploaded by

yosefmuluye42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Array

Creating an array
// Create an array of four elements, and add values later
string[] cars = new string[4];

// Create an array of four elements and add values right away


string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"};

// Create an array of four elements without specifying the size


string[] cars = new string[] {"Volvo", "BMW", "Ford", "Mazda"};

// Create an array of four elements, omitting the new keyword, and without specifying
the size
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Length
Example 1
1, int[] myArray = { 3, 7, 2, 9, 1, 5 };

int length = myArray.Length;

Console.WriteLine("Length of the array: " + length);


Example 2

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}

GetLength
2, int[] myArray = { 3, 7, 2, 9, 1, 5 };

// Getting the length of the array's first dimension


int lengthDimension0 = myArray.GetLength(0);
Console.WriteLine("Length of dimension 0: " + lengthDimension0);

Example 2
int[,] myArray = { {3, 7, 2,8 },{2,3,4,7} };

// Getting the length of the array's first dimension


int lengthDimension0 = myArray.GetLength(0);
Console.WriteLine("Length of dimension 0: " + lengthDimension0);
GetUpperBound
3, int[] myArray = { 3, 7, 2, 9, 1, 5 };
// Getting the upper bound of the array's first dimension
int upperBoundDimension0 = myArray.GetUpperBound(0);
Console.WriteLine("Upper bound of dimension 0: " +
upperBoundDimension0); // Output: 5

Copy
4, int[] myArray = { 3, 7, 2, 9, 1, 5 };

// Creating a copy of the array


int length = myArray.Length;
int[] copyArray = new int[length];
Array.Copy(myArray, copyArray, length);
Console.WriteLine("Copied array: ");
foreach (var item in copyArray)
{
Console.Write(item + " "); // Output: 3 7 2 9 1 5
}

Binary Search

5, int[] sortedArray = { 1, 3, 5, 7, 9 };
int searchValue = 5;

int index = Array.BinarySearch(sortedArray, searchValue);


if (index >= 0)
{
Console.WriteLine($"Value {searchValue} found at index {index}");
}
else
{
Console.WriteLine($"Value {searchValue} not found. ");
}

Sorting
Example 1
6, int[] unsortedArray = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };

// Sorting the array


Array.Sort(unsortedArray);

Console.WriteLine("Sorted array:");
foreach (var item in unsortedArray)
{
Console.Write(item + " ");
}

Example 2

int[] myNumbers = {5, 1, 8, 9};


Array.Sort(myNumbers);
foreach (int i in myNumbers)
{
Console.WriteLine(i);
}
2D Arrays

7, int[,] myArray = new int[3, 4];


int totalLength = myArray.Length;

int rows = myArray.GetLength(0);


int columns = myArray.GetLength(1);
Console.WriteLine("Total number of elements in 2D array: " + totalLength);
Console.WriteLine("Number of rows: " + rows);
Console.WriteLine("Number of columns: " + columns);
8, Example 1

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };

for (int i = 0; i < numbers.GetLength(0); i++)


{
for (int j = 0; j < numbers.GetLength(1); j++)
{
Console.WriteLine(numbers[i, j]);
}
}

Example 2
int[,] myArray2D = new int[3, 4];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
myArray2D[i, j] = i + j;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
Console.WriteLine($"myArray2D[{i},{j}] = {myArray2D[i, j]}");
}
}

Example 2

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };


Console.WriteLine(numbers[0, 2]);

Example 3

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };


numbers[0, 0] = 5; // Change value to 5
Console.WriteLine(numbers[0, 0]);

You might also like