// Project: Serialization // Module: serialization3 // Source code file EmployeeRoster.java // Create a programmer defined collection class, // which is able to serialize its own objects. import java.util.ArrayList; import java.util.Iterator; import java.io.*; public class EmployeeRoster implements Serializable, Iterable { private ArrayList col; public EmployeeRoster( ) { col = new ArrayList( ); } // The iterator method must be supplied to // implement the Iterable interface. @Override public Iterator iterator() { return col.iterator( ); } public void clear( ) { col.clear( ); } public Person get(int theIndex) { return col.get(theIndex); } public void add(Person thePerson) {col.add(thePerson); } public void remove(int theIndex) { col.remove(theIndex); } public int size( ) { return col.size( ); } public void save( ) throws IOException { ObjectOutputStream outStream = new ObjectOutputStream( new FileOutputStream("employee-roster.ser")); outStream.writeObject(col); outStream.close( ); } @SuppressWarnings("unchecked") public void load( ) throws IOException, ClassNotFoundException { ObjectInputStream inStream = new ObjectInputStream( new FileInputStream("employee-roster.ser")); col = (ArrayList) inStream.readObject( ); inStream.close( ); } }