aspose file tools*
The moose likes Beginning Java and the fly likes Question regarding storing arraylists within arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question regarding storing arraylists within arraylist" Watch "Question regarding storing arraylists within arraylist" New topic
Author

Question regarding storing arraylists within arraylist

D. McPherson
Greenhorn

Joined: Feb 04, 2010
Posts: 10
Hi All,

I hope that someone help me out with a problem that I'm trying to address (and that I'm asking in the right part of the forum) - I am fairly new to Java.

I'll try to keep it brief I have a .csv file of data. At the moment I can get all the data from the .csv stored within an arraylist (arData in the code (below)) - in effect an arraylist of arrays (each line of the original .csv having been turned into an array of strings).
What I want to do is to examine the first entry of each array within the arraylist and use the value it contains (a date) to break the arraylist down further, into an arraylist of arraylists. In this way I should be better able to determine values for various fields e.g. max, average, etc.

I am currently looping through the arraylist, checking the value of each of the first elements and then adding that array to an arraylist (arListDay), then checking whether this is the last entry for that day. If so then arListDay is added to arListLocation, arListDay is cleared, and an arraylist for another day is begun. The problem is that when I clear arListDay - the "copied" data within arListLocation is also deleted (which I understand), but I'm drawing a blank in thinking of a way to do it that will work.

I've included the code in question below



Can anyone suggest a way to do this? Or point me in the right direction?

Thanks for any suggestions.



Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm confused, so you want to clear the ArrayList of all the elements except the one being added?

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
D. McPherson
Greenhorn

Joined: Feb 04, 2010
Posts: 10
Hi Hunter,

Sorry to take so long in replying to your rapid response to my post. I've been taking some anti-smoking medication that has completely messed-up my sleep cycle, as well as my thought processes. A sorry excuse I know, but the only one I have "The varenicline did it - not me".

What I want to do should, I feel, be quite simple (meaning, quite simple for me to solve on my own - but I've been at this for most of the day):

- Iterate item by item over the arraylist that I already have (arData)
- Check whether the first item in the arraylist has a first element that is the same as the one in the previous iteration
- If the same, then just append the item - in this case an array of data (array of Strings), to an arraylist called arListDay
- If different, then copy/write/do somethingwith the arraylist (arListDay) that has been created, in order to store it in another arraylist called arListLocation

The answer is most probably enormously simple - just right at the moment I simply can't see it, for whatever reason. I used to consider myself a fairly handy programmer (about ten years ago & in VB 4-6) - but Java is fairly new to me, OO too, and I can't see a means of copying the contents of arListDay to arListLocation, and then clearing the contents of arListDay, ready for it to receive data that pertains to a new day (without clearing from arListLocation the copy of the data that has just been made to it in the process).
OK - I can see the problem is to do with objects An arraylist is an object (as just about everything is in Java :thumbup. I'm adding that object (arListDay) to another arraylist (& object) (arListLocation), and then simply saying "arListDay.clear();" - which is obviously going to not just clear the 'surface' "arListDay", but also the "arListDay" that has just been added to the arListLocation. That's my problem!
Should I use some method like clone() to make a copy of the arraylist arListDay, then add the copy to arListLocation, prior to clearing the contents of arListDay? I take it that if I do then that is a "deep copy" - and so expensive, plus what happens when I create (when the next day's data has been stored in arListDay) and I use clone again - wont that simply overwrite the previous addition to arListLocation? - what I really just want to do is (if there were such a thing) a 'copy' 'byVal' - in order to just get a completely new arraylist that I can then simply append to arListLocation (call it arListDay1) and then empty arListDay using clear(). Then empty arListDay1.
Subsequently, I could just loop through the code in my first post - and refill arListDay, before adding more data from it (or it's copy (arListDay1) to arListLocation).

I hope this helps explain my problem (edited to add - probably not - I just reread the above and didn't underststand it myself! I'll repost in the morning) - or maybe explain why I'm having the problem I've been awake now, thanks to the GP prescribed anti-smoking medication, for 36 hours - and am having huge problems concentrating.

Thanks again for any replies

ETA - see second last paragraph above.

Don.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding storing arraylists within arraylist