# Explanation on Arrays NEEDED

• November 7th, 2009, 12:10 AM
lamagiapc
Explanation on Arrays NEEDED
Alright so first of all i just signed up to this forum, i am really interested into learning the java programming language however, while reading the official tutorial from their official website (sun.com) i saw this example of a multidimensional array that i can't understand.

Example of program:
Code :

``` class MultiDimArrayDemo { public static void main(String[] args) { String[][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}}; System.out.println(names[0][0] + names[1][0]); //Mr. Smith System.out.println(names[0][2] + names[1][1]); //Ms. Jones } }```

First of all, why is it claiming both arrays position for every single part, i mean its doing names[0][0] + names[1][0] isn't it supposed to be for example names[0][1] for Mr. Smith ? why do i need to declare both positions of the arrays twice and not once ? i don't understand that small piece of development could anyone please give me a hand ?
• November 7th, 2009, 01:35 AM
helloworld922
Re: Explanation on Arrays NEEDED
No, Here's a table of what the array looks like:

Code :

```  Mr. Mrs. Ms. Smith Jones```

So, names[0] would give you the array {"Mr.", "Mrs.", "Ms."}. the second index extracts the item from this array you want, so names[0][0] would give you "Mr. ".

This of course isn't a complete name, so we need to get the last names array from names[1]: {"Smith", "Jones"}, and then extract the last name we want: names[1][0] = "Smith"

Put them together, and we get "Mr. Smith"

Code :

```System.out.println(names[0][0]); // print the prefix "Mr. " System.out.println(names[1][0]); // print the last name "Smith" System.out.println(names[0][0] + names[1][0]); // print both together "Mr. Smith"```
• November 7th, 2009, 09:03 AM
lamagiapc
Re: Explanation on Arrays NEEDED
oh ok, that explains a lot. i didn't know that we had to first call the array that we wanted to work with and stuff. thanks a lot man, that made things a lot more clear.
• November 7th, 2009, 01:33 PM
helloworld922
Re: Explanation on Arrays NEEDED
Yes, you can think of 2D arrays as an array of an array of something (that's not a typo, I really did mean to type "array of" twice)

The same applies with any N-D array. So, a 3D array would contain at each "bucket" 2D arrays, and inside of each 2D array will be 1D arrays, and inside each of those are the actual values.
• November 7th, 2009, 10:36 PM
lamagiapc
Re: Explanation on Arrays NEEDED
Heh! i guess that the arrays section actually covers a lot of information then uh ? :x oh well, i guess u guys might be seeing my posts a lot then :) thanks for the help though i really really appreciate it.