I'm working on a part of my Programming in Java 1 group project to save and load and ArrayList to/from a file but I'm getting an unchecked cast warning at the line:
Code:
computers = (ArrayList<Computer>) ois.readObject();
I've done a bit of research and found that I could just suppress the warning but that there should be a more elegant way to do it. I can't find it, however.
Relevant code:
Create the arraylist.
Code:
static ArrayList<Computer> computers = new ArrayList<Computer>();
Method to load serialized arraylist from file:
Code:
public static void loadData(){
//loads data from computers.dat to ArrayList computers
try {
//create file input stream
FileInputStream fis = new FileInputStream("computers.dat");
//create object input stream
ObjectInputStream ois = new ObjectInputStream(fis);
//copy data from file to object
computers = (ArrayList<Computer>) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
I can't see what I've done wrong. I've specified that the arraylist is only holding objects of the Computer class which is all I need to do according to what I've read.
Ideas?
Bookmarks