0

I have a class Mold with the following constructor

public Mold(int[][] mold){
    this.mold = mold; 
  }

I also have a method sum() which looks like this:

public int sum(){
    int sum = 0;
    for( int i = 0; i <matrix.length; i++) {
      sum += matrix[i][i];
    }
    return sum;
  }

What I am trying to do is compute the sum of all the elements in Mold but I am unsure as to what to put in the sum += matrix[i][i]; part in place of 'i'. I might even be doing it the complete wrong way. I have been stuck on this for a while and I cannot seem to figure it out.

An example of the output I would like to get is: int[][] test1 = { { 10, 5 }, { 2, 8 } }; should give 25 one the sum method has been applied.

If someone could push me in the right direction that would be great! Many thanks,

2
  • 3
    Serach for - "How to iterate an array of array in Java".
    – Rohit Jain
    Commented Sep 19, 2013 at 16:36
  • 1
    You are only summing up a diagonal of the matrix (10 and 8). Try 2 for loops, one looping through 'x' and the other 'y'
    – tcooc
    Commented Sep 19, 2013 at 16:38

6 Answers 6

2

You are doing this:

Supposing a bi-dimensional array of 3x3 elements these are their coordinates (row, column):

[0,0][0,1][0,2]
[1,0][1,1][1,2]
[2,0][2,1][2,2]

The way you are counting using only one loop and only one variable in the loop is doing this:

i = 0 (counting the elemen 0,0)

[*][ ][ ]
[ ][ ][ ]
[ ][ ][ ]

i = 1 (counting the elemen 1, 1)

[ ][ ][ ]
[ ][*][ ]
[ ][ ][ ]

i = 2 (counting the elemen 2,2)

[ ][ ][ ]
[ ][ ][ ]
[ ][ ][*]

You need to use a double loop to do the sum. Loop every row and for every row looped you have to loop for each column of that row.

Something like this:

for (i = 0; i < maxRows; i++){
 for (j = 0; j < maxCols; j++{
  //
 }
}
1

The way you have it, you will only sum up positions [0][0], [1][1], [2][2], ..., [i][i] and you will miss most of the elements.

You have 2 dimensions in your array, so you need 2 loops.

Create another for loop inside your existing for loop, with a different looping variable, j. It will stop when it reaches the end of the current row's length: matrix[i].length. When adding to sum, use both looping variables: matrix[i][j].

1

In your current approach, you're just adding the elements in the diagonal (0,0), (1,1), (2,2). If handling an array of array, you can use the enhanced for loop twice: one to loop over the elements in the array and another to loop over the elements in the array of the array:

int sum = 0;
for(int[] moldElement : mold) {
    for(int element : moldElement) {
        sum += element;
    }
}
return sum;
1

Think about what you are doing exactly and how to accomplish it.

Remember a 2d array is an array of arrays.

for( int i = 0; i <matrix.length; i++) {
      sum += matrix[i][i];
}

`sum[i][i]` in this code is essentially doing this.
     0  0
     1  1
     2  2
     3  3
     4  4
     ...

What you need it to be doing is

     0   0 
     0   1
     0   2
     ...
     1   0
     1   1 
     1   2
     ...

Think of it like coordinates X and Y!

this would be your answer.

for(int i = 0; i < matrix.length; i++){
    for(int j = 0; z < matrix[j].length; j++{
        sum += matrix[i][j];
    }
}

Notice this line in particular. matrix[i].length What if the array wasn't completely uniform??

for instance.

  i
j[0][][][][][]
 [1][]
 [2][][][]
 [3][][][][][][]

this still would able to iterate through.

0

do double for loops.

int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
    for(int j = 0; j < matrix[0].length; j++)
    {
        sum += matrix[i][j];
    }
}
return sum;
4
  • 2
    Matrix[0] is incorrect because you don't know how long the matrix[1] will be.
    – Brad
    Commented Sep 19, 2013 at 16:40
  • It is a 2D array, all elements will have the exact same length irregardless of position.
    – Quillion
    Commented Sep 19, 2013 at 16:41
  • false. int [][]foo = {{1,2,3}, {4, 5, 6, 7}}; compiles and foo[i] has different lengths
    – Brad
    Commented Sep 19, 2013 at 16:54
  • @progehand wow I never knew that. Thanks so much :) this is quite enlightening, and will help in future
    – Quillion
    Commented Sep 19, 2013 at 16:59
0

Nest the for loop

 public int sum(){
    int sum = 0;
    for( int i = 0; i <matrix.length; i++) {
        for (int j = 0; k < matrix[i].length; j++){
            sum += matrix[i][j];
        }
    }
    return sum;
  }

As someone said, you're only iterating over one dimension of the array. When you have n dimensions of an array, you need n loops.

Not the answer you're looking for? Browse other questions tagged or ask your own question.