• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

add up all the numbers within an arraylist

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"

 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Daniel Grinevich

Please show us what you have so far.
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76450
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Squanch that. And squanch this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic