Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

add up all the numbers within an arraylist  RSS feed

 
Daniel Grinevich
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help with the code below. I'm working with Arraylists and I cant figure out how to add up all the numbers within an arraylist except for numbers like 10 and over.

 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, some clarifications needed.

Are you working with array types: i.e. int[]
or the Collections framework with java.util.List: i.e. List<Integer> ?

The word "add" has multiple meanings.
In mathematics add means to sum numbers together.
In terms of a list, add means "put on the end of the list"

So the "add" method you called on your numsList probably won't do what you want it to.

I would assume that you are dealing with an array.
In that case you probably want to
- set up a variable "total" to store the result
- loop through the array, adding each element from the array to "total"

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Daniel Grinevich

Please show us what you have so far.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Grinevich wrote:I need help with the code below. I'm working with Arraylists and I cant figure out how to add up all the numbers within an arraylist except for numbers like 10 and over.

forget about java...how would YOU do it? Tell me in English what you would do if you were given a bunch of numbers and had to complete the task.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what that means; I cannot see its relevance to the question in hand.

And don't post complete solutions.
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Daniel- I am assuming that the code from line no. 4-7 is basically used for populating an ArrayList with the values 1-10 and you are asking what we should write in line no. 15 to add up all these values.
If my assumption is correct then your question already has the answer. You'll just write another for loop to iterate over the list and in that loop will do something like sum += a[i], where sum is an int you defined before the loop with the value 0. So once the loop finishes, the sum variable will contain the sum of all the numbers.

Hope this helps.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am not sure the OP is still around to look for the answer.
Challenge: Now we have had Java8 for nearly two years, work out the average without using the keywords do for and while.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earilier today, I wrote:. . . Challenge: Now we have had Java8 for nearly two years, work out the average without using the keywords do for and while.
Solution:-
java ListAverager 273 872359 76324 -83745 12 0 -34875
118621.14285714286
java ListAverager
0.0
There are lots of other possible versions of that solution. I shall be very naughty and leave Winston to explain how that code works
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!