Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Explanation on Arrays NEEDED

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
     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 ?
    Last edited by helloworld922; November 7th, 2009 at 01:31 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Explanation on Arrays NEEDED

    No, Here's a table of what the array looks like:

     
    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"

    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"

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Explanation on Arrays NEEDED

    Heh! i guess that the arrays section actually covers a lot of information then uh ? oh well, i guess u guys might be seeing my posts a lot then thanks for the help though i really really appreciate it.

Similar Threads

  1. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM
  2. Urgent code needed
    By subhvi in forum AWT / Java Swing
    Replies: 4
    Last Post: August 27th, 2009, 12:55 AM
  3. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  4. Java/J2EE with Linus/Unix
    By markthomas20 in forum Paid Java Projects
    Replies: 2
    Last Post: November 5th, 2008, 08:19 AM
  5. Replies: 4
    Last Post: May 22nd, 2008, 10:59 AM