Home
About me (CV)
My Videos
Video: Multidimensional Arrays in C# (Bulgarian)
March 11, 2022
Nikolay Kostov (Nikolay.IT)
Topics covered:
Matrices and Multidimensional Arrays
Declaring
Usage
Jagged Arrays
Declaring
Usage
The
Array
Class
Sorting
Binary Search
Video (in Bulgarian)
Presentation Content
What is Multidimensional Array?
Multidimensional arrays
have more than one dimension (2, 3, …)
The most important multidimensional arrays are the 2-dimensional
Known as
matrices
or
tables
Example of matrix of integers with 2 rows and 4 columns:
5
0
-2
4
5
6
7
8
Declaring and Creating Multidimensional Arrays
Declaring multidimensional arrays:
Creating a multidimensional array
Use
new
keyword
Must specify the size of each dimension
Initializing Multidimensional Arrays with Values
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)
Jagged Arrays
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?
Initialization of Jagged Arrays
When creating jagged arrays
Initially the array is created of
null
arrays
Need to initialize each of them
Example of Jagged Arrays
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 Array Class
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
Methods of Array
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
Methods of Array (2)
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 Arrays
Sorting in .NET is usually done with
System.Array.Sort()
Sort(Array)
– sorts array elements
Elements should implement
IComparable
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
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
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
Advices for Working with Arrays
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
Tweet
Share