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

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

### Contents

Search other websites:

##### 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 declarations
int[][] A = new int; // regular array
int[][] B = new int[]; // irregular two-dimensional array
int[][][] C = new int[]; // irregular three-dimensional array
int[][][] D = new int[][]; // 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 = new int; // 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 = new int[]; // 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 B
for (int i=0; i<3; i++)
B[i] = new int [i*2+1];

// filling array B with values
for (int i=0; i<3; i++)
for (int j=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, B, B. The length of the array B is 1. The length of the array B is 3. The length of the array B is 5. The length property is used to determine the length of the array.

```int t;
t = B.length; // t = 1
t = B.length; // t = 3
t = B.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 = new int[]; // irregular three-dimensional array

// creating irregular array C
// memory allocation for array C
for (int i=0; i<3; i++)
for (int j=0; j<4; j++)
C[i][j] = new int[(i+1)*(j+1)]; // memory is irregularly allocated

// filling in array C with values
for (int i=0; i<3; i++)
for (int j=0; j<4; j++)
for (int k=0; k<C[i][j].length; k++)
C[i][j][k]=i+j+k;

// checking
int t;
t = C; // 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 B
int[][] B = new int[]; // irregular two-dimensional array

// memory allocation for array B
for (int i=0; i<3; i++)
B[i] = new int [i*2+1];

// filling array B with values
for (int i=0; i<3; i++)
for (int j=0; j<B[i].length; j++)
B[i][j] = i*3+j;

// reading the values of the items of array B
int t;
t = B.length; // t = 1
t = B.length; // t = 3
t = B.length; // t = 5
t = B; // t = 0
t = B; // t = 3
t = B; // 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 day
class WorkDay {
int hours; // hours worked
double tariff; // tariff

// class constructor
WorkDay(int hours, double tariff) {
this.hours = hours;
this.tariff = tariff;
}
}

// a class that describes a working year for some employee
class WorkYear {
// array of WorkDay type, separated by months of the year
WorkDay[][] WD = new WorkDay[]; // irregular array of objects of WorkDay type

// constructor
WorkYear() {
// in the constructor the memory is allocated for an irregular array of objects
WD = new WorkDay; // January - 31 days
WD = new WorkDay; // February - 29 (28) days
WD = new WorkDay; // March - 31 days
WD = new WorkDay; // April - 30 days
WD = new WorkDay; // May
WD = new WorkDay; // June
WD = new WorkDay; // July
WD = new WorkDay; // August
WD = new WorkDay; // September
WD = new WorkDay; // October
WD = new WorkDay; // November
WD = new WorkDay; // December

// Be sure to allocate memory for any object through the constructor
for (int i=0; i<WD.length; i++)
for (int j=0; j<WD[i].length; j++)
WD[i][j] = new WorkDay(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 = new WorkYear();
int nDays; // number of days in a month

// checking
nDays = WY.WD.length; // nDays = 30

// filling in an arbitrary element of the WY array
WY.WD.hours = 6; // January, 1
WY.WD.tariff = 200.00; // tariff = 200.00 monetary units
WY.WD.hours = 7; // April, 1
WY.WD.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 type
class DoubleArray
{
// declaration of three-dimensional irregular array of double type
double[][][] D = new double[];

// constructor
DoubleArray() {
// memory allocation for irregular array D[]
D = new double;
D = new double;
D = new double;
D = new double;
D = new double;
D = new double;

// filling array items with arbitrary values
for (int i=0; i<D.length; i++)
for (int j=0; j<D[i].length; j++)
for (int k=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 = new DoubleArray();
double x;
x = da.D; // x = 1.5
x = da.D; // x = 1.75
x = da.D; // 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 specified
int[][][] D = new int[][]; // irregular three-dimensional array

// int[][][] E = new int[][][]; // error
// formation of array D
// allocation of arbitrary size of memory for the 2nd dimension
for (int i=0; i<D.length; i++) // D.length = 3
D[i] = new int[i+2][];

// allocation of arbitrary size of memory for the 3rd dimension
for (int i=0; i<D.length; i++)
for (int j=0; j<D[i].length; j++)
D[i][j] = new int [(i+2)*(j+1)];

// filling in array D with values
for (int i=0; i<D.length; i++)
for (int j=0; j<D[i].length; j++)
for (int k=0; k<D[i][j].length; k++)
D[i][j][k] = i+j*(k+2);

// could be so
D = 23;
D = 230;

// using the array D
int t;
t = D; // t = 4
t = D.length; // t = 3
t = D.length; // t = 2
t = D.length; // t = 3
t = D.length; // t = 4
t = D.length; // t = 4
t = D.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 A
class A {
int a;

// class constructor
A(int a) {
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 objects
int n = 4;
A[][][] obj = new A[n][][]; // declare obj as irregular array

// allocate memory for an irregular array of items of the 2nd dimension
for (int i=0; i<obj.length; i++)
obj[i] = new A[i+2][]; // some formula

// allocate memory for an irregular array of items of the 3rd dimension
for (int i=0; i<objA.length; i++)
for (int j=0; j<objA[i].length; j++)
objA[i][j] = new A[i+j+2];

// allocate memory for class A objects
for (int i=0; i<objA.length; i++)
for (int j=0; j<objA[i].length; j++)
for (int k=0; k<objA[i][j].length; k++)
obj[i][j][k] = new A(i+2*j+3*k); // constructor call

// checking
int t;
t = objA.a; // t = 8
t = objA.a; // t = 5```