how to find the sum of all the values in an int[][] Java?

int, java

Solution

Do you want to iterate all number in arrays,you can try with this:

           double threshold = 0;
           for(int i = 0; i < pixels.length; i++)
           {
               for(int j=0;j<pixels[i].length;j++){
                   threshold += (double)pixels[i][j];
               }
           }
           System.out.print(threshold);

Problem

i have an array of integer arrays 'int pixels[][]' and i want to find the sum of all of them so i can find the average pixel value this will be used so that i can set the average value of a pixel to be the threshold value for a pbm image if the value if above threshold i will export a white pixel if its below i will export a black one (just to give some context) i assume the code below is not correct at all as the output it 6.0 but i think its something like this ``` double threshold = 0; for(int i = 0; i < pixels.length; i++) { threshold += (double)pixels[i][i]; } System.out.print(threshold); ```

Original source