+ 1
Plz expailn it i cant understand what are multidimensional arrays plz someone make me understand this fully....
public class Program { public static void main(String[] args) { int[ ][ ] sample = { {1, 2, 3}, {4, 5, 6} }; int x = sample[1][0]; System.out.println(x); } }
6 Answers
+ 13
good
+ 6
@ChaoticDawg was right, you should not post any private information online, that includes your phone number, it's against privacy policy, please remove your number, a decent answer had been given, and the best way to understand it, is indeed by practicing what had been explained.
If you find difficulties after trying to practice the sample you are free to consult again.
I would additionally suggest you to use a spreadsheet application to help yourself understanding the multidimensional array, though, it is limited to illustrate a maximum two dimensions array. Fill up the first column down from 1A until 10A with number 1 - 10, now, you have a one dimension (column) array.
For two dimensions array, fill down from 1A to C10 with any number you want, now, having understood that array index starts from zero:
Cell 1A is [row 0] [col 0] = array[0][0]
Cell 1B is [row 0] [col 1] = array[0][1]
Cell 1C is [row 0] [col 2] = array[0][2]
Cell 2A is [row 1] [col 0] = array[1][0]
Cell 2B is [row 1] [col 1] = array[1][1]
Cell 2C is [row 1] [col 2] = array[1][2]
Give it a try!
Hth, cmiiw
+ 4
You can think of a multidimensional array as arrays within an array. In the example you have 2 integer arrays that each have 3 elements within an array. This is a 2 dimensional array. You can think of it as rows and columns. It is often easier to visualize if you organize the array in a more readable fashion:
int[ ][ ] sample = {
{1, 2, 3},
{4, 5, 6}
};
Here you can clearly see the 2 inner arrays. The top "row" is row 0 and the next "row" will be row 1. In order to access the first row you would use 0 for its index.
int[] firstRow = sample[0]; // {1, 2, 3}
int[] secondRow = sample[1]; // {4, 5, 6}
sample[index] will return the inner array of the given index in the code above.
Then you could access the individual elements by their given index like a normal array.
int i = firstRow[1]; // 2
The code below is like a single step shortcut for the code above.
int x = sample[1][0];
// Gets the second inner array (row) [1] then the first element [0] of that inner array.
System.out.println(x); // outputs 4
You can have as many dimensions as you need, but it is usually recommended that you try to limit it to 3 or 4 as it can be quite difficult to comprehend the depth of the array after that.
For a 3 dimension array you would use 3 sets of brackets to access the element in the inner most array.
int[][][] cube = {
{
{1,2,3},
{4,5,6}
},
{
{7,8,9},
{10,11,12}
}
};
int y = cube[1][0][2]; // y = 9
+ 4
Sorry I'm not going to call you, and you really shouldn't post your phone number here at all.
If you are still having issues understanding I suggest you experiment with some code to help your understanding. Feel free to ask any other questions that are specific to what it is exactly that you feel you're not fully grasping.
Also, there is a good free video course on Java at caveofprogramming.com that may assist you further. Good luck.
+ 1
still i undrrstood a little bit
+ 1
If you are good with math then consider a multi dimension arreay(2-d) array as a matrix now moving furthur in terms of computer science a multi dimension array is basically a nested array means array or set of arrays in one arrays the dimensions of the multi dimension array depends on actually how many arrays are there don't forget to count the parental array(an array consisting all other nested arrays) together with the nested arrays a multi dimension array can also be treated like a tensor(another topic) of mathematics that are currently use for increasing the effectiveness of code and decrease the time lag also for deep learning so you must have a good knowledge of mathematics to be a successful coder.
Thanks
Prince amit