Results 1 to 4 of 4
  1. #1
    dellacpa is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default

    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?
    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.
    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.

  3. #3
    dellacpa is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Norm,
    thanks for the answer, but I am really new to Java. Do you have an example of a code that I could use?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default

    Can you post your code for the Fish class?
    Sample method to create line to write to file:
    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);
    }
    This code is off the top of my head and may not compile or execute correctly.
    It is intended as an example only
    Last edited by Norm; 09-10-2010 at 06:21 PM.

Similar Threads

  1. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 04:34 PM
  2. how to print
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 03-18-2010, 03:23 PM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. Print XML tag
    By DonCash in forum XML
    Replies: 2
    Last Post: 08-07-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •