• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding sum of two dimensional Array  RSS feed

 
ivan auyoung
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, ive got a college question asking me to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3.
after this i need to add up all the element in this array and return the value.


this is what i have done so far(not 100% if is correct..) but i have no idea how to add up all the array.

thank you
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are you sure you want to increment i here?

Except for that your code looks OK.

How do you sum?
Declare sum variable before the loop with initial value 0 and add foo[i][j] to it in the loop.

PS. Do you realize that there are no multidimensional arrays in java?
There are merely arrays of arrays.
int[][] is an one-dimensional array that stores int[] references.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivan auyoung wrote:Hi, ive got a college question asking me to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3. after this...

Just FYI, you can make your loops more generic by simply using the 'length' attribute. What you've done isn't wrong, but it is tied to your declared constants.is more generic, and you can probably copy and paste it wherever it's needed.

And if you only need to read elements, for-each is even more concise:I'll leave you to suss out how it works.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Arrays#fill instead of that loop. But you have to do it twice, because the arrays are of different types.

You have not got a 2D array because Java® does not support 2D arrays. What you have is an array of arrays, which is different (and better).
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What you have is an array of arrays, which is different (and better).

I disagree. More flexible? Yes. Better? That depends.
In real 2D array length in any dimension is fixed. So no need to check the length of row for every column etc.
And you don't have to worry that some intermediate array might be null.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:I disagree. More flexible? Yes. Better? That depends.
In real 2D array length in any dimension is fixed. So no need to check the length of row for every column etc.
And you don't have to worry that some intermediate array might be null.

But either way, infinitely better than '**args'.

I'm also not quite sure, given that the Java designers wanted an array to be an object, how they could have implemented it without an extra 'Matrix' or 'Grid' class, which you could easily implement yourself (I have).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!