如何在int [] [] Java中找到所有值的总和?

时间:2021-09-03 08:35:50

i have an array of integer arrays 'int pixels[][]'

我有一个整数数组的数组'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

这将被使用,以便我可以将像素的平均值设置为pbm图像的阈值

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将导出一个白色像素,如果它下面我将导出一个黑色像素(只是为了给出一些上下文)

i assume the code below is not correct at all as the output it 6.0 but i think its something like this

我假设下面的代码根本不正确作为6.0的输出,但我认为它是这样的

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

5 个解决方案

#1


5  

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);

#2


0  

HINT

暗示

You have a 2D array, So to add each element you need to traverse each column of each row. For this 2 for loops might be of some use.

你有一个2D数组,所以要添加每个元素遍历每一行的每一列。对于这2个for循环可能有一些用处。

traverse over each value and add them

遍历每个值并添加它们

exit loop

退出循环

Divide your sum with number of elements to get average (you may need to give some thought to type casting here)

将您的总和除以元素数量以获得平均值(您可能需要考虑在此处输入类型)

#3


0  

You need to iterate over each row and column in the array, so you need 2 for-loops. What you're currently doing only covers the following: (0,0), (1,1), (2,2), ....

您需要遍历数组中的每一行和每列,因此您需要2个for循环。你目前所做的只包括以下内容:(0,0),(1,1),(2,2),....

This would work: (assuming a non-jagged array, at least for the threshold calculation)

这将工作:(假设一个非锯齿状数组,至少为阈值计算)

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

Or more simply: (assuming a non-jagged array, at least for the threshold calculation)

或者更简单:(假设一个非锯齿状阵列,至少用于阈值计算)

long sum = 0;
for (int[] i: pixels)
for (int j: i)
{
   sum += j;
}
double threshold = (double)sum / (pixels.length * pixels[0].length);
System.out.print(threshold);

#4


0  

You need to use a double for here. Something like this:-

你需要在这里使用双。像这样: -

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

#5


0  

you dont need a double when you count sum of ints, this is a waste, use long, it's faster

当你计算总数时,你不需要双倍,这是浪费,使用时间长,速度更快

    long sum = 0;
    int n = 0;
    for (int[] a : pixels) {
        for (int e : a) {
            sum += e;
            n++;
        }
    }
    double avg = ((double) sum) / n;

#1


5  

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);

#2


0  

HINT

暗示

You have a 2D array, So to add each element you need to traverse each column of each row. For this 2 for loops might be of some use.

你有一个2D数组,所以要添加每个元素遍历每一行的每一列。对于这2个for循环可能有一些用处。

traverse over each value and add them

遍历每个值并添加它们

exit loop

退出循环

Divide your sum with number of elements to get average (you may need to give some thought to type casting here)

将您的总和除以元素数量以获得平均值(您可能需要考虑在此处输入类型)

#3


0  

You need to iterate over each row and column in the array, so you need 2 for-loops. What you're currently doing only covers the following: (0,0), (1,1), (2,2), ....

您需要遍历数组中的每一行和每列,因此您需要2个for循环。你目前所做的只包括以下内容:(0,0),(1,1),(2,2),....

This would work: (assuming a non-jagged array, at least for the threshold calculation)

这将工作:(假设一个非锯齿状数组,至少为阈值计算)

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

Or more simply: (assuming a non-jagged array, at least for the threshold calculation)

或者更简单:(假设一个非锯齿状阵列,至少用于阈值计算)

long sum = 0;
for (int[] i: pixels)
for (int j: i)
{
   sum += j;
}
double threshold = (double)sum / (pixels.length * pixels[0].length);
System.out.print(threshold);

#4


0  

You need to use a double for here. Something like this:-

你需要在这里使用双。像这样: -

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

#5


0  

you dont need a double when you count sum of ints, this is a waste, use long, it's faster

当你计算总数时,你不需要双倍,这是浪费,使用时间长,速度更快

    long sum = 0;
    int n = 0;
    for (int[] a : pixels) {
        for (int e : a) {
            sum += e;
            n++;
        }
    }
    double avg = ((double) sum) / n;