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:GeSHi (java):
Created by GeSHI 1.0.7.20
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:GeSHi (java):
personHolder[1][0] = "Amber";
personHolder[1][1] = "F";
personHolder[1][2] = "12 April 1992";
personHolder[1][3] = "Single";
Created by GeSHI 1.0.7.20
To show the 2d array you can create a for loop to display the contents of the array like this:
GeSHi (java):
for(int i = 1; i < 3; i++){
for(int j = 0; j <= personHolder.length; j++){
System.
out.
println(personHolder
[0][j
] +
" " + personHolder
[i
][j
]);
}
}
Created by GeSHI 1.0.7.20
Sourcecode:GeSHi (java):
public class TwoDimensionalArray {
public static void main
(String[] args
){ 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
]);
}
}
}
}
Created by GeSHI 1.0.7.20
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
I posted this a few months ago on my blog here:
http://www.engineeringser...how-to-create-a-2d-array/ but i wanted to post it here also for those that want to know how to create 2d string arrays.