what do you really want to know about java array? concept of array? or just java syntax?
what do you really want to know about java array? concept of array? or just java syntax?
something related to datastructure in java...more on array man gud cya and methods
Hope this will help....you can try it as you please...
================================================== ============
//////////////////////////////////////////////////////////////////////////////////
// This code snippet illustrates a 5x5 two dimensional array. The values//
// of the array are presented in a table. //
/////////////////////////////////////////////////////////////////////////////////
//defines the number of rows and columns
int rows = 5;
int columns = 5;
// initialize the array
String [][] array = new String[rows][columns];
/**
* Let's stores values into the array(in this case the value is a string).
* The stored values represents the indexes of the array where
* the value is stored.
*
* e.g. array[0][1] has a stored string "[0][1]" this means that
* at row 0 column 1 the value is "[0][1]".
*
* furthermore,
* array[0][1] == "[0][1]"
*/
//loops the rows
for(int x=0; x<array.length; x++)
{
//loops the columns
for(int y=0; y<array.length; y++ )
{
//store the string value to the array
array[x][y] = " ["+x+"]["+y+"]";
}
}
/**
* Let's display the string we stored in the array. The loops below
* traverse the array row by row.
*/
//prints the column heading
System.out.println(" 0 1 2 3 4");
//traverse the rows
for(int x=0; x<array.length; x++)
{
//prints the row heading
System.out.print(""+ x + " ");
//traverse the columns
for(int y=0; y<array.length; y++ )
{
//prints the value in columns
System.out.print(""+array[x][y]);
}
//prints a next line for the next row
System.out.print("\n");
}
========================================
-----0-----1-----2-----3-----4----
0 [0][0] [0][1] [0][2] [0][3] [0][4]
1 [1][0] [1][1] [1][2] [1][3] [1][4]
2 [2][0] [2][1] [2][2] [2][3] [2][4]
3 [3][0] [3][1] [3][2] [3][3] [3][4]
4 [4][0] [4][1] [4][2] [4][3] [4][4]
You can put arrays this way...imagine a locker with numbers. like this
[0][0] [0][1] [0][2] [0][3] [0][4]
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
[3][0] [3][1] [3][2] [3][3] [3][4]
[4][0] [4][1] [4][2] [4][3] [4][4]
each pair of numbers represents one locker. if you store a thing, example a "ball", into locker number [1][1]...in java it would be like...
array[1][1] = "ball" ;
when you want to get the "ball", you get it from locker [1][1] because it is where you stored it. In java, this would be...
String str = array[1][1];
the value of str is "ball".
so if you will have multi dimensional arrays...the same concept follows. you can treat the indexes as address of your value.
Similar Threads |
|