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

Thread: confused about this for loop/ array?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default confused about this for loop/ array?

     
    int [] [] mat = new int [3] [4]; 
     
    for (int row = 0;  row < mat.length; row ++ ) 
    { 
     
        for ( int col = 0; col < mat[0].length; col++ ) 
        { 
     
             if ( row < col ) 
                mat [row] [col] = 1; 
                else if (row == col ) 
                mat [row] [col] = 2;
                else
                mat [row] [col] = 3; 
     
        }
    }




    I'm completely lost as how this work, could someone break it down into simple terms for me?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: confused about this for loop/ array?

    A 2D array requires two indexes to access an element in the array.
    The posted code uses the variable row to index into the first dim of the array
    and uses the variable col to index into the second dim of the array.
    mat[row][col] represents one element in the array.

    To see what is in the array, use the Arrays class's deepToString() method:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    GregBrannon (April 18th, 2014)

Similar Threads

  1. Getting confused need to add a for loop
    By iamgonge in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 25th, 2013, 04:40 PM
  2. [SOLVED] Confused without a teacher on a loop.
    By Hazmat210 in forum Loops & Control Statements
    Replies: 16
    Last Post: January 30th, 2013, 11:03 PM
  3. Array problems...newby...confused?
    By toppcon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 9th, 2011, 03:10 AM
  4. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 07:45 AM
  5. confused on loop
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2010, 03:26 PM