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

Topic 04 - Array & Collections

The document provides a comprehensive overview of arrays in C#, covering their declaration, initialization, element access, modification, length, iteration, and multidimensional arrays. It also introduces common array methods and briefly discusses collections like List<T>. Additionally, several exercises are included to reinforce the understanding of array manipulation in C#.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Topic 04 - Array & Collections

The document provides a comprehensive overview of arrays in C#, covering their declaration, initialization, element access, modification, length, iteration, and multidimensional arrays. It also introduces common array methods and briefly discusses collections like List<T>. Additionally, several exercises are included to reinforce the understanding of array manipulation in C#.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Array

In C#, arrays are a fundamental data structure used to store a fixed-size sequence of
elements of the same type. Arrays are particularly useful when you need to manage
and manipulate collections of data that have a predictable and consistent size. Here’s a
detailed overview of arrays in C#, including their declaration, initialization, and
common operations.

1. Declaring Arrays
To declare an array in C#, you need to specify the type of its elements and use square
brackets `[]`. Here’s how you declare an array:

// Declaring an array of integers


int[] numbers;

// Declaring an array of strings


string[] names;

2. Initializing Arrays
After declaring an array, you need to initialize it to allocate memory. This can be done
in several ways:

1. Initializing with Fixed Size:


// Initialize an array with a fixed size of 5 elements
int[] numbers = new int[5];

2. Initializing with Values:


You can also initialize an array with a set of values:

// Initialize and assign values to an array


int[] numbers = new int[] { 1, 2, 3, 4, 5 };

// Alternatively, the `new int[]` part is optional


int[] moreNumbers = { 6, 7, 8, 9, 10 };
3. Using Array Initialization Syntax:
// Using array initializer
string[] colors = { "Red", "Green", "Blue" };

3. Accessing Array Elements


Array elements are accessed using zero-based indexing. The index specifies the
position of the element in the array.
int[] numbers = { 1, 2, 3, 4, 5 };

// Accessing the first element (index 0)


int firstNumber = numbers[0]; // 1

// Accessing the third element (index 2)


int thirdNumber = numbers[2]; // 3

4. Modifying Array Elements


You can modify the value of an element at a specific index:
int[] numbers = { 1, 2, 3, 4, 5 };
// Changing the value of the second element (index 1)
numbers[1] = 10; // Array is now { 1, 10, 3, 4, 5 }

5. Array Length
The `Length` property returns the number of elements in the array.
int[] numbers = { 1, 2, 3, 4, 5 };
int length = numbers.Length; // 5

6. Iterating Over Arrays


You can use various techniques to iterate over arrays, such as `for` loops, `foreach`
loops, and LINQ.

1. Using `for` Loop:


int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}

2. Using `foreach` Loop:


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

foreach (int number in numbers)


{
Console.WriteLine(number);
}

7. Multidimensional Arrays
C# supports multidimensional arrays, such as two-dimensional arrays, which are
essentially arrays of arrays.

1. Two-Dimensional Array:
// Declaring and initializing a 2D array
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

// Accessing elements
int value = matrix[1, 2]; // 6 (second row, third column)

2. Jagged Arrays:
Jagged arrays are arrays of arrays and can have different lengths for each array.
// Declaring and initializing a jagged array
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6 };
// Accessing elements
int value = jaggedArray[1][2]; // 5

8. Common Array Methods


1. `Array.Sort()`:
Sorts the elements of the array.
int[] numbers = { 5, 2, 8, 1, 3 };
Array.Sort(numbers); // Array is now { 1, 2, 3, 5, 8 }

2. `Array.Reverse()`:
Reverses the order of elements in the array.
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Reverse(numbers); // Array is now { 5, 4, 3, 2, 1 }

3. `Array.Copy()`:
Copies a range of elements from one array to another.
int[] source = { 1, 2, 3, 4, 5 };
int[] destination = new int[3];
Array.Copy(source, 1, destination, 0, 3); // destination is { 2, 3, 4 }

Collection
These collections are part of the System.Collections and System.Collections.Generic
namespaces. They include collections like lists, dictionaries, queues, and stacks.
1.1. List<T>
is a dynamic array that grows as needed. It provides fast indexed access to
List<T>
elements and supports a wide range of methods for manipulating data.
Example:
using System;using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Add an element
numbers.Add(6);

// Insert an element at a specific index


numbers.Insert(2, 10);
// Access elements
int firstNumber = numbers[0]; // 1

// Iterate over the list


foreach (int number in numbers)
{
Console.WriteLine(number);
}

// Remove an element
numbers.Remove(10);
numbers.RemoveAt(0); // Remove by index
}
}

Exercise:
 Write a C# program that takes an array of integers as input, reverses the order of
elements in the array, and prints the reversed array.
 Write a C# program that removes all duplicates from a given array of integers and
prints the resulting array.
 Write a C# program that finds and prints the maximum and minimum elements in
a given array of integers.
 Write a C# program that counts the frequency of each element in an array of
integers and displays the result.

You might also like