Results 1 to 4 of 4
Thread: print out an arraylist
- 09-08-2010, 04:59 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
print out an arraylist
Hi, I am working on a simulation model for fish.
My model has a class called SpawningGround which contains an Arraylist<Fish>. (So in other words a SpawningGround is composed of a lot of Fish)
What I would like to know is the following. What code/classes do I need in order to save as a csv or a txt file the arraylist<Fish> for each one of my spawningGrounds?
(PS: each line of the ArrayList<Fish> represents an individual fish, and each column of the array a proprety of the fish)
Thanks for your help,
Patrick
- 09-08-2010, 10:46 PM #2
You could write the contents of each ArrayList to its own text file. Each line in the text file would have all the data for an entry/object in the ArrayList.What code/classes do I need in order to save as a csv or a txt file the arraylist<Fish> for each one of my spawningGrounds?
Add two methods to the object:
1) to create a line for the text file that contains the data for a fish
2) A constructor that takes a line from the text file and creates the fish object.
- 09-10-2010, 04:52 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
Norm,
thanks for the answer, but I am really new to Java. Do you have an example of a code that I could use?
- 09-10-2010, 05:19 PM #4
Can you post your code for the Fish class?
Sample method to create line to write to file:
This code is off the top of my head and may not compile or execute correctly.Java Code:final String SepChar = "|"; // field separater // Fish has two properties: name and type String createRecord() { return name + SepChar + type; // build string with the Fish's properties } //Sample constructor: Fish(String str) { // parse record created by the createRecord method int ix = str.indexOf(SepChar); name = str.substring(0, ix); type = str.substring(ix+1); }
It is intended as an example onlyLast edited by Norm; 09-10-2010 at 05:21 PM.
Similar Threads
-
Creating an ArrayList from an ArrayList
By Klahking in forum New To JavaReplies: 17Last Post: 09-09-2010, 03:34 PM -
how to print
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 03-18-2010, 02:23 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Print XML tag
By DonCash in forum XMLReplies: 2Last Post: 08-07-2007, 06:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks