How to create a 2d array
3,341 views 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
Download the sourcecode here: http://www.freefilehost.co.uk/191377
password: engineeringserver.com








(2 votes, average: 4.5 out of 5)
America’s Army
Audiosurf
Battlefield 2
Battlefield 2142
C&C 3
Call of Duty 2
Call of Duty 4
CoH Series
Company of Heroes
Counter-Strike 1.6
Counter-Strike: Source
Crysis
Dawn of War
DoD: Source
EVE Online
Everquest 2
FEAR
Garry’s Mod
GTA Series
Guild Wars
Half-Life
Half-Life 2
Half-Life 2 Mods
Half-Life Mods
Halo
Hellgate London
LotR Online
Misc Gaming
Oblivion
Portal
Quake Series
Second Life
Silkroad Online
STALKER
Supreme Commander
SW Galaxies
Team Fortress 2
Trackmania Nations
War Rock
Warcraft 3
Wolfenstein: ET
World of Warcraft





Adobe Flex Builder 3 Pro is available for free to all education customers. This includes all students, faculty and staff of eligible education institutions.