Arrays
Arrays are good for retrieving and updating values, but not so good for finding, adding, and deleting values.
Array is the simplest collection in C#. It uses an Index and Value.
Initialize an Array
There are couple ways to initialize an array.
int[] lockCombination = new int[3];
int[] lockCombination = new int[] {10, 5, 32};
int[] lockCombination = new[] {10, 5, 32};
int[] lockCombination = {10, 5, 32};
Inserting data in Array
Inserting data in array, starts from declaring the array variable but do not assigned it with data.
int[] numbers = new int[5];
numbers[0] = 10; //store 10 in array index 0
numbers[5] = 20; //error: index was outside the bounds
Accessing data in Array
Accesing the data in array is pretty simple, just call the index of the array.
lockCombination[2] //result: 32
Updating data in Array
Updating the data acts as adding the data in array. Updating means replacing the current data in array.
int[] numbers = new int[5];
numbers[0] = 10; //store 10 in array index 0
numbers[0] = 20; //array contains 20 in index 0
Deleting data in Array
Code shown below will deleting all data in array
Array.Clear(numbers, 0, 5); //deleting from index 0 with length 5
Jagged Array
Jagged Array works by storing an array inside an array, called array of array that could be differ in inner array length.
Initialize the Jagged Array.
int[][] numbers = new int [50][];
// results
// { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
// null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, nu
// ll, null }
Initialize the inner array.
numbers[0] = new int[2];
// results
// { { 0, 0 }, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
// null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, nu
// ll, null }
Updating the data in inner array.
numbers[0][1] = 2;
// results
// { { 0, 2 }, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
// null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, nu
// ll, null }
Accessing the data in inner array.
numbers[0][1]; //result: 2
numbers[10]; //result: null
numbers[10][1]; //error: object reference not set
Multidimentional Array
Multidimentional Array works by storing an array inside an array, called array of array that couldn't be differ in inner array length.
Initialize the Multidimentional (2D) Array.
int[,] numbers = new int [50,2];
Updating the data in inner array.
numbers[4, 1] = 5;
numbers[4, 1]; //result: 5
numbers[4, 0]; //result: 0
numbers[5, 2]; //result: 0
Notes:
- Jagged array could have a flexible length in each inner array, Multidimentional Array only have a fixed length of array.
- Multidimentional Array use less memory than Jagged Array with same size.
- Jagged Array have better element access performance.
Disadvantage of Array:
- Need to initialize the length in beginning. Therefore, we couldn't add data that exceeded the length of array.
- Solution: create a larger array. Use
CopyTo
method to copy data in old array into new array.
- Solution: create a larger array. Use
- Same drawbacks when needed to shift the data in adjacent index.
- The unused index that has been initialize still consume memory altough it isn't stored by some data.