This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 12357
201
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: 60192
188
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!