[ SOLVED] Need help with code

Discussion in 'Programming' started by AeriusMC, May 23, 2015.

  1. Been trying to figure this out for a few minutes now.. still don't get it but here's what I partially understand. So there is a multi-dimensional array with 15 elements, and I have a for loop that has a counter variable called row and when row is less than the grid length which is 15 it adds 1 more. So basically row is equal to 14.

    Next there is another for loop (Would this example be a nested for loop like a nested if statment?) and it has a counter variable called column and when it is less than the grid row length it adds 1? So basically the grid row length is 14 and now the column length is being added when it is less than 14? So the column length value is now 13? After that it prints out grid with the variables row and column which I have no idea how that works... if it printed out the values of row and column wouldn't t be 14 and 13?

    That's pretty much all I understand and I'm pretty sure half the things I understand about this code is wrong lol. Could someone explain it in great detail and please explain it like you would to a teenager who is learning... Thanks!
    Code (Text):
    package tutorialpackage;

    public class Tutorialclass {

        public static void main(String[] args) {
            int[][] grid = {
                {1, 2, 3, 4, 5},
                {6, 7, 8, 9, 10},
                {11, 12, 13, 14, 15}
            };
            for(int row=0; row<grid.length; row++) {
                for(int column = 0; column<grid[row].length; column++){
                    System.out.print(grid[row][column] + "\t");
                }
                    System.out.println();
            }
               
           
        }
    }
     
  2. Help... In desperate need...
     
  3. Sudzzy

    Patron

    You have three groups of numbers there and what you are trying to do in that code is to loop through each value and print it out to the console so it is easy to read.

    The first for loop written attempts to loop through each GROUP of numbers:

    Code (Text):
    for (int row = 0; row < grid.length; row++) {}
    I'll start by splitting down this for loop into easier sections.

    Code (Text):
    int row = 0;
    This is creating a variable which you can reference as an index later on in the code and signifies the amount of times this loop has gone through (or iterated).

    Code (Text):
    row < grid.length;
    This is either a true or false value, it checks whether or not the amount of times this for loop has iterated is smaller than the amount of groups of numbers. If it returns false (as in you have reached the final group of numbers) it will not attempt to go to the next loop.

    Code (Text):
    row++;
    Simply adds 1 to the index variable (row) once the loop has finished, so it will be larger on the next loop.

    Then in the inner (or nested) loop, it is looking through each of the values within this group of numbers:

    Code (Text):
    for (column = 0; column < grid[row]; column++) {}
    Code (Text):
    int column = 0;
    Creates a new index to find what point within this group of numbers the loop is at which is used later in the code to easily check where rpthe value is being taken from.

    Code (Text):
    column < grid[row];
    Returns a boolean (true or false) depending on whether we have finished every loop possible.

    Code (Text):
    column++;
    Adds 1 to the index, ready for the next loop.

    Code (Text):
    System.out.print(grid[row][column] + "\t");
    Simply prints out a piece of text depending on the current iteration. For example grid[1][3] would be 9. Then it will print a tab which is easily written in Java as " \t".

    Once all values have been looped through within the current group of values, it will print a new line:

    Code (Text):
    System.out.println();
    Ready to then have the next group of values to be written on the next line.

    Nice to see people learning Java, hopefully this will help you understand this a little more. :)
     
    #3 Sudzzy, May 23, 2015
    Last edited: May 23, 2015
    • Useful Useful x 1
  4. Sudzzy

    Patron

    To answer your questions:
    1. Yes, the second for loop is nested as it is within the first for loop.
    2. No, it does as many iterations as the amount of values the group of numbers currently has. This group of numbers is determined by the inner loop. Once the set of code has been completed for that specific value (counter), the counter will have 1 added to it and the code will be executed again for this new value.
    3. No, the grid row length starts at 0 and acts as a counter, it will look at the multi dimensional array and see 3 groups of numbers. Once the loop has gone through each of these groups, the code is complete.
    4. No, the column variable is another counter and that code is executed 3 times (starting at 0, then adding up until it reaches the maximum size of its current group of numbers).
    5. row and column are variables, and change depending on which loop you are at. The code starts at grid[0][0] which is 1. The next loop will then be grid[0][1] which is 2 up until grid[0][4] which is 5. Then it will go to the next group of numbers which will be grid[1][0] which is 6, then the next value would be grid[0][1] which is 7. It will keep doing this all the way until grid[2][4] which is 15.
     
    • Useful Useful x 1
  5. The thing I don't understand is there is a row variable and after the loop is done its value is 14. After that the nested for statement uses the length of the row variable and makes it so that if the column variable is < than row then it adds 1 so your result is 13..... wouldn't that print out the values as 14 and 13?
     
  6. Ok thanks for the response! I'm reading your responses and thinking hard so I'll probably be back with more questions lol. Btw I can't believe I'm talking to the owner of originmc, I played that server for alittle :).
     
  7. Sudzzy

    Patron

    Haha awesome :) I'll leave you to think this through a little, while I go to sleep. Tag me again in a message with @Sudzzy if you want me to explain any more to you when I wake up tomorrow.
     
    • Friendly Friendly x 1