What is an array?
What is an array? Learn about arrays, a powerful and versatile data structure used to store and manipulate large sets of data in programming. Find out how array elements can be created, accessed, and modified using C# examples, including one-dimensional arrays, multi-dimensional arrays, and jagged arrays.
Introduction:
An array is a data structure that stores a collection of elements, which can be of any data type, such as numbers, strings, or objects. The elements are stored in contiguous memory locations, and the elements are identified by their index, an integer value starting with 0. Arrays are frequently used in programming to store and manipulate large amounts of data efficiently and effectively.
An array’s key characteristics
An array consists of the following characteristics:
- Indexes are integer values used to identify elements in arrays.
- A contiguous memory location is used to store elements.
- Array sizes are fixed and must be specified when they are created.
- A large set of data can be accessed and manipulated efficiently using arrays.
Types of Arrays
One-dimensional arrays:
There are several types of linear arrays. Linear arrays are arrays that have one index and store a list of elements.
Example:
A one-dimensional array can store a list of elements and has only one index. Here is an example of creating and manipulating such an array in C#.
Copy code
using System;
class Program
{
static void Main()
{
// Creating a one-dimensional array of integers with a capacity of 5 elements
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
// Accessing the first element of the array Console.
WriteLine(“First element: ” + numbers[0]);
// Modifying the second element of the array numbers[1] = 10;
// Iterating through the array and print out the elements Console.
WriteLine(“Elements of the array:”);
for (int i = 0; i < numbers.Length; i++)
{ Console.WriteLine(numbers[i]); }
// Sorting the elements of the array Array.Sort(numbers);
Console.WriteLine(“Sorted elements of the array:”);
for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]);
}
// Reversing the order of the elements in the array Array.Reverse(numbers);
Console.WriteLine(“Reversed elements of the array:”);
for (int i = 0; i < numbers.Length; i++)
{ Console.WriteLine(numbers[i]); }
}
}
Firstly, we create an array containing five elements with a capacity of one-dimensional integers and initialize it with the values 1, 2, 3, and 4. Afterward, we print out the first element of the array, modify the second, and iterate through the array, printing out the rest.
To sort the array’s elements in ascending order we use the built-in Array. Sort method, and to reverse the order, we use the built-in Array. Reverse method.
It is important to note that the output will differ from the input since the array elements have been sorted and reversed.
2. Two-dimensional array:
A two-dimensional array, also known as a matrix array, is a data structure that stores a collection of elements in a tabular form, and each element is identified by its row and column indices. Integer indices are used to represent each element in the array, one for each row and one for each column. The elements are stored in contiguous memory locations and the array’s size is fixed when it’s created. It’s efficient for accessing and manipulating large sets of data to store and manipulate large sets of data in a two-dimensional array.
Example:
Two-dimensional arrays are arrays with two indices that store elements in a tabular form. Here is an example of how to create and manipulate a two-dimensional array in C#:
using System;
class Program
{
static void Main()
{
// Creating a two-dimensional array of integers with 3 rows and 4 columns
int[,] matrix = new
int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
// Accessing the element at the first row and first column Console.
WriteLine(“Element at [0,0]: ” + matrix[0, 0]);
// Modifying the element at the second row and third column matrix[1, 2] = 15;
// Iterating through the array and print out the elements Console.WriteLine(“Elements of the array:”);
for (int i = 0; i < matrix.GetLength(0); i++)
{ for (int j = 0; j < matrix.GetLength(1); j++)
{ Console.Write(matrix[i, j] + ” “); }
Console.WriteLine();
}
}
}
A two-dimensional array of integers with three rows and four columns is created and initialized with values from 1 to 12 in a tabular form.
Iterating through the array using nested loops, we print out the elements in the first row and first column, modify the elements in the second row and third column, and access them again in the next row and column.
To determine the number of rows and columns in a 2-D array, we use nested loops and the GetLength() method.
The syntax for multidimensional arrays is similar to that for simple arrays, but the number of indices will increase accordingly.
3. Multi-dimensional arrays:
They are also called matrix arrays because of their multiple indices and tabular format.
Conclusion
It is commonly used in programming to store and manipulate large amounts of data using arrays, which are powerful and versatile data structures. They are characterized by their ability to store elements in contiguous memory locations, their use of indices to identify elements and their efficiency in accessing and manipulating data. For any programmer, it is crucial to know how to create, access, and manipulate arrays.