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

Thread: Can someone explain the logic behind looping through multidimensional arrays?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Can someone explain the logic behind looping through multidimensional arrays?

    Let say i have this code:
    public class ArrayDemo {
        public static void main (String[]args) {
     
            int [][] numbers = {{5,3,2},{7,5,3}};
            int sum=0;
            int index1;
            int index2;
     
            for (index1 =0; index1< numbers.length; index1++) {
            for (index2 =0; index2< numbers[index1].length; index2++) {
                 sum += numbers[index1][index2];
     
            }
        } 
           System.out.println("Sum: " + sum);
           System.out.println("Length: " + numbers.length);
     
    }}
    this is output
    output.PNG
    What does the statement
     for (index2 =0; index2< numbers[index1].length; index2++)
    mean?
    And why does the length = 2?
    Please i need an explanation in layman's terms.Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone explain the logic behind looping through multidimensional arrays?

    Please break down each element of the for loop you've highlighted and tell us what you think it means. For example, write your explanation for each of the following:

    index1:

    index2:

    numbers:

    numbers[index1]:

    numbers[index1].length:

    index2++:

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Can someone explain the logic behind looping through multidimensional arrays?

    ehm,
    index1 = the index of the first element (row) in the array, say numbers[0][1], no?
    index 2 = the index of the first element (column) in the array
    numbers = the array
    numbers[i]= the current index the loop is in
    numbers[i].length (no idea, this is in fact the source of my confusion)
    index2++ = no idea.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone explain the logic behind looping through multidimensional arrays?

    Good job, and that little exercise pointed to the source of your confusion.

    Some disagree with the description of arrays in Java as "multi-dimensional." They insist, for example, that a 2D array in Java, myArray[x][y], is 'x' arrays of 'y' arrays, and 'y' doesn't have to be the same for each 'x'. That's why the notation you're confused about is important.

    Imagine the array was defined this way instead of the way in your original post:

    int [][] numbers = {{5,3,2},{7,5}};

    In this case, y = 3 elements in the first row but only 2 elements in the second row. Then to get the iteration right in nested for loops, the inside loop - the one that iterates through each column element - has to account for this difference in length:
    numbers.length = 2      // for 2 rows
    numbers[0].length = 3  // for 3 elements in the first row (3 columns)
    numbers[1].length = 2  // for 2 elements in the second row (2 columns)

    Hope this helps.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    ojonugwa (August 27th, 2013)

  6. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Can someone explain the logic behind looping through multidimensional arrays?

    Thanks.That explains a lot

  7. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone explain the logic behind looping through multidimensional arrays?

    thanx to all nice sharing . this thread help me lot of..

Similar Threads

  1. Help With Multidimensional Arrays
    By tristannvk in forum Collections and Generics
    Replies: 4
    Last Post: December 20th, 2012, 03:05 PM
  2. Arrays/Looping probelm?
    By dx8292 in forum Collections and Generics
    Replies: 7
    Last Post: February 27th, 2012, 08:44 AM
  3. Pls explain me the code logic
    By Shajith in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:41 AM
  4. looping and arrays
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 23
    Last Post: January 7th, 2011, 10:16 PM
  5. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM