Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Create a two dimensional string array with java  (Read 947 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: July 19, 2008, 05:53:40 PM »

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:
Code
GeSHi (java):
  1. String[][] personHolder = new String[3][4]
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:
Code
GeSHi (java):
  1. personHolder[1][0] = "Amber";
  2. personHolder[1][1] = "F";
  3. personHolder[1][2] = "12 April 1992";
  4. 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:

Code
GeSHi (java):
  1. for(int i = 1; i < 3; i++){
  2. for(int j = 0; j <= personHolder.length; j++){
  3. System.out.println(personHolder[0][j] + " " + personHolder[i][j]);
  4. }
  5. System.out.println();
  6. }
  7.  
Created by GeSHI 1.0.7.20

Sourcecode:
Code
GeSHi (java):
  1. public class TwoDimensionalArray {
  2. public static void main(String[] args){
  3. String[][] personHolder = new String[3][4];
  4. personHolder[0][0] = "Name:    \t";
  5. personHolder[0][1] = "Gender:    \t";
  6. personHolder[0][2] = "Day Of Birth:\t";
  7. personHolder[0][3] = "Status:    \t";
  8. personHolder[1][0] = "Amber";
  9. personHolder[1][1] = "F";
  10. personHolder[1][2] = "12 April 1992";
  11. personHolder[1][3] = "Single";
  12. personHolder[2][0] = "Peter";
  13. personHolder[2][1] = "M";
  14. personHolder[2][2] = "12 April 1980";
  15. personHolder[2][3] = "Married";
  16. //loop starts at one because you only want to show the persons
  17. for(int i = 1; i < 3; i++){
  18. for(int j = 0; j <= personHolder.length; j++){
  19. System.out.println(personHolder[0][j] + " " + personHolder[i][j]);
  20. }
  21. System.out.println();
  22. }
  23. }
  24. }
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.
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here