Java. Arrays. Part 2. Two-dimensional and multi-dimensional arrays




Two-dimensional and multi-dimensional arrays



Search other websites:

1. What arrays are called two-dimensional and multi-dimensional?

General concepts about arrays and the use of one-dimensional arrays are described in detail here.

The array is considered two-dimensional, if to determine the item location in the array, you need to specify the values of the two indexes. The two-dimensional array is associated with the table. If the number of rows and columns of the array (table) is equally, then the array is called the matrix.

The array is considered multi-dimensional, if to determine the item location in the array, you need to specify the values of the three and more indexes.

 

2. Example of defining and using of two-dimensional arrays in the program
// integer matrix of size 3 * 4
int Matr[][];
Matr = new int[3][4];

// filling of matrix cells
Matr[0][2] = 23;
Matr[0][0] = -220;
Matr[2][3] = 380;

// matrix of real numbers, size 2 * 3
float[][] MatrF = new float[2][3];
MatrF[0][2] = 9.88f;
MatrF[0][0] = -33.8329f;

 

3. Example of declaring a two-dimensional array by using the alternative variants

In Java, a two-dimensional array can be described in different ways.

This example shows different variants of describing the two-dimensional array of integers of size 2×3.

Variant 1.

// declaring the matrix of size 2 * 3
int[] Matr[] = new int[2][3];

Variant 2.

int[][] Matr = new int[2][3];

Variant 3.

int[][] Matr = new int[2][3];

Variant 4.

int[][] Matr;
Matr = new int[2][3];

Variant 5.

int[] Matr[];
Matr = new int[2][3];

Variant 6.

int Matr[][];
Matr = new int[2][3];

Variant 7.

int Matr[][] = new int[2][];
Matr[0] = new int[3];
Matr[1] = new int[3];

Variant 8.

int[][] Matr = new int[2][];
Matr[0] = new int[3];
Matr[1] = new int[3];

Variant 9.

int[] Matr[] = new int[2][];
Matr[0] = new int[3];
Matr[1] = new int[3];

 

4. How is implemented the initialization of the two-dimensional array? Example

To initialize the two-dimensional array, you need the initializer of each measurement take between the braces.

The following example declares a two-dimensional array of real numbers with the name X. The array size 2×3.

// initialization the two-dimensional array of size 2 * 3
float X[][] = {
                  { 0.1f, 0.3f, 1.2f },
                  { 1.3f, 2.8f, 3.2f }
              };


 

5. Example of declaration a two-dimensional array, with a different number of elements in a single row (a different dimensionality of the second dimension)

In this example, a two-dimensional array is declared in which the number of elements in each row is various. Then, the array filled with the values as shown in the figure.

Figure. The array, according to the condition of the problem

// declaration of the array in which 3 rows have different dimensions
long M[][] = new long[3][];

M[0] = new long[2]; // 2 elements in the zero row
M[1] = new long[4]; // 4 elements in the first row
M[2] = new long[3]; // 3 elements in the second row

M[0][0] = 10l;
M[0][1] = 350l;

M[1][0] = 25l;
M[1][1] = 45l;
M[1][2] = 85l;
M[1][3] = 1200l;

M[2][0] = 55l;
M[2][1] = 450l;
M[2][2] = 2500l;

 

6. Example of declaring multidimensional arrays

Multidimensional arrays use 3 or more indexes to access the array elements.

// declare the array of size 3 * 2 * 4
int M[][][] = new int[3][2][4];

// use the array
M[1][1][2] = 23;
M[0][0][1] = -12;

// declare the array of size 5*10*15
double[][][] D;
D = new double[5][10][15];
D[3][7][11] = -29.33;

 

7. Example of initialization of a multidimensional array

In this example, the size of the array is initialized of size 2×3×3.

