Monday, July 22, 2024 14:52

Table of contents >> Data Structures > Arrays


We talked about arrays before, in an entire chapter. The arrays are collections of fixed number of elements of a given type (strings, integers, etc) where the elements preserve their initial order. Each element can be accessed through its numerical index, which starts at 0. The arrays are memory areas which have a predefined size. That’s why, adding a new element in an array is a slow operation. To do this, we first have to allocate a memory of the same size plus one, and copy all the data from the original array to the new one. Searching in an array takes time because we have to compare every element to the searched value. It takes N/2 comparisons in the average case. Removing an element from an array is also a slow operation. As before, we have to allocate a memory of the same size minus one, and copy all the old
elements except the removed one. Accessing elements by index is direct, and thus, a fast operation. The arrays should be used only when we have to process a fixed number of elements to which we need a quick access by index. For example, if we have to sort some numbers, we can keep them in an array and then apply some of the sorting algorithms. If we have to change the elements count, the array is not the correct data structure we should use. Use arrays when you have to process a fixed number of elements to which you need an access through index. An array is defined like this:


Arrays have a lot of methods, of which the following are the most useful:

Array.AsReadOnly() – makes it possible to restrict access to arrays by client code. Example:

Array.BinarySearch() – This method quickly and accurately pinpoints the location of an element in the array. It can be told how to compare elements. It works correctly only on a presorted array. Example:

Array.Clear() – zeros out all elements. It provides a one-line, reliable and understandable way to empty or clear your array. It works on arrays of any type – including numbers, booleans, structs and class instances. Example:

Array.ConvertAll() – allows you to declaratively convert an entire array with a single statement. It converts all elements in one array to another type. It incurs some performance overhead. But it can simplify code, particularly in programs where many different conversions take place. Example:

Array.Copy() – copies elements from one array to another. It has some complexities. This operation can result in certain exceptions. The type of elements – in both the target and source arrays – is important. This is a static method. Example:

Array.Exists() – provides a handy way to test for an element that matches a certain predicate condition. It involves some overhead and performance drawbacks. But it can be useful for certain programs. Example:

Array.Find() – This method searches an array (with declarative syntax). We specify a Predicate type instance to determine what logic the search uses. Example:

Array.ForEach() – loops over every element. It calls a method on each element in an array. It is a declarative syntax form. It simplifies certain code patterns. No loop construct is needed. Examples:

Array.IndexOf() – searches an array. It acts upon an array of any type. It locates the offset of the value specified. The IndexOf method on Array, in both its generic form and its LastIndexOf form, is useful in many program contexts. Example:

Array.LastIndexOf() – finds the last matching element. It searches from the end of an array. It returns the index of the element that contains the specified value. It has limitations – it may be of limited value. Examples:

Array.Resize() allocates a new array. It then copies existing element values to the new array. This logic is needed when an array’s size is inadequate. I show how Resize reallocates and copies elements:

Array.Reverse() – inverts the ordering of an array’s elements. This task could be accomplished with a for-loop. But the Array.Reverse() method is more convenient – and also easier to read. Example:

Array.Sort() – This method orders elements in an array. It modifies the array in-place. It handles different types of elements, including strings and ints. Example:

Array.TrueForAll() – a static Array method. It gives you a declarative way to test every element in your array for some condition using a Predicate. It scans the array and returns true or false. Example:

The most important property of arrays is:

Length – an array has a length – this is its size (its element count). An int of 0 or greater is returned – no iteration is done (a cache is used). Example:

Tags: , , , ,

Leave a Reply

Follow the white rabbit