File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes retrieve values from ArrayList and adding it to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "retrieve values from ArrayList and adding it to an array" Watch "retrieve values from ArrayList and adding it to an array" New topic
Author

retrieve values from ArrayList and adding it to an array

senthil kumar
Greenhorn

Joined: Mar 19, 2008
Posts: 15
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: 3169
    
  10
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: 15
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

Joined: Sep 13, 2007
Posts: 1966
    
    7

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 OCPJP7
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieve values from ArrayList and adding it to an array
 
Similar Threads
i don't know what i'm doing
How to return two values back to the main from a for loop?
Retrieval of data from microsoft sql to pass into an array of String
logic:iterator
Retrieve maximum value