## Irregular arrays. Memory allocation. N-dimensional irregular arrays. Advantages of using an irregular arrays. Examples

### Contents

- 1. What are
**irregular arrays**? Features of irregular arrays.**Examples**of declaring irregular arrays - 2. What is the
**difference**between irregular arrays and regular arrays?**Example** - 3. What are the
**benefits**of using irregular arrays? - 4. Can irregular arrays be three-dimensional, four-dimensional and more?
- 5. How many dimensions can irregular arrays have?
- 6.
**Example**of declaring and using a two-dimensional irregular array of type**int** - 7. An
**example**of declaring and using a two-dimensional irregular array of objects (instances) of a class - 8.
**Example**of declaring and using a three-dimensional irregular array of**double**type - 9. An
**example**of declaring and using a three-dimensional array of integers in which two dimensions have a variable number of items - 10. An
**example**of declaring and using a three-dimensional irregular array of objects (instances) of a class **Related topics**

##### 1. What are irregular arrays? Features of irregular arrays. Examples of declaring irregular arrays

Irregular arrays are a kind of *n*-dimensional (*n*>1) arrays, which are represented as arrays of one-dimensional arrays with different numbers of elements (items).

In an irregular array, the size of one-dimensional arrays, which corresponds to the last dimension, is various. Memory for such one-dimensional arrays is allocated of different sizes.

When declaring an irregular array, at least one dimension does not contain a specific value (empty brackets **[ ]**).

**For example**, examples of different types of ad irregular arrays are shown below

// regular and irregular array declarationsint[][] A =newint[3][5]; // regular arrayint[][] B =newint[3][]; // irregular two-dimensional arrayint[][][] C =newint[3][4][]; // irregular three-dimensional arrayint[][][] D =newint[3][][]; // irregular three-dimensional array // int[][][] E = new int[][][]; // error

In the above example, one regular (A) and three irregular (B, C, D) arrays are declared.

##### 2. What is the difference between irregular arrays and regular arrays? Example

In a regular array, the number of elements in each dimension is known in advance. This number is set at the time of allocating memory for the array with the new operator.

In an irregular array, the number of elements in at least one dimension is unknown. This number may vary within this dimension.

**For example.** Let a regular array A be declared for which memory is allocated

int[][] A =newint[3][5]; // regular array, number of items = 3 * 5 = 15

In this array, the number of elements is known when allocating memory using the new operator.

If irregular array is declared

int[][] B =newint[3][]; // irregular array, the total number of elements is unknown

it is known that this array B contains 3 one-dimensional arrays. The number of elements in any of these arrays is unknown. This is a sign of the declaration of an irregular array. To form these 3 one-dimensional arrays, you can write, for example, the following code

// memory allocation for array Bfor(inti=0; i<3; i++) B[i] =newint[i*2+1]; // filling array B with valuesfor(inti=0; i<3; i++)for(intj=0; j<B[i].length; j++) B[i][j] = i*3+j;

In the above code, memory is first allocated for any of the three one-dimensional arrays: B[0], B[1], B[2]. The length of the array B[0] is 1. The length of the array B[1] is 3. The length of the array B[2] is 5. The length property is used to determine the length of the array.

intt; t = B[0].length; // t = 1 t = B[1].length; // t = 3 t = B[2].length; // t = 5

After such a code, the number of elements in an irregular array will be 1 + 3 + 5 = 9.

##### 3. What are the benefits of using irregular arrays?

The use of irregular arrays gives the following advantages:

- in some cases, memory is significantly saved when there are a large number of unused items in the array. This is especially important if the elements of an irregular array are objects (instances) of classes that take up a lot of memory;
- establishing a different controlled length of each one-dimensional array in some cases can better reflect the content of the solution of the problem.

##### 4. Can irregular arrays be three-dimensional, four-dimensional and more?

Yes, they can.

**For example**. A three-dimensional irregular array C is declared and used.

int[][][] C =newint[3][4][]; // irregular three-dimensional array // creating irregular array C // memory allocation for array Cfor(inti=0; i<3; i++)for(intj=0; j<4; j++) C[i][j] =newint[(i+1)*(j+1)]; // memory is irregularly allocated // filling in array C with valuesfor(inti=0; i<3; i++)for(intj=0; j<4; j++)for(intk=0; k<C[i][j].length; k++) C[i][j][k]=i+j+k; // checkingintt; t = C[0][1][1]; // t = 2

##### 5. How many dimensions can irregular arrays have?

Irregular arrays can have two or more dimensions. The most common are two-dimensional irregular arrays. However, it is allowed to declare irregular arrays with any number of dimensions. The main thing is that the first (upper) dimension should have some value. Otherwise, the compiler will generate an error.

##### 6. Example of declaring and using a two-dimensional irregular array of type int

The example declares and uses an irregular two-dimensional array named B. The array contains 3 one-dimensional arrays whose sizes do not match.

// the declaration of irregular array Bint[][] B =newint[3][]; // irregular two-dimensional array // memory allocation for array Bfor(inti=0; i<3; i++) B[i] =newint[i*2+1]; // filling array B with valuesfor(inti=0; i<3; i++)for(intj=0; j<B[i].length; j++) B[i][j] = i*3+j; // reading the values of the items of array Bintt; t = B[0].length; // t = 1 t = B[1].length; // t = 3 t = B[2].length; // t = 5 t = B[0][0]; // t = 0 t = B[1][0]; // t = 3 t = B[1][1]; // t = 4

##### 7. An example of declaring and using a two-dimensional irregular array of objects (instances) of a class

A two-dimensional irregular array of WorkDay objects is declared. The WorkDay class contains information on hours worked by some employee and the tariff.

