Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A method that returns the sum of a given row (2d array)  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been struggling with how to properly write a method that returns the sum of a given row in a two dimensional array.
I'm trying to figure out how to get the values in the array row, and add them to the total.
This is the code I have at the moment;

 
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Luna wrote:I've been struggling with how to properly write a method that returns the sum of a given row in a two dimensional array.
I'm trying to figure out how to get the values in the array row, and add them to the total.
This is the code I have at the moment;



You are probably missing this at line 14:
 
Tom Luna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried adding the row to it, but I'm still getting an error;

java.lang.ArrayIndexOutOfBoundsException: 3
 
Sheriff
Posts: 12073
197
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your use of values.length is what is causing the error. That expression tells you how many rows there are, not how many elements there are in a row.

Note that in Java, you don't have multidimensional arrays. You have arrays that can have elements that are themselves arrays that can have elements that are themselves arrays that can ... and so on. In short, you can have nested arrays.

So if you declare int[][] num, then num[0] is a regular int[] that you can iterate over just as any regular int array.

In your case, values[row] is going to be an int[] and you can use the same code as you would to iterate over any int[]:

On line 2 above, you can use a regular for-loop or if you don't need an index variable, a for-each loop would be simpler.
 
Greenhorn
Posts: 1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi  Tom Luna.

In your loop, you've written

It means, you iterate the lines because you've specified the number of lines (values.length).
But in your case, you need to iterate the columns because you've already known the line (it is the row variable):

Then, your loop will be something like this:
.
In the body of the loop, you can add up all the values (code written by Chandran for line 14):
 
Marshal
Posts: 59392
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think everybody is giving suboptimal solutions because they are using a straight for loop.
 
Tom Luna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help guys! Much appreciated.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!