how to loop though cols?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gamer2d
    New Member
    • Jul 2014
    • 10

    how to loop though cols?

    How can I set up my nested for loop so it loop though columns and not rows?

    I want to get min/Max value of a all column in a two-dimensional arra

    so for example, lets say you have the following 2d array:

    1 2 3
    4 5 6
    7 8 9

    Than the out put should be:

    -- col 1 --
    min value at col 1 is 1
    max value at col 1 is 7
    -- col 2 --
    min value at col 2 is 2
    max value at col 2 is 8
    -- col 3 --
    min value at col 3 is 3
    max value at col 3 is 9

    Code:
    public static void col(int[][] array) {
    
            for (int i = 0; i < array.length; i++) {
                int minValue = 0;
                int maxValue = 0;
                for (int j = 0; j < array[i].length; j++) {
                    if (minValue > array[j][i]) {
                        minValue = array[j][i];
                    }
                    if (maxValue < array[j][i]) {
                        maxValue = array[j][i];
                    }
                }
            }
    
        }
    error:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    having a quick look, I must say, your code will work perfectly when you switch the column and row variables in line 7 to 12.
    Your array should be accessed by "array[i][j]" and not by "array[j][i]".

    Your code can be optimized. Just store "array[i][j]" in a local variable called "currentVal ue" and use that instead of accessing the array again and again.

    For better readability, consider using Math.max() and Math.min() methods instead of if() statements.

    Comment

    • gamer2d
      New Member
      • Jul 2014
      • 10

      #3
      but if I do some thing like this below, wont that loop though rows and not cols? I am trying to find min of each cols.


      Code:
      public static void col(int[][] array) {
              for (int i = 0; i < array.length; i++) {
                  int minValue = 0;
                  int maxValue = 0;
                  for (int j = 0; j < array[i].length; j++) {
                      if (minValue > array[i][j]) {
                          minValue = array[i][j];
                      }
                  }
              }
       
          }

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Ok, you are right. But it is tricky to loop the other way around. because the size of each array can be different. For example you can have an array like this:
        Code:
        int[][] array = {{1,2}, {4,5,6}, {7}};
        So how do you know how many columns you have? The first subarray has 2, the second 3 and the third 1.
        You can try to do get the maximum in your double loop somehow if you need to optimize performance, but it is cleaner code to do it separately.
        So following plan
        1. Step: Make a look that gets the maximum array size of all subarrays
        2. Step: make another loop that only gets the first entry of each subarray (hard-coded index 0), that is the first column, and remembers the maximum value of all these entries. Remember that there could be a subarray of size 0, so check the size of the array first before accessing the entry and skip it if the array size is lesser than 1.
        3. step: now we want to do it for all columns: just surround the code in step 2 with a loop that increases the counter-variable "c" (c=column) each time until the maximum computed in step 1 is reached. Then replace all hardcoded index "0" in step 2 with "c". Adapt your array size check accordingly (hardcoded 1 --> c+1)

        I want you to do the steps, else you won't learn. If you have difficulties with one of these steps, then tell me and I will help you further on or just spoonfeed you (that means give you "teh codez")

        Comment

        Working...