Arrays

Overview

  • Inherit from System.Array class
  • Is a reference type object
    int[] a = null;
  • Is has fixed number of variables
  • Arrays are stored in continous block of memory
  • Arrays are denoted with square brackets after the element type
    char[] vowels = new char[5];
  • Square brackets are used to access the index of the array

Enumeration

  • For loop
    for (int i = 0; i < vowels.Length; i++)
    Console.Write (vowels[i]); // aeiou
    
  • Foreach loop
    int[] myArray = {1,2,3};
    foreach (int val in myArray)
         Console.WriteLine(val);
    

    Using static Array

    Array.ForEach (new[] {1, 2, 3 }, Console.WriteLine); 

Property

Length Number of elements in the array
LongLength Total number of elements in the array
Rank Returns number of dimensions in array

Methods

GetLength() Length for a given dimension
GetLongLength Length for a given dimension
GetLowerBound Useful with nonzero indexed arrays
GetUpperBound Returns same result as adding GetLowerBound to GetLength
Clone() Duplicate Array

arrayB = arrayA.Clone();

Array Initialization

char[] vowels = new char[] {'a','e','i','o','u'};
char[] vowels = {'a','e','i','o','u'};

Default Element Initialization

The default value for a type is the result of a bitwise zeroing of memory.

Value types versus reference types in Array
When the element type is a value type, each element value is allocated as part of the array.
For example:

public struct Point { public int X, Y; }
...
Point[] a = new Point[1000];
int x = a[500].X; // 0

Creating array for class object will allocate null, therefore you cannot access the value directly instead you will need to use iteration using for each loop to access the object value.

public class Point { public int X, Y; }
...
Point[] a = new Point[1000];
int x = a[500].X; // Runtime error, NullReferenceException

Multidimensional Arrays

  1. Rectangular - n-dimensional block of memory
    Declared using commas to separate each dimension

    int[,] matrix = new int[3,3];

    Iteration

    for (int i = 0; i < matrix.GetLength(0); i++)
    for (int j = 0; j < matrix.GetLength(1); j++)
    matrix[i,j] = i * 3 + j;
    

    Initialization

    int[,] matrix = new int[,]
    {
    {0,1,2},
    {3,4,5},
    {6,7,8}
    };
    
  2. Jagged - arrays of arrays
    Declared using successive square brackets to represent each dimension

    int[][] matrix = new int[3][];

    Each inner array can be an arbitrary length
    Each inner array is implicitly initialized to null rather than an empty array

    for (int i = 0; i < matrix.Length; i++)
    {
    matrix[i] = new int[3]; // Create inner array
    for (int j = 0; j < matrix[i].Length; j++)
    matrix[i][j] = i * 3 + j;
    }
    

    Initialization

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

Simplified Array Initialization Expressions

  1. Omit new operator
    char[] vowels = {'a','e','i','o','u'};
    int[,] rectangularMatrix =
    {
    {0,1,2},
    {3,4,5},
    {6,7,8}
    };
    
    int[][] jaggedMatrix =
    {
    new int[] {0,1,2},
    new int[] {3,4,5},
    new int[] {6,7,8}
    };
    
  2. Use var keyword
    var i = 3; // i is implicitly of type int
    var s = "sausage"; // s is implicitly of type string
    // Therefore:
    var rectMatrix = new int[,] // rectMatrix is implicitly of type int[,]
    {
    {0,1,2},
    {3,4,5},
    {6,7,8}
    };
    var jaggedMat = new int[][] // jaggedMat is implicitly of type int[][]
    {
    new int[] {0,1,2},
    new int[] {3,4,5},
    new int[] {6,7,8}
    };
    

You can also let the compiler infer the array type

var vowels = new[] {'a','e','i','o','u'}; // Compiler infers char[]

Bounds Checking
All array indexing is bounds-checked by the run-time
IndexOutOfRangeException thrown if invalid index

Searching
BinarySearch methods

For rapidly searching a sorted array for a particular item

IndexOf / LastIndex methods

For searching unsorted arrays for a particular item

Find / FindLast / FindIndex / FindLastIndex / FindAll / Exists / TrueForAll

For searching unsorted arrays for item(s) that satisfy a given Predicate

Leave a Reply

Your email address will not be published. Required fields are marked *