English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can you help me by giving some tips on answering this question.It's easy question but I'm a Beginner in JAVA programming. It's goes like this :write a following program to display a person's name in the following style. For example, the name :
William Jefferson Clinton
would produce this output
clinton,William J (multidimensional array is used)

2007-03-15 04:52:29 · 3 answers · asked by Niven 1 in Computers & Internet Programming & Design

Tks 4 the answer, anyway the program should go like this, it will ask us to enter name.we'll have enter the name.It is not static value.

2007-03-19 01:28:47 · update #1

3 answers

In java (unlike C), 2D array is just an 1D array of 1D arrays, hope that makes sense. Think of it as a regular array of Objects, which happen to be themselves arrays.

IMPORTANT

new String [3][2]

DOES NOT WORK!!!


So here's what you do:

// use whatever's appropriate
int numPeople = 3;

// initialize the first dimension
String [][] people = new String [numPeople] [];

// now iterate through the array, and initialize the second dimension

for (int i = 0; i < numPeople; i ++)
{
// initializing to 2 fields:first name and last name
people [i] = new String [2];
}

// now insert some people
people [0][0] = "Clinton";
people [0][1] = "Hillary";
people [1][0] = "Hussein";
people [1][1] = "Saddam";
// etc.

// here's how you'd print it:

for (int i = 0; i < people.length; i ++)
{
System.out.println ("Person #" + (i+1) + ": " + people [i][0] + ", " + people [i][1]);
}


cheers!

2007-03-15 05:15:00 · answer #1 · answered by iluxa 5 · 0 0

Here is some tip? Since this is Java/C# not C/C++ it should be easy to figure out the rest.

String[][] names = new String[3][];
// the 3 is Given Middle Surname; deliberately leaving the 2nd index off for x number of names.

Some would argue I give the indexing wrong but I don't think so

2007-03-15 05:10:52 · answer #2 · answered by Andy T 7 · 0 1

Declare the multi-dimensional array:

String[MAX_ROWS][2] names;

Initialize:
names[0][0] = "William J";
names[0][1] = "Clinton";
...

Then to output:

for(int i=0; i System.out.println(names[i][1] + "," + names[i][0]);

2007-03-15 05:10:02 · answer #3 · answered by Anonymous · 0 1

fedest.com, questions and answers