0

I have serialized an ArrayList and stored it to a file with extension .ser . How can I deserialize the object and store it back into an ArrayList. I am getting an error Note: Hw5b.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

//Serializing Object
ArrayList<String[]> list = new ArrayList<String []>();
FileOutputStream fout =  new FileOutputStream("movie-matrix2.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(list);

//Deserializing Object
FileInputStream streamIn = new FileInputStream("movie-matrix2.ser");
ObjectInputStream objectInputStream =  new  ObjectInputStream(streamIn);
list = (ArrayList<String[]>)objectInputStream.readObject();

1 Answer 1

1

It's not an error. It's a warning.

For details, you could recompile with ecompile with -Xlint:unchecked, just as the message says.

But you can basically do nothing about that warning, except suppressing it: the compiler warns you that there is now way to ensure that the list you get is an ArrayList<String[]>. Only that it's an ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

There are workarounds: Cast to (ArrayList<?>) for example, then cast individual elements as needed. Or, avoid serializing a Collection at all, by instead writing list.toArray(new String[0][]).
Yes, but that will make the code more complex, without brining any additional type safety.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.