java declare multilayred array values

www.lautt‮.iru‬com
java declare multilayred array values

In Java, you can declare a multidimensional array by using multiple sets of square brackets ([]) after the array type. The number of sets of brackets indicates the number of dimensions in the array.

Here is an example of how to declare a two-dimensional array of integers in Java:

int[][] myArray;

This declares a new variable myArray that refers to a two-dimensional array of integers. The first set of brackets indicates that the array has one dimension, and the second set of brackets indicates that the array has a second dimension.

To create a new array object and assign it to the myArray variable, you can use the following syntax:

myArray = new int[3][4];

This creates a new two-dimensional array with 3 rows and 4 columns, and assigns it to the myArray variable. The array can hold a total of 12 integer elements, which are organized in a grid of 3 rows and 4 columns.

You can access the elements of the array using the array indexing syntax, with the indices separated by commas. For example:

myArray[0][0] = 1;  // Assign the value 1 to the element at row 0, column 0
myArray[0][1] = 2;  // Assign the value 2 to the element at row 0, column 1
myArray[1][0] = 3;  // Assign the value 3 to the element at row 1, column 0
myArray[1][1] = 4;  // Assign the value 4 to the element at row 1, column 1

This code assigns values to the elements at the specified indices in the array.

You can declare multidimensional arrays with more than two dimensions by adding additional sets of brackets. For example, to declare a three-dimensional array of integers, you can use the following syntax:

int[][][] myArray;

This declares a new variable myArray that refers to a three-dimensional array of integers. You can create a new array object and assign it to the myArray variable using the following syntax:

myArray = new int[3][4][5];

This creates a new three-dimensional array with 3 rows, 4 columns, and 5 layers, and assigns it to the myArray variable. The array can hold a total of 60 integer elements, which are organized in a grid of 3 rows, 4 columns, and 5 layers.

Created Time:2017-11-03 00:14:53  Author:lautturi