Author : MD TAREQ HASSAN | Updated : 2021/03/22
Understanding array
- array is a collection of a fixed number of values of same type & referred by a common name
- an array is a data structure which can store multiple elements of same data type
- specific element of an array is accessed by an index
- array elements are stored in contiguous memory locations
Types:
- 1-d
- 2-d (matrix)
- multi-d (nested array, jagged array)
1d-array-allocated-in-memory
2d-array-allocated-in-memory
Row-major order in 2d array
- in computing, row-major order and column-major order describe methods for storing multidimensional arrays in linear memory
- row-major order is a method of representing multi dimension array in sequential memory
- reading memory in contiguous locations is faster than jumping around among locations. Therefore row-major is better and used by most compiler (C and C++ use row-major, Fortran and matlad uses column major)
Finding array element by row-major
offset = row * columnSize + column
Array syntax
// Creating array
int[] foo = new int[10];
var foo = new int[10]; // type inferred
int[,] foo = new int[10, 5]; // multi-dimentional array
var foo = new int[10, 5]; // type inferred
var foo = Array.Empty<int>();
var my1DArray = Array.CreateInstance(typeof(int), 10);
var array = Enumerable.Repeat(String.Empty, 10).ToArray(); // Creating Array From Enumerable with same default value
var array = itemList.ToArray(); // from any IEnumerable
// Initialization
var foo = new int[] { 1, 2 }; // dimention is inferred
int[] foo = { 1, 2 }; // dimention is inferred
var foo = new int[2] { 1, 2 }; // enforcing dimention
var arr = new int[,] { {1, 2, 3}, {4, 5} }; // dimention is inferred
int[,] arr = { {1, 2, 3}, {4, 5} }; // dimention is inferred
// Properties
arr.Length
arr.IsReadOnly
// Array class static methods
Array.Sort(arr);
Array.BinarySearch(<overloads>)
Array.Reverse(arr);
Array.Resize(ref arr, n);
Array.Exists<T>(arr, Predicate<T>)
Array.Find(arr, Predicate<T>);
Array.ForEach<T>(arr, Action<T>)
Links
Jagged array
// Jagged array - array of arrays
int[][] jaggedArray = new int[n][]; // 2nd diention is variable, can't be specified
var jaggedArray = new int[][]{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
Readonly array
Array.AsReadOnly(T[]) // Returns a read-only wrapper for the specified array.
var items = new int[] {1 ,2, 3, 4}
var readOnlyItems = Array.AsReadOnly(items)
Array to string
var words = new string[] { "hover", "mind" }; // String.Join(string separator, params string[] values);
var str = String.Join(" ", words);
List<int> primes = GetPrimes(count: 20);
var primeString = String.Join(" ", primes) // String.Join(string separator, params object[] values);
String to array
string[] words = sentence.Split(" ");
string[] words = sentence.Split(new string[]{ " ", "," });
string[] words = sentence.Split(" ", StringSplitOptions.None);
string[] words = sentence.Split(" ", StringSplitOptions.RemoveEmptyEntries);
string[] words = sentence.Split(new char[]{ ' ' });
string[] words = sentence.Split(new char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries);
string[] words = sentence.Split(",;.".ToCharArray());
var word = "hovermind"
char[] chars = word.ToCharArray()
Array to list
var list = new List<T>(array);
var list = array.ToList();
var list = new List<T>();
list.AddRange(array);
List to array
string[] arr = list.ToArray();
Array to dictionary
var dict = array.ToDictionary(key => key, value => value); // key == value == each item
// to avoid duplicate key, use index as key
var dictionary = array.Select((v, i) => new {Key = i, Value = v}).ToDictionary(o => o.Key, o => o.Value);
// to avoid duplicate key, use hashset
var hashset = new HashSet<string>(array);
var dict = hashset.ToDictionary(key => key, value => value);
Dictionary to array
var array = dict.Values.ToArray();
var array = dict.Keys.ToArray();
T[] array = new T[dict.Count];
dict.Values.CopyTo(array, 0); // starting from 0 index
// using LINQ - has overhead (calling Lambda each time)
var array = dict.Select(pair => pair.Value).ToArray(); // CopyTo() is faster than ToArray()