Results 1 to 4 of 4
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default multidimensional array prob T_T

    Java Code:
    class TwoDArray {
    public static void main(String args[]) {
    int twoD[][]= new int[4][5];
    int i, j, k = 0;
    for(i=0; i<4; i++)
    for(j=0; j<5; j++) {
    twoD[i][j] = k;
    k++;
    }
    for(i=0; i<4; i++) {
    for(j=0; j<5; j++)
    System.out.print(twoD[i][j] + " ");
    System.out.println();
         }
       }
    
    
    }
    this is the output
    Java Code:
    0 1 2 3 4
    5 6 7 8 9
    10 11 12 13 14
    15 16 17 18 19
    can someone explain how this code works(in complete detail please :D).

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: multidimensional array prob T_T

    Please go through Code Conventions for the Java Programming Language: Contents and learn how to format code for readability.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: multidimensional array prob T_T

    ok:

    Java Code:
    int twoD[][]= new int[4][5];
    The above line of code create an array of arrays (also known as 2d array, or multi dimensional array). This will be structured as follow:

    4 "rows"
    5 "elements" in each row

    Also the contents of each element must be an integer as specified by this line : new int[4][5]

    This matches the format of your output


    Next nested for loop:

    Java Code:
    int i, j, k = 0;
    for(i=0; i<4; i++)
    for(j=0; j<5; j++) {
    twoD[i][j] = k;
    k++;
    first of all it instantiates the values of integers i, k, and j as 0. int i, j, k = 0;

    Now the nested for loop happens, i assume you know about for loops in general. First of all the outer most for loop which counts i is used to iterate through the rows in the the 2d array. so when i = 0, it refers to row 0 (the first row) in the table of out put, (in this case 0 1 2 3 4 = Row 0)

    the same applied for the J for loop, but this keeps track of the columns, or elements in said "i" row. This for loop completes before the i value is incremented. The j for loop with execute 5 times before you move onto the next row. (next row means i ++)

    Inside the J for loop the value are assigned to the array

    Java Code:
    twoD[i][j] = k;
    k++;
    twoD[i][j] refers to the location in the 2D array, twoD[0][0] refers to the very first value in the array. Then you set the value at this location to the value of k (which is currently 0).

    At this stage this means that the lines:
    Java Code:
    twoD[i][j] = k;
    k++;
    translates to:
    Java Code:
    twoD[0][0] = 0; //i = 0, j = 0 and k = 0 AT THIS POINT
    k++;

    Now the k value is increased by one using k++ so Now k = 1 Also the J for loop has completed one cycle so the J value increases, J Value is now 1


    Lets run the code again

    At this stage this means that the lines:
    Java Code:
    twoD[i][j] = k;
    k++;
    translates to:
    Java Code:
    twoD[0][1] = 1; //i = 0, j = 1 and k = 1 AT THIS POINT
    k++;

    So now the value at [0][1] will be 1, [0][1] is the 2nd element in the first row (remember Java counts from 0!) Then we increment J and K again.

    One this has happened 5 times, we increment i (i++) and execute the J for loop another 5 times

    now that i has been incremented the code is as follows:

    At this stage this means that the lines:
    Java Code:
    twoD[i][j] = k;
    k++;
    translates to:
    Java Code:
    twoD[1][0] = 5; //i = 1, j = 0 and k = 5 AT THIS POINT
    k++;
    And the pattern happens again (see above explanation)


    This cycle continues till the I value equals 3 and the J value = 4 (remember java counts from 0, so i = 3 means 4 rows, j = 4 means 5 columns)

    Then the code simply prints out the value at every point in the grid.


    Printing example
    Java Code:
    System.out.print(twoD[i][j] + " ");
    System.out.println(); // PRINTS A BLANK LINE

    for the first element in the array

    Java Code:
    System.out.print(twoD[0][0] + " "); // i and j start off as 0
    System.out.println();
    this will look in the 2D Array and look up the value at [0][0] ([row][column]) and simply print it out to the console.
    This will again happen the same amount of times as the previous explanation.

    Hope this explains everything

  4. #4
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: multidimensional array prob T_T

    Thank you for your explanation and now i understand this code. i will comment again if i have any questions.

Similar Threads

  1. Multidimensional Array
    By TyCox94 in forum New To Java
    Replies: 11
    Last Post: 09-25-2011, 11:00 PM
  2. Multidimensional char array
    By leepikamukharji in forum New To Java
    Replies: 18
    Last Post: 04-13-2011, 01:18 PM
  3. Multidimensional Array help
    By Gaebril in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 08:36 PM
  4. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 03:02 PM
  5. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-22-2009, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •