Results 1 to 4 of 4
Thread: Load data from file to ArrayList
- 01-04-2011, 01:58 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Load data from file to ArrayList
Hello,
I have written a program which includes this code
Instead of having this code reiteration at getData function, I would like to write a function getData(ArrayList array, String filename).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(); } } }
The problem with this function is when I try to load the data form the files to the arraylists.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(); }
At my main class I call this function by writting
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.Java Code:ArrayListManager list = new ArrayListManager(); list.getData(list.getArrayAdmin(), "Admin.dat"); list.getData(list.getArrayManager(), "Manager.dat"); .... list.getData(list.getArrayStorageUnit(), "StorageUnit.dat");
I would appreciate your help.Last edited by humbug; 01-04-2011 at 02:03 PM.
- 01-04-2011, 02:31 PM #2
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 ForumsHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-04-2011, 04:15 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
This line is creating a new ArrayList.Java Code:public void getData(ArrayList array, String filename) { ... array = (ArrayList) in.readObject(); ...
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:
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).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");
- 01-05-2011, 11:53 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Save/Load ArrayList
By chielt in forum New To JavaReplies: 3Last Post: 05-08-2011, 06:12 PM -
How do I load characters from a String into an arraylist with a boolean value?
By Grendel0 in forum New To JavaReplies: 0Last Post: 03-16-2010, 11:08 AM -
Arraylist Save and Load
By frankycool in forum Advanced JavaReplies: 1Last Post: 11-14-2009, 10:29 PM -
Load file to data structure
By TinoJF in forum New To JavaReplies: 4Last Post: 03-31-2009, 03:16 PM -
Storing data from text file in ArrayList
By tjhodge in forum New To JavaReplies: 1Last Post: 02-12-2009, 01:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks