



In this example i’m showing you how to create a 2d array ( Two Dimensional Array )
First you must declare a 2d String array like this:
String[][] personHolder = new String[3][4]This means that the string can hold 3 items and in each of the item it can hold 4 other items.
After that add data to the string array like this:
personHolder[1][0] = “Amber”;
personHolder[1][1] = “F”;
personHolder[1][2] = “12 April 1992″;
personHolder[1][3] = “Single”;
To show the 2d array you can create a for loop to display the contents of the array like this:
for(int i = 1; i < 3; i++){
for(int j = 0; j <= personHolder.length; j++){
System.out.println(personHolder[0][j] + ” ” + personHolder[i][j]);
}
System.out.println();
}
Sourcecode:
public class TwoDimensionalArray {
public static void main(String[] args){
String[][] personHolder = new String[3][4];
personHolder[0][0] = “Name: \t”;
personHolder[0][1] = “Gender: \t”;
personHolder[0][2] = “Day Of Birth:\t”;
personHolder[0][3] = “Status: \t”;
personHolder[1][0] = “Amber”;
personHolder[1][1] = “F”;
personHolder[1][2] = “12 April 1992″;
personHolder[1][3] = “Single”;
personHolder[2][0] = “Peter”;
personHolder[2][1] = “M”;
personHolder[2][2] = “12 April 1980″;
personHolder[2][3] = “Married”;
//loop starts at one because you only want to show the persons
for(int i = 1; i < 3; i++){
for(int j = 0; j <= personHolder.length; j++){
System.out.println(personHolder[0][j] + ” ” + personHolder[i][j]);
}
System.out.println();
}
}
}
Output:
Name: Amber
Gender: F
Day Of Birth: 12 April 1992
Status: Single
Name: Peter
Gender: M
Day Of Birth: 12 April 1980
Status: Married


More Options ...
Categories
Tag Cloud
Blog RSS
Comments RSS

Void « Default
Life
Earth
Wind
Water
Fire
Light 