| Author |
retrieve values from ArrayList and adding it to an array
|
senthil kumar
Greenhorn
Joined: Mar 19, 2008
Posts: 13
|
|
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
Joined: Aug 05, 2005
Posts: 3011
|
|
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)
|
Joanne
|
 |
senthil kumar
Greenhorn
Joined: Mar 19, 2008
Posts: 13
|
|
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
Ranch Hand
Joined: Sep 13, 2007
Posts: 1259
|
|
|
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.
|
K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
|
 |
 |
|
|
subject: retrieve values from ArrayList and adding it to an array
|
|
|