Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieve values from ArrayList and adding it to an array

 
senthil kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList1,which got another ArrayList2 within.

Now I need to retrieve values inside ArrayList2 and add those to a double array, also need to find sum of this array.

How could I do this, any ideas will be higly appreciated.

Thanks
senthil
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a double array the same size as ArrayList2.
Initialise a double variable to zero.
For each item in ArrayList2, add the value to the double variable and insert it into the next element of the array.

Have a go at that and if you have any problems post your code telling us what the problem is (compilation - post the error(s), runtime - tell us what it is doing that you didn't expect)
 
senthil kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.Below code I tried will work fine when j=0.
But when j=1,i=0 it will just overlap the exisiting array, I know I need to get the length of latArr and lonArr, but don't know where to get that and how to pass that value into the loop so that my array values will not be overlaped.

for(int j=0;j<searchList.size();j++){
searchDetailList=searchList.get(j).getSearchList();
for(int i=0;i<searchDetailList.size();i++){
double d=Double.valueOf(searchDetailList.get(i).getLat());
double d1=Double.valueOf(searchDetailList.get(i).getLon());
Double latArr[]=new Double[searchDetailList.size()];
Double lonArr[]=new Double[searchDetailList.size()];
latArr[i]=d;
lonArr[i]=d1;
}
}

Actually I don't want the sum of the array, just need to send values of the above two arrays. sorry for the confusion.

Thanks
senthil
 
K. Tsang
Bartender
Posts: 3524
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find it hard to work with arraylist.size(), try using the enhanced for looping the arraylist, setting the j and i outside. And increment j and/or i accordingly. This should get you sorted with arraylist overlapping.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic