30 Apr 2008 @ 3:04 PM 
 

How to create a 2d array

 


1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...
3,705 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

Related posts

Your Ad Here
Tags Tags: , ,
Categories: Java, uncategorized
Posted By: HappyFace
Last Edit: 06 Jun 2009 @ 09 06 PM

EmailPermalink
 

Responses to this post » (None)

 

Post a Comment

You must be logged in to post a comment.

\/ More Options ...
Change Theme...
  • Users » 888
  • Posts/Pages » 1,444
  • Comments » 506
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Online Users



    No Child Pages.

Forum



    No Child Pages.