Also is declared the WorkYear class, which contains an array of WD objects of type WokrDay, which is grouped by months of the year. The WD array is irregular and contains 12 one-dimensional arrays, each of which corresponds to a month of the year. Each one-dimensional array has as many days as there are days in the corresponding month of the year.

// class that describes one working dayclassWorkDay {inthours; // hours workeddoubletariff; // tariff // class constructor WorkDay(inthours,doubletariff) {this.hours = hours;this.tariff = tariff; } } // a class that describes a working year for some employeeclassWorkYear { // array of WorkDay type, separated by months of the year WorkDay[][] WD =newWorkDay[12][]; // irregular array of objects of WorkDay type // constructor WorkYear() { // in the constructor the memory is allocated for an irregular array of objects WD[0] =newWorkDay[31]; // January - 31 days WD[1] =newWorkDay[29]; // February - 29 (28) days WD[2] =newWorkDay[31]; // March - 31 days WD[3] =newWorkDay[30]; // April - 30 days WD[4] =newWorkDay[31]; // May WD[5] =newWorkDay[30]; // June WD[6] =newWorkDay[31]; // July WD[7] =newWorkDay[31]; // August WD[8] =newWorkDay[30]; // September WD[9] =newWorkDay[31]; // October WD[10] =newWorkDay[30]; // November WD[11] =newWorkDay[31]; // December // Be sure to allocate memory for any object through the constructorfor(inti=0; i<WD.length; i++)for(intj=0; j<WD[i].length; j++) WD[i][j] =newWorkDay(0, 0.0); // call the constructor } }

The use of the WorkYear class WD irregular array can be as follows.

// use of WD irregular array WorkYear class WorkYear WY =newWorkYear();intnDays; // number of days in a month // checking nDays = WY.WD[5].length; // nDays = 30 // filling in an arbitrary element of the WY array WY.WD[0][0].hours = 6; // January, 1 WY.WD[0][0].tariff = 200.00; // tariff = 200.00 monetary units WY.WD[3][0].hours = 7; // April, 1 WY.WD[3][0].tariff = 130.00;

##### 8. Example of declaring and using a three-dimensional irregular array of double type

The example declares a DoubleArray class containing a three-dimensional irregular array D of type double. Memory allocation and filling with the values of the elements of the array D is implemented in the class constructor.

// class that implements three-dimensional irregular array of double typeclassDoubleArray { // declaration of three-dimensional irregular array of double typedouble[][][] D =newdouble[2][3][]; // constructor DoubleArray() { // memory allocation for irregular array D[2][3][] D[0][0] =newdouble[5]; D[0][1] =newdouble[6]; D[0][2] =newdouble[3]; D[1][0] =newdouble[4]; D[1][1] =newdouble[5]; D[1][2] =newdouble[7]; // filling array items with arbitrary valuesfor(inti=0; i<D.length; i++)for(intj=0; j<D[i].length; j++)for(intk=0; k<D[i][j].length; k++) D[i][j][k] = i + j + k*0.25; } }

The following demonstrates how to use the DoubleArray class.

// the use of irregular array D of DoubleArray class DoubleArray da =newDoubleArray();doublex; x = da.D[0][1][2]; // x = 1.5 x = da.D[1][0][3]; // x = 1.75 x = da.D[1][2][5]; // x = 4.25

##### 9. An example of declaring and using a three-dimensional array of integers in which two dimensions have a variable number of items** **

A three-dimensional array D is declared and used, in which the number of elements is not specified in two dimensions.

// in array D in the 2nd and 3rd dimensions the number of elements is not specifiedint[][][] D =newint[3][][]; // irregular three-dimensional array // int[][][] E = new int[][][]; // error // formation of array D // allocation of arbitrary size of memory for the 2nd dimensionfor(inti=0; i<D.length; i++) // D.length = 3 D[i] =newint[i+2][]; // allocation of arbitrary size of memory for the 3rd dimensionfor(inti=0; i<D.length; i++)for(intj=0; j<D[i].length; j++) D[i][j] =newint[(i+2)*(j+1)]; // filling in array D with valuesfor(inti=0; i<D.length; i++)for(intj=0; j<D[i].length; j++)for(intk=0; k<D[i][j].length; k++) D[i][j][k] = i+j*(k+2); // could be so D[0][0][1] = 23; D[1][2][2] = 230; // using the array Dintt; t = D[0][1][2]; // t = 4 t = D.length; // t = 3 t = D[0].length; // t = 2 t = D[1].length; // t = 3 t = D[2].length; // t = 4 t = D[0][1].length; // t = 4 t = D[1][2].length; // t = 9

##### 10. An example of declaring and using a three-dimensional irregular array of objects (instances) of a class

Let some class named A be given. The class implements an internal variable a of an integer type and a class constructor. This example demonstrates memory allocation, filling with values and using a three-dimensional irregular array of objects of class A.

// some class AclassA {inta; // class constructor A(inta) {this.a = a; } }

The following code can serve as an example for working with a three-dimensional array of objects.

// use of a three-dimensional irregular array of class A objectsintn = 4; A[][][] obj =newA[n][][]; // declare obj as irregular array // allocate memory for an irregular array of items of the 2nd dimensionfor(inti=0; i<obj.length; i++) obj[i] =newA[i+2][]; // some formula // allocate memory for an irregular array of items of the 3rd dimensionfor(inti=0; i<objA.length; i++)for(intj=0; j<objA[i].length; j++) objA[i][j] =newA[i+j+2]; // allocate memory for class A objectsfor(inti=0; i<objA.length; i++)for(intj=0; j<objA[i].length; j++)for(intk=0; k<objA[i][j].length; k++) obj[i][j][k] =newA(i+2*j+3*k); // constructor call // checkingintt; t = objA[0][1][2].a; // t = 8 t = objA[0][1][1].a; // t = 5