Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Add (Cumulative) using Array List

 
Bindu Sanju
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bindu Sanju
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response, but still its not working, I dont know whats the reason.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic