What Is An Array

What Is An Array
What Is An Array

Video: What Is An Array

Video: What Is An Array
Video: 9.1: What is an Array? - Processing Tutorial 2024, April
Anonim

We can say with one hundred percent certainty that there is no programmer who does not use an array in his programs. They not only simplify the developer's life, but also make it possible to perform tasks that simply cannot be completed without him.

What is an Array
What is an Array

An array is an ordered collection of data, optionally of the same type, that is identified by one or more indexes. The first type of array is static. It is present in all high-level languages. Such arrays can be one-dimensional and multidimensional (usually they have no more than 2 or 3 dimensions). Some languages do not have the latter, for example ActionScript. In it, they are organized by creating a so-called "array of arrays", i.e. placing in array cells not simple data (int, Boolean, byte, etc.), but other arrays. Examples of declaring a static array in different languages: In Pascal: x: array [1..15] of Integer; {One-dimensional array of 15 elements of Integer type} x1: array [1..5, 1..5] of Char; {Two-dimensional array (table) with 5 rows and 5 columns} In C / C ++: int a [10]; // One-dimensional array for 10 elements of type integer (int) double b [12] [15]; // Two-dimensional array with 12 rows and 15 columns of double type The second type of array is dynamic. This type can change its size during program execution. This feature can be quite useful. It is used when it is difficult to immediately decide what dimension to create an array. Examples: In Delphi: a1: Array of Byte; // One-dimensional array of type byte a2: Array of Array of char; // Multidimensional array of char type In C ++: float * arr1; // One-dimensional array int ** arr2; // Multidimensional array arr1 = new float [70]; // allocation of 70 float blocks arr2 = new int * [99]; // allocation of 99 blocks with the size of a pointer to int for (int k = 0; k <99; k ++) arr2 [k] = new int [17]; Advantages of arrays - ease of determining the address of an element by index, the same access time to any element and small size of elements. However, there are some disadvantages inherent in their different types. For example, the disadvantage of a static array is the lack of dynamics, while a dynamic array is slower. Therefore, it is worth choosing exactly the type that is optimal for solving the task at hand.

Recommended: