What is multidimensional array?
Simply a Multidimensional Array is an array of array.
You know that an array is a collection of variable, accessed by a one name and there are indexes for each variable.
And also array is an Object in java
So you can create Array of Arrays!
These are called multidimensional Arrays.
How to create an array in java
Syntax
<data_type>[] name_of_array=new <data_type>[element_count];
Example
int[] locations=new int[25]; // This will create int array with 25 element
Accessing arrays
locations[0]=5;//This will assigns 5 to index 0th element in the locations array.
How to create multidimensional array in java
<data_type>[][] name_of_array=new <data_type>[element_count1][element_count2];
Example
int[][] locations=new int[10][10];// This will create a two-dimensional array.
Accessing two dimensional array.
locations[2][5]=4;//In this code it assigns 4 into the array where indexed as 2,5;
System.out.println(locations[2][5]);//In this code, prints 4 to the console, Because of we have assign 4 to that 2,5 index.
Output:
4
And also you can create, initialize and assign values to arrays when creating it.
int[]locations={10,5,8};
You can test it by System.out.println(locations[0]);
You can see in the console, 10, because of 10 is the 0th indexed element in the locations array.
No comments:
Post a Comment