Arrays in C#
Arrays
An array is a collection of variables of the same
type that are referred to by a common name.
Arrays are used for a variety of purposes because
they offer a convenient means of grouping together
related variables.
One-Dimensional Arrays (1/2)
A one-dimensional array is a list of related
variables. Such lists are common in programming.
◦ For example, you might use a one-dimensional
array to store the account numbers of the active
users on a network.
◦ Another array might store the current batting
averages for a baseball team.
One-Dimensional Arrays (2/2)
type[ ] array-name = new type[size];
int[] sample = new int[10];
int[] sample;
sample = new int[10];
int[] nums = new int[] { 99, 10, 100, 18, 78, 23, 63,
9, 87, 49 };
int[] nums = { 99, 10, 100, 18, 78, 23, 63, 9, 87, 49 };
Multidimensional Arrays
A multidimensional array is an array that has
two or more dimensions, and an individual
element is accessed through the combination
of two or more indices.
Two-Dimensional Arrays
int[,] table = new int[10, 20];
Notice that the two dimensions are separated
from each other by a comma. In the first part
of the declaration, the syntax
[,]
Initializing Multidimensional Arrays
type[,] array_name = {
{ val, val, val, ..., val },
{ val, val, val, ..., val },
...
{ val, val, val, ..., val }
};
int[,] nums = new int[2,5] {{ 99, 10, 100, 18,
78},{ 23, 63, 9, 87, 49 }};
nums[0,0]=99
nums[0,4]=78
What is the value of nums[1,3] ??
Multi-Dimensional Arrays
int[,,] table = new int[10, 20,5];
C# allows more than two-dimension arrays [,
…..,]
Write a program to enter and sum the values
on a diagonal of 3*3*3 matrix
Jagged Arrays
C# supports multidimensional arrays, which
are arrays of arrays.
Jagged arrays.
◦ Data comes in various shapes. Sometimes the
shape is uneven. With a 2D array, we might waste a
great amount of memory.
But with jagged arrays,
◦ we can store (efficiently) many rows of varying
lengths. Any type of data, reference or value, can
be used.
Example
int[][] scores = new int[2][]{new int[]{92,93,94},
new int[]{85,66,87,88}};
// Declare local jagged array with 3 rows.
int[][] jagged = new int[3][];
// Create a new array in the jagged array and assign it.
jagged[0] = new int[2];
jagged[0][0] = 1;
jagged[0][1] = 2;
Syntax: It is important to know that the pairs of
brackets indicate "jagged," and the comma in the
brackets means "2D".
Initialization
Example1:
int[][] jaggedArray = new int[5][];
//declare the size of each array, columns each row will have
jaggedArray[0] = new int[3];
jaggedArray[1] = new int[5];
jaggedArray[2] = new int[2];
jaggedArray[3] = new int[8];
jaggedArray[4] = new int[10];
//assign values
jaggedArray[0] = new int[] { 3, 5, 7, };
jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 1, 6 };
jaggedArray[3] = new int[] { 1, 0, 2, 4, 6, 45, 67, 78 };
jaggedArray[4] = new int[] { 1, 0, 2, 4, 6, 34, 54, 67, 87,
78 };
Example 2:
int[][] jaggedArray2 = new int[][]
{
new int[] { 3, 5, 7, },
new int[] { 1, 0, 2, 4, 6 },
new int[] { 1, 6 },
new int[] { 1, 0, 2, 4, 6, 45, 67, 78 }
};
Example 3:
int[][] jaggedArray3 =
{
new int[] { 3, 5, 7, },
new int[] { 1, 0, 2, 4, 6 },
new int[] {1, 2, 3, 4, 5, 6, 7, 8},
new int[] {4, 5, 6, 7, 8}
};
2D arrays with Jagged arrays
It is possible to mix jagged and multidimensional arrays
The following is a declaration and initialization of a single-
dimensional jagged array that contains two-dimensional array
elements of different sizes:
int[][,] jaggedArray4 = new int[3][,]
{
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }
};
You can access individual elements as shown in this example,
which displays the value of the element [1,0] of the first array
(value 5):
System.Console.Write("{0}", jaggedArray4[0][1, 0]);
Demonstration of jagged array of integers
1. using System;
2. namespace jagged_array
3. {
4. class Program
5. {
6. static void Main(string[] args)
7. {
8. // Declare the array of four elements:
9. int[][] jaggedArray = new int[4][];
10. // Initialize the elements:
11. jaggedArray[0] = new int[2] { 7, 9 };
12. jaggedArray[1] = new int[4] { 12, 42, 26, 38 };
13. jaggedArray[2] = new int[6] { 3, 5, 7, 9, 11, 13 };
14. jaggedArray[3] = new int[3] { 4, 6, 8 };
15. // Display the array elements:
16. for (int i = 0; i < jaggedArray.Length; i++)
17. {
18. System.Console.Write("Element({0}): ", i + 1);
19. for (int j = 0; j < jaggedArray[i].Length; j++)
20. {
21. System.Console.Write(jaggedArray[i][j] + "\t");
22. }
23. System.Console.WriteLine();
24. }
25. Console.ReadLine();
26. }
27. }
28. }
Output:
A jagged little summary.
◦ Jagged arrays are fast and easy to use once you
learn the syntax. Prefer them to 2D arrays when
performance is key. Be careful of excess memory
usage.
◦ We should be careful using methods like
GetLowerBound(), GetUpperBound, GetLength, etc.
with jagged arrays. Since jagged arrays are
constructed out of single-dimensional arrays, they
shouldn't be treated as having multiple dimensions
in the same way that rectangular arrays do.