Multidimensional arrays If you are a java developer so it is for sure you are dealing with arrays, in java you can create 1, 2 or 3 dimensional arrays, in this article we go throw how to use multidimensional arrays in java. For memory allocations, java uses an approach named “Arrays of Arrays” where java establishes a multidimensional array from a group of one dimensional arrays and not like other approaches that reserve a blocks in memory.
1-dimensional arrays: For one 1dimensional array you will have only one row and you will have to specify its length in allocation step. You first will declare a 1D array as follows:
int array1D;// Declares 1D array.
And you do memory allocation as follows:
array1D = new int;// memory allocation
You do these two steps in only one:
int array1D = new int;
This creates a one array of 5 elements. Note that the index start from zero, following snippet fills a 1D array and prints its content:
2-dimensional arrays For creating 2D dimensional arrays you will need to define the number of rows and columns in your array, the declaration and allocation is as follows:
int array2D = new int;
The previous line creates a 2d dimensional array; this actually creates 7 objects, one dimensional array of 6 elements for each for the rows, and a one dimensional array of 3 elements. Following snippet adds data and prints it to a 2D array:
int array2D= new int; array2D=54; array2D=11; array2D=43;