So basically, I have had these a few superclasses, with a of their few sublcasses forming up a small program. As for the file handling, since I was using “Arrays”, I used writeobject and readobject method. But now, I have changed all my arrays to “ArrayLists”, but when it comes to file handling, it does not work as before, obviously.
there are 2 specific parts that I get error, first is:
for(i=0;i<100;i++)
{
customerOOS.writeObject(Customer[i]);//since we no longer have (Customer[]) array, and instead Custarray of arraylist
}
.
.
.
Second part:
for (i=0;i<100;i++)
{
Customer[i] = (Customer)customerOIS.readObject();//same problem again
}
So, what should I replace these parts with, so then it works. Or should I use a completely different way to do file handling of the ArrayLists?
An
ArrayListis an implementation ofList(which is backed by a dynamic array) which is a container ofObjects.This container has very specific and well document methods which allow you to manage the list of elements.
In order to retrieve an element from the list, you need to use
List#get(int)To add a new element, you need to use
List#add(Object)I would take some time to study the API documentation and have a read through the Collections trail, as these are very basic concepts