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 4 of 4

Thread: Assigning Values to Multi Array

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assigning Values to Multi Array

    Hi,

    I'm new to Java programming, and trying to thoroughly understand multidimensional arrays and how they work.

    If I entered the following, it appears the following multi array (table) would be created:

    int[][] table = {{44,22,43},
    {55,39,88},
    {92,40,99}};

    ----------------
    | 44 | 22 | 43 |
    ---------------
    | 55 | 39 | 88 |
    ---------------
    | 92 | 40 | 99 |
    ---------------

    Then if I later called:

    System.out.println(table[0][2]);

    This would print out row 1, column 3: 43.

    Another option (to do the same thing):

    int[][] table = new int[3][3];

    Then, I could assign an array to a variable, like this:

    int[] t0 = { 44, 22, 43 };
    int[] t1 = { 55, 39, 88 };
    int[] t2 = { 92, 40, 99 };

    ...and I could then assign each variable to my original array like this:

    table[0] = t0;
    table[1] = t1;
    table[2] = t2;

    I have 2 questions:
    1. Is there an advantage to either approach?
    2. With the latter approach I've tried assigning the arrays directly to the original variable without success. For example:

    table[0] = { 44, 22, 43 };

    This produces an error though (i.e. Illegal start of expression). Is there a reason I need to create temp variables for each array before adding it to the main array (i.e. table)?

    Thanks


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

    Default Re: Assigning Values to Multi Array

    Directly initializing an N-d array can only be done when the array is first declared.

    int a[] = {1,2,3}; // understood implicitly as int a[]= new int[]{1,2,3};
    int b[];
    b = new int[]{1,2,3};

    It depends on the application. If you know what values you're going to put into an array already, why bother splitting it up into multiple statements? On the other hand, sometimes you don't know what's suppose to be in each row, so you'd use the second approach. The second method also lets you produce jagged arrays (arrays where different rows can have different widths).

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assigning Values to Multi Array

    Gotcha - thanks!

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

    Default Re: Assigning Values to Multi Array

    Hmm, I must have been tired when I posted that (and/or crazy), but either method can actually be used to create jagged arrays. However, the other info should be accurate.

Similar Threads

  1. Help on 2D Multi Table Array
    By clevel211 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 1st, 2011, 04:28 AM
  2. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM
  3. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  4. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  5. Assigning variable to an Array
    By sridevi in forum Collections and Generics
    Replies: 3
    Last Post: August 10th, 2009, 10:58 PM