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
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[3][5]; // regular array int[][] B = new int[3][]; // irregular two-dimensional array int[][][] C = new int[3][4][]; // irregular three-dimensional array int[][][] D = new int[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 = new int[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 = new int[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 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[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.
int t; 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 = new int[3][4][]; // 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[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 B int[][] B = new int[3][]; // 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[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 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[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] = new WorkDay[31]; // January - 31 days WD[1] = new WorkDay[29]; // February - 29 (28) days WD[2] = new WorkDay[31]; // March - 31 days WD[3] = new WorkDay[30]; // April - 30 days WD[4] = new WorkDay[31]; // May WD[5] = new WorkDay[30]; // June WD[6] = new WorkDay[31]; // July WD[7] = new WorkDay[31]; // August WD[8] = new WorkDay[30]; // September WD[9] = new WorkDay[31]; // October WD[10] = new WorkDay[30]; // November WD[11] = new WorkDay[31]; // 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[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 type class DoubleArray { // declaration of three-dimensional irregular array of double type double[][][] D = new double[2][3][]; // constructor DoubleArray() { // memory allocation for irregular array D[2][3][] D[0][0] = new double[5]; D[0][1] = new double[6]; D[0][2] = new double[3]; D[1][0] = new double[4]; D[1][1] = new double[5]; D[1][2] = new double[7]; // 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[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 specified int[][][] D = new int[3][][]; // 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[0][0][1] = 23; D[1][2][2] = 230; // using the array D int t; 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 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[0][1][2].a; // t = 8 t = objA[0][1][1].a; // t = 5