- Matrices and Multidimensional Arrays
- Declaring
- Usage

- Jagged Arrays
- Declaring
- Usage

- The
*Array*Class- Sorting
- Binary Search

*Multidimensional arrays*have more than one dimension (2, 3, …)- The most important multidimensional arrays are the 2-dimensional
- Known as
*matrices*or*tables*

- Known as

- The most important multidimensional arrays are the 2-dimensional
- Example of matrix of integers with 2 rows and 4 columns:

5 | 0 | -2 | 4 |
---|---|---|---|

5 | 6 | 7 | 8 |

- Declaring multidimensional arrays:
- Creating a multidimensional array
- Use
*new*keyword - Must specify the size of each dimension

- Use

- Creating and initializing with values multidimensional array:
- Matrices are represented by a list of rows
- Rows consist of list of values

- The first dimension comes first, the second comes next (inside the first)

- Matrices are represented by a list of rows

- Jagged arrays are like multidimensional arrays
- But each dimension has different size
- A jagged array is array of arrays
- Each of the arrays hasdifferent length

- How to create jagged array?

- When creating jagged arrays
- Initially the array is created of
*null*arrays - Need to initialize each of them

- Initially the array is created of

- Check a set of numbers and group them by their remainder when dividing to 3 (0, 1 and 2)
- Example: 0, 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2
- First we need to count the numbers
- Done with a iteration

- Make jagged array withappropriate sizes
- Each number is addedinto its jagged array

- The
*System.Array*class- Parent of all arrays
- All arrays inherit from it
- All arrays have the same:
- Basic functionality
- Basic properties
- E.g.
*Length*property

- Important methods and properties of
*System.Array**Rank*– number of dimensions*Length*– number of all elements through all dimensions*GetLength(index)*– returns the number of elements in the specified dimension- Dimensions are numbered from 0

*GetEnumerator()*– returns*IEnumerator*for the array elements*BinarySearch**(…)*– searches for a given element into a sorted array (uses binary search)*IndexOf**(…)*– searches for a given element and returns the index of the first occurrence (if any)*LastIndexOf(…)*– searches for a given element and returns the last occurrence index*Copy(src,**dest,**len)*– copies array elements; has many overloads*Reverse**(…)*– inverts the arrays elements upside down*Clear**(…)*_ _ – assigns value 0 (null) for each elements*CreateInstance**(…)*_ _ – creates an array- Accepts as parameters the number of dimensions, start index and number of elements

- Implements
*ICloneable*,*IList*,*ICollection*and*IEnumerable*interfaces

- Sorting in .NET is usually done with
*System.Array.Sort()**Sort(Array)*– sorts array elements- Elements should implement
*IComparable*

- Elements should implement
*Sort(Array,*_ _*IComparer)*– sorts array elements by given external*IComparer**Sort(Array,*_ _*Comparison<T>)*– sorts array elements by given comparison operation- Can be used with lambda expression

- Binary search is a fast method for searching for an element in a sorted array
- Has guaranteed running time of
*O(log(n))*_ _ for searching among arrays of with*n*elements

- Has guaranteed running time of
- Implemented in the
*Array.BinarySearch( Array,**object)*method- Returns the index of the found object or a negative number when not found

- All requirements of the
*Sort()*method are applicable for*BinarySearch()*- Either all elements should implement
*IComparable<T>*or instance of*IComparer<T>*should be passed

- Either all elements should implement

- When given method returns an array and should return an empty array (array with 0 elements) instead of
*null* - Arrays are passed by reference
- To be sure that given method will not change the passed array, pass a copy of it

*Clone()*returns shallow copy of the array- You should implement your own deep clone when working with reference types

© 2011-2022 **Nikolay Kostov's Blog**