// initialization of array of size 2 * 3 * 3
byte B[][][] = {
                   {
                       { 0, 1, 2 },
                       { 1, 2, 3 },
                       { 3, 4, 5 }
                   },
                   {
                       { 1, 2, 3 },
                       { 2, 3, 4 },
                       { 5, 6, 7 }
                   }
               };

 

8. An example of zeroing of a two-dimensional matrix of real numbers, size 5*5
// zeroing of matrix of size of 5 * 5
double Matrix[][] = new double[5][5];

for (int i=0; i<5; i++)
    for (int j=0; j<Matrix[i].length; j++)
        Matrix[i][j]=0.0;

 

9. Example of searching a given element in the array of integers of size 10×10
// searching for the item in the matrix
short Matrix[][] = new short[10][10];
boolean f_is;
short num; // the required element

// enter the value of num
// ...

f_is = false; // first we assume that there is no element in the matrix

for (int i=0; i<10; i++)
    for (int j=0; j<Matrix[i].length; j++)
        if (Matrix[i][j]==num)
            f_is = true; // item is found

 

10. Example of searching the maximum value in a matrix of real numbers
// search for the maximum in matrix
float Matrix[][] = new float[10][10];
float max; // the required maximum value

// ...

max = Matrix[0][0]; // assume that max - one of the elements of the matrix

for (int i=0; i<10; i++)
    for (int j=0; j<Matrix[i].length; j++)
        if (Matrix[i][j]>max)
            max = Matrix[i][j];

 

11. Example of calculating the sum of the items of two-dimensional matrix of real numbers
// the sum of the elements of the matrix
float Matrix[][] = new float[10][10];
float sum; // the required sum

// ...

sum = 0;

for (int i=0; i<10; i++)
    for (int j=0; j<Matrix[i].length; j++)
        sum = sum + Matrix[i][j];





 

12.  An example of declaring and using of two-dimensional array of strings.

In the Java language to work with the strings implemented a special class String.

This example shows the different variants of the declaration, initialization and using of a two-dimensional array of strings.

// declare the two-dimensional array of strings
String M1[][] = new String[5][5]; // creating an variable of array type

// put the information in an array of rows
for (int i=0; i<5; i++)
    for (int j=0; j<M1[i].length; j++)
        M1[i][j] = "Item #" + i + j;

// deep copy of arrays using the clone() method
String M2[][] = M1.clone(); // arrays M1 and M2 point to different memory areas

// declare and initialize an array of strings of size 3 * 3
String M3[][] = {
                    { "Item-00", "Item-01", "Item-02" },
                    { "Item-10", "Item-11", "Item-12" },
                    { "Item-20", "Item-21", "Item-22" }
                };

// shallow copy
String M4[][]; // the description of a variable of type two-dimensional array of strings

M4 = M1; // arrays M1 and M4 point to the same memory location

// change in array M1 leads to a change in the array M4 and vice versa
M1[1][4] = "1111"; // M4[1][4] = "1111"
M4[1][4] = "2222"; // M1[1][4] = "2222"

 

13. An example of declaring and using of two-dimensional array of classes

In this example, declared a two-dimensional array of class of size 3×4.

Process of variable declaration takes place in several stages:

  • declare a variable of the type of two-dimensional array of MyPoint class;
  • allocate memory for a general reference to the classes of size 3×4;
  • allocate memory for each element of the array, which is the class.
// declare the variable mp of two-dimensional array type
MyPoint[][] mp;

// memory allocation for the array of classes of size 3 * 4
mp = new MyPoint[3][4];

// memory allocation for each item of array (class)
for (int i=0; i<3; i++)
    for (int j=0; j<mp[i].length; j++)
        mp[i][j] = new MyPoint();

// array use in the program
for (int i=0; i<3; i++)
    for (int j=0; j<mp[i].length; j++)
        mp[i][j].SetXY(i, j);

int d;
d = mp[2][2].GetY(); // d = 2
d = mp[1][2].GetX(); // d = 1

 


Related topics