Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding it all up

 
Shrey Puranik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have written some code, with an arraylist order and I have stored some doubles in there.

How can I output a total of all the doubles stored in the arraylist?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

By doubles, I presume you mean Doubles? You can easily iterate through the List with a for, for-each or iterator, and add the members. And remember
  • Sum = 0 + a + b + c
  • Product = 1 * a * b * c
  •  
    Shrey Puranik
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Welcome to JavaRanch

    By doubles, I presume you mean Doubles? You can easily iterate through the List with a for, for-each or iterator, and add the members. And remember
  • Sum = 0 + a + b + c
  • Product = 1 * a * b * c


  • The problem here is that new data is being added to the arrayList all the time-the total function is mainly so atthe end the user can find out wha tthe total is.

     
    Himanshu Gupta
    Ranch Hand
    Posts: 598
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you want to say that in your ArrayList insertions are happening all the time and you may want to now the total at any instance of time? Well you may simply call the method which adds all the elements present in the ArrayList. If you mean something else then please write here in detail.
     
    Shrey Puranik
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Himanshu Gupta wrote:Do you want to say that in your ArrayList insertions are happening all the time and you may want to now the total at any instance of time? Well you may simply call the method which adds all the elements present in the ArrayList. If you mean something else then please write here in detail.


    Gotcha-thanks :-)
     
    Shrey Puranik
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shrey Puranik wrote:
    Himanshu Gupta wrote:Do you want to say that in your ArrayList insertions are happening all the time and you may want to now the total at any instance of time? Well you may simply call the method which adds all the elements present in the ArrayList. If you mean something else then please write here in detail.


    Gotcha-thanks :-)


    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.
     
    Ankit Garg
    Sheriff
    Posts: 9510
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shrey Puranik wrote:
    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.


    I think your code has many errors. First of all you didn't tell that is the order ArrayList typed or not. I am assuming that it is not. So this would be the code

     
    Shrey Puranik
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ankit Garg wrote:
    Shrey Puranik wrote:
    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.


    I think your code has many errors. First of all you didn't tell that is the order ArrayList typed or not. I am assuming that it is not. So this would be the code



    by typed what do you mean? basically by clicking a button, the cuirrent element is added to the arraylist. ...
     
    Ankit Garg
    Sheriff
    Posts: 9510
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By typed I mean this

    ArrayList<Double> order = ...

    This would remove the unnecessary casts from the code. As per your need, you must execute the totaling code each time a new element is added into the list...
     
    Shrey Puranik
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ankit Garg wrote:By typed I mean this

    ArrayList<Double> order = ...

    This would remove the unnecessary casts from the code. As per your need, you must execute the totaling code each time a new element is added into the list...


    I have put:

    ArrayList order = new ArrayList();

    at the top of my class.
     
    Himanshu Gupta
    Ranch Hand
    Posts: 598
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ankit Garg wrote:
    Shrey Puranik wrote:
    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.


    I think your code has many errors. First of all you didn't tell that is the order ArrayList typed or not. I am assuming that it is not. So this would be the code



    Ankit it seems that you mean this


    if order is an Object of ArrayList.

     
    Ankit Garg
    Sheriff
    Posts: 9510
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes! Sorry I made a mistake in the code :P . I meant this

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic