Results 1 to 4 of 4
  1. #1
    humbug is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Question Load data from file to ArrayList

    Hello,

    I have written a program which includes this code

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.HashSet;
    
    /**
     * The purpose of this class is to be an
     * ArrayList manager. It helps manage the
     * ArrayLists and the HashSet and have
     * access to them and their data.
     */
    public class ArrayListManager {
    
    
        private ArrayList<Admin> admin;
        private ArrayList<Manager> manager;
        private ArrayList<Employee> employee;
        private ArrayList<Keyboard> keyboard;
        private ArrayList<Mouse> mouse;
        private ArrayList<Printer> printer;
        private ArrayList<StorageUnit> storageUnit;
        private ArrayList<Order> order;
        private HashSet<String> mySet;
    
    /**
         * This is the constructor of the class ArrayListManager. 
         * It creates eight different arraylists and one HashSet.
         */
        public ArrayListManager()
        {
            admin = new ArrayList<Admin>();
            manager = new ArrayList<Manager>();
            employee = new ArrayList<Employee>();
            keyboard = new ArrayList<Keyboard>();
            mouse = new ArrayList<Mouse>();
            printer = new ArrayList<Printer>();
            storageUnit = new ArrayList<StorageUnit>();
            order = new ArrayList<Order>();
            mySet = new HashSet<String>();
        }
        
        /**
         * This function returns an admin ArrayList.
         * @return The list with the administrators.
         */
        public ArrayList getArrayAdmin()
        {
            return admin;
        }
        
        /**
         * This function returns a manager ArrayList.
         * @return The list with the managers.
         */
        public ArrayList getArrayManager()
        {
            return manager;
        }
    
        /**
         * This function returns an employee ArrayList.
         * @return The list with the employees.
         */
        public ArrayList getArrayEmployee()
        {
            return employee;
        }
    
        /**
         * This function returns the ArrayList keyboard.
         * @return The list with the keyboards.
         */
        public ArrayList getArrayKeyboard()
        {
            return keyboard;
        }
    
        /**
         * This function returns the ArrayList mouse.
         * @return The list with the mice.
         */
        public ArrayList getArrayMouse()
        {
            return mouse;
        }
    
        /**
         * This function returns the ArrayList printer.
         * @return The list with the printers.
         */
        public ArrayList getArrayPrinter()
        {
            return printer;
        }
    
        /**
         * This function returns the ArrayList storageUnit.
         * @return The list with the storage units.
         */
        public ArrayList getArrayStorageUnit()
        {
            return storageUnit;
        }
    
        /**
         * This function returns the ArrayList order.
         * @return The list with the orders.
         */
        public ArrayList getArrayOrder()
        {
            return order;
        }
    
     /**
         * This functions gets the data from the files Admin.dat, Manager.dat, Employee.dat,
         * Keyboard.dat, Mouse.dat, Printer.dat, StorageUnit.dat, Order.dat and MySet.dat.
         * In this way, any changes that had been saved during a previous execution of the
         * programme are loaded again.
         */
        public void getData() {
    		FileInputStream fis = null;
    		ObjectInputStream in = null;
    		try {
    			fis = new FileInputStream("Admin.dat");
    			in = new ObjectInputStream(fis);
    			admin = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
    		try {
    			fis = new FileInputStream("Manager.dat");
    			in = new ObjectInputStream(fis);
    			manager = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
                    try {
    			fis = new FileInputStream("Employee.dat");
    			in = new ObjectInputStream(fis);
    			employee = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
                    try {
    			fis = new FileInputStream("Keyboard.dat");
    			in = new ObjectInputStream(fis);
    			keyboard = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
                    try {
    			fis = new FileInputStream("Mouse.dat");
    			in = new ObjectInputStream(fis);
    			mouse = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
                    try {
    			fis = new FileInputStream("Printer.dat");
    			in = new ObjectInputStream(fis);
    			printer = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
                    try {
    			fis = new FileInputStream("StorageUnit.dat");
    			in = new ObjectInputStream(fis);
    			storageUnit = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
                    
                    try {
    			fis = new FileInputStream("Order.dat");
    			in = new ObjectInputStream(fis);
    			order = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
    
    	}
    }
    Instead of having this code reiteration at getData function, I would like to write a function getData(ArrayList array, String filename).

    Java Code:
    public void getData(ArrayList array, String filename) {
    		FileInputStream fis = null;
    		ObjectInputStream in = null;
    		try {
    			fis = new FileInputStream(filename);
    			in = new ObjectInputStream(fis);
    			array = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
    The problem with this function is when I try to load the data form the files to the arraylists.

    At my main class I call this function by writting

    Java Code:
    ArrayListManager list = new ArrayListManager();
    list.getData(list.getArrayAdmin(), "Admin.dat");
    list.getData(list.getArrayManager(), "Manager.dat");
    ....
    list.getData(list.getArrayStorageUnit(), "StorageUnit.dat");
    The problem is that when I print the size of the arraylists using the function list.getArrayAdmin().size(), it is 0 which shows that the data is not loaded.

    I would appreciate your help.
    Last edited by humbug; 01-04-2011 at 03:03 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    So put some print statements in there, or better yet use a debugger, to figure out where the code does something different from what you expected. When you have it narrowed down to a couple lines, we can go from there.

    Crossposted: Data from file into ArrayList - Java Programming Forums
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Java Code:
    public void getData(ArrayList array, String filename) {
    ...
    			array = (ArrayList) in.readObject();
    ...
    This line is creating a new ArrayList.
    Since Java is pass by value, you are essentially overwriting the value (the reference) held by array. This does not change the reference passed in at all, so is not "seen" by whatever code is calling this method.
    For example:
    Java Code:
    ArrayList myArray = new ArrayList(); // This is my array.
    // In this method the reference to the above array is overwritten and populated with the
    // data from the file.  However myArray here still refers to the above empty ArrayList.
    getData(myArray, "some file name");
    So, you can either create a new list in the getData() method and use addAll() (I think that's the method) to add them all to the array parameter, or you can return the array list and the calling method can assign it (or whatever).

  4. #4
    humbug is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you so much! It actually worked with addAll(Collection)!!

Similar Threads

  1. Save/Load ArrayList
    By chielt in forum New To Java
    Replies: 3
    Last Post: 05-08-2011, 07:12 PM
  2. Replies: 0
    Last Post: 03-16-2010, 12:08 PM
  3. Arraylist Save and Load
    By frankycool in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2009, 11:29 PM
  4. Load file to data structure
    By TinoJF in forum New To Java
    Replies: 4
    Last Post: 03-31-2009, 04:16 PM
  5. Storing data from text file in ArrayList
    By tjhodge in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 02:22 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
  •