+ 2
explain two dimensional array in java
i am confused about this... some one help me in simple
6 Answers
+ 9
An array should be looked at like a kind of an spreadsheet. You have Coloms and Rows. in a multidimensional array, the first [ 4] stands for 4 Coloms or Horizontal cells. The second [6] stands for Rows so for rows down.
If you want to address the values in the cells, remember that you have to start count with 0 and not 1 because in our sample, 4 stands for
cell 0 cell 1 cell 2 cell 3 so if you count from 0 to 3 you have 4 cells.
The same is for the Rows, you start with row 0 that is the first row or top row.
Hope this clears it up a little bit. If notm let me know we will figure it out, but the best way is to look at it like a spreadsheet.
+ 8
for example [4][3] means that every element of the first array contains three elements of the second array. 12 elements total (4*3=12). that's my understanding of this staff and I might be wrong...
+ 7
what about 3+ dimensional arrays? I'm thinking about atomic structure of a crystal or something similar... is it possible?
+ 6
@Angelina: A 3 dimensional array could be easily refered to as the Rubins Cube. I don't know your age, but Rubins Cube was very popular in the 90 periode and in some places it is still popular. This is the closest you can get to visualize a 3 dim array.
You see Airplanes in the air they are flying also in an three dimensional space if not they would continuously colide with each other.
More then 3 dimensions are rarely used, but keep in mind the every cell of an arry can contain an array so your starting point to visualize a real multiple dimensional array is to imagine a Rubins Cube where one cell of the cube is connected to another cube so you have created a double three dimensional array.
I hope that this helps you in visualization of arrays and indeed, an atomic structure could be compared with an array structure with multiple dimensions. 😆😆😆🌷🌸🌹🌻🌼🌺🍀🍀
+ 5
Just to avoid any miss-understandings,
an array is basically an object that contains elements to represent a list of one data type.
A multidimentional array is a bit different.
Each element in the multi-dimensional array IS another array. So a multi-dimensional (2d) array is really an array of arrays. Cols and rows are just analogys to visually see what's happening.
+ 4
An array of Integer represents a series of numbers like A = {1,2, 7 ,9}
Each value in the array can obtained by an index. A[0] =1, A[1] =2, A[2] = 8, A[3] = 9. If you ever played Battleship (or chess), you would realize that a 2 dimensional array is used. One square can be represented by two coordinates. Thus you need the horizontal indexes and the vertical indexes.
So, a value in the multimendional array, A[2,3] is the value at index 2 vertically and index 3 horizontally.
1 2 6
3 7 5
6 8 7
We go through this mutlidimensional array, you start with the vertical index. At index 0, the first row, get the value at horizontal index
The code would be:
Go through each row
Go trough each value on that row
Having A[][] of 3 by 3
for (int i = 0, i < horizontalArray.length; i++){
for (int j = 0 < verticalArray.length; j++){
System.out.println( A[i][j])
// Or put value depending on a condition
if (i == j){
A[i][j] = 3;
}else {
A[i][j] = 1;
}
}
}