Lecture
n computer science, a ragged array, also known as a jagged array, is an array of arrays of which the member arrays can be of different sizes and [1] producing rows of jagged edges when visualized as output. In contrast, two-dimensional arrays are always rectangular.[2]
Arrays of arrays in languages such as Java, PHP, Python (multidimensional lists), Ruby, C#.Net, Visual Basic.NET, Perl, JavaScript, Objective-C, Swift, and Atlas Autocode are implemented as Iliffe vectors.
Array of arrays
It is necessary to distinguish from multidimensional arrays an array of arrays or the so-called "jagged array":
int [] [] nums = new int [3] [];
nums [0] = new int [2] {1, 2}; // allocate memory for the first subarray
nums [1] = new int [3] {1, 2, 3}; // allocate memory for the second subarray
nums [2] = new int [5] {1, 2, 3, 4, 5}; // allocate memory for the third subarray
Here, two groups of square brackets indicate that it is an array of arrays, that is, an array that in turn contains other arrays. Moreover, the length of the array is indicated only in the first square brackets, all subsequent square brackets should be empty: new int [3] []. In this case, our nums array contains three arrays. Moreover, the dimension of each of these arrays may not coincide.
A cogged array is one whose length of the second and subsequent measurements can be different.
Simply put, this is an array consisting of arrays of the same type, but of different lengths. It is also called stepped. Visually, a two-dimensional step array is depicted as a flat table with rows that contain a different number of cells.
Зубчатый массив nums
1 |
2 |
|||
1 |
2 |
3 |
||
1 |
2 |
3 |
4 |
5
|
A cogged array is one whose length of the second and subsequent measurements can be different.
Simply put, this is an array consisting of arrays of the same type, but of different lengths. It is also called stepped. Visually, a two-dimensional step array is depicted as a flat table with rows that contain a different number of cells.
A jagged array is declared using successive square brackets. Dimension should be determined only for the first measurement.
Example:
int[][] jaggArr = new int[3][]; // Объявление трехстрочного зубчатого массива.
jaggArr[0] = new int[2]; // Первая строка на 2 ячейки.
jaggArr[1] = new int[5]; // Вторая строка на 5 ячеек.
jaggArr[2] = new int[3]; // Третья строка на 3 ячейки.
int count = 0;
for (int i = 0; i < 3; i++)
{
len = jaggArr[i].Length;
for (int j = 0; j < len; j++)
{
jaggArr[i][j] = j + 1 + count;
}
count += len;
}
int t13 = jaggArr[1][3]; // 6.
As a result, a jaggArr jagged array will be created, which can be represented as follows:
one |
2 |
|||
3 |
4 |
5 |
6 |
7 |
8 |
nine |
10 |
The variable t13 was assigned the value of the underlined cell [1] [3].
A jagged array can be initialized in two ways: fully and line by line. For line-by-line initialization, you first need to declare a jagged array, and then initiate each row separately. Also, an array can be completely declared and initialized at a time, however it looks cumbersome and can be confusing to the code.
We give an example of full and line-by-line initialization. For clarity, we will create jagged arrays of the same structure as in the previous example:
int[][] jaggedArr1 = new int[3][]; // Объявляем зубчатый массив.jaggedArr1[0] = new int[2] { 1, 2 }; // Далее инициализируем каждую строку отдельно. jaggedArr1[1] = new int[5] { 3, 4, 5, 6, 7 }; jaggedArr1[2] = new int[] { 8, 9, 10 }; // Допустимо определение длины по количеству значений.int[][] jaggedArr2 = { // Полная инициализация с опущенным словом new. new int[2] { 1, 2 }; new int[] { 3, 4, 5, 6, 7 }; new int[3] { 8, 9, 10 }; };
By initializing internal arrays, their sizes can be omitted. With full initialization, using the new operator in the declaration is not necessary, but in both cases it must be used for internal arrays.
Unlike multidimensional, in a jagged array, the Length property does not count all internal cells, but the number of array elements in it. But applying the Length property to a cell returns its length.
Example:
int[][] jagArr257 = new int[3][]; jagArr257[0] = new int[2]; jagArr257[1] = new int[5]; jagArr257[2] = new int[7]; int len = jagArr257.Length; Console.WriteLine(“Количество внутренних массивов: {0}”, len); for (int i = 0; i < len; i++) { Console.WriteLine(“Длина {0}-го элемента: {1}", i+1, jagArr257[i].Length); }/* Вывод будет следующим: Количество внутренних массивов: 3 Длина 1-го элемента: 2 Длина 2-го элемента: 5 Длина 3-го элемента: 7 */
It is permissible to insert multidimensional into gear arrays. It is important not to forget the differences in working with them, including the use of the Length property.
Example:
char[][ , ] jagArr2D = new char[3][ , ]; // Объявление двумерного зубчатого массива jagArr2D[0] = new char[2,5]; jagArr2D[1] = new char[4,5]; jagArr2D[2] = new char[3,7]; int len = jagArr2D.Length; Console.WriteLine(“Количество внутренних массивов: {0}”, len); for (int i = 0; i < len; i++) { Console.WriteLine(“Длина {0}-го элемента: {1}", i+1, jagArr2D[i].Length); }/* Вывод будет следующим: Количество внутренних массивов: 3 Длина 1-го элемента: 10 Длина 2-го элемента: 20 Длина 3-го элемента: 21 */
This conclusion is due to the fact that Length for a gear array gives out the number of its array cells, and for a multidimensional one it considers the total number of elements.
Comments
To leave a comment
Structures and data processing algorithms.
Terms: Structures and data processing algorithms.