This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes How Add (Cumulative) using Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How Add (Cumulative) using Array List" Watch "How Add (Cumulative) using Array List" New topic
Author

How Add (Cumulative) using Array List

Bindu Sanju
Greenhorn

Joined: Feb 23, 2006
Posts: 16
Hi,

See the Below example, I added the values in ArrayList.These values I am fetching in loop. These iterator values I want to get it cumulative way. Values are dynamic. I am expecting the result like below

Actual value cumulative value

10 --------------- 10
20 --------------- 30(10+20)
30 --------------- 60(30+30)
50 --------------- 110(60+50)
80 --------------- 190(80+110) like result I am expecting.

Please see the below code and change if any modification required.

int value;
int A1=30;
int A2=300;
int B1=40;
int B2=400;
int C1=20;
int C2=200;
int D1=10;
int D2=100;
int tots=A1+A2;
int tot = 0;
int to = 0;
Bean b=new Bean();
ArrayList al=new ArrayList();
al.add(new Integer(A1));
al.add(new Integer(B1));
al.add(new Integer(C1));
al.add(new Integer(D1));
Iterator it=al.iterator();
for(int i=0;i<al.size();i++){
b.setObjValue(al.get(i));
Integer k= (Integer) b.getObjValue();
System.out.println(b.getObjValue()+" -------------- "+k );
double d=0;
d=d+k;
}


Awating for any + ve response with result.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

Here each time through the loop you are initializing d to 0, and then adding k. Then you do absolutely nothing with d. You should move the initilization of d outside the body of the for loop. You should also consider using more descriptive names for your variables.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Bindu Sanju
Greenhorn

Joined: Feb 23, 2006
Posts: 16
Hi Garett Rowe,

Thanks for your response. I tried what ever you mentioned, I am not able to compile , the error message is showing like

Error : The Operator (+) is undefined for the argument type(s) Double,Integer.

As per my knoledge it wont work out like this way.

If possible can you try in your local system Same code.

Thank you
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Bindu Sanju:
Hi Garett Rowe,

Thanks for your response. I tried what ever you mentioned, I am not able to compile , the error message is showing like

Error : The Operator (+) is undefined for the argument type(s) Double,Integer.

As per my knoledge it wont work out like this way.

If possible can you try in your local system Same code.

Thank you

If you are using a pre 1.5 version of Java then you have to exrtact the primitive value from the wrapper class to use the primitive math operators.
try:


[ April 24, 2007: Message edited by: Garrett Rowe ]
Bindu Sanju
Greenhorn

Joined: Feb 23, 2006
Posts: 16
Thanks for your response, but still its not working, I dont know whats the reason.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42363
    
  64
but still its not working, I dont know whats the reason.

This problem description gives us nothing to work with. What code are you running now, what are you expecting it to do, and what does it actually do?


Ping & DNS - my free Android networking tools app
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How Add (Cumulative) using Array List