-
Serializable Class
I have a class that needs to implement the "Serializable" interface. I am doing this because I am saving an instance of my class to a binary file. And in order to do that it needs to be Serializable.
The name of the class is "Animation". It is a collection of images.
And has a lot of functions to make an animation of these images. I am not going to mention those here though. Because they have nothing to do with my question.
My class looks a bit like this
Code:
public class Animation(){
ArrayList files; // a collection of Strings (the path of the imagefile)
ArrayList images; // a collection of BufferedImages
public Animation(){ //a constructor
}
public void addFile(String file){ //function to add new Images
images.add(loadimages(file));
files.add(file);
}
}
now what functions do I need to add to make it Serializable ?
I added the implements Serializable on top of my class and then I tried the following functions ...
Code:
private void writeObject(java.io.ObjectOutputStream out)
throws IOException{
out.writeObject(files);
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
String[] imported_files = (String[])in.readObject();
setImageArray(imported_files);
}
private void setImageArray(String[] imported_files){
images.clear();
files.clear();
for(int i=0;i<imported_files.length;i++){
images.add(loadimages(imported_files[i]));
files.add(imported_files[i]);
}
}
When I run this I get an error at the following line:
String[] imported_files = (String[])in.readObject();
It says that the casting to String[] is not possible. I tried the same thing with int[] in another project and there it worked fine. :ehh: a bit strange.
I only get this error when i am trying to read my instance from a file. Writing to the file works without errors. (Allthough I can not be sure it works because I have never been able to load it again).
Why do I get this error and how can I fix it?
Thank you in advance ;)
-
I wrote a temporary solution ... this converts the array to a string. (a pretty lame answer. I do not like avoiding problems. If one always avoid problems, then the only thing one will learn is "how to avoid problems".)
Code:
private void writeObject(java.io.ObjectOutputStream out)
throws IOException{
String files2 = "";
for (int x=0;x<files.size();x++){
files2 += files.get(x) + "\n";
}
out.writeObject(files2);
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
String filestr = (String)in.readObject();
int b=0; //b = begin
aList = new ArrayList();
files = new ArrayList();
for(int e=0;e<filestr.length();e++){ //e = end
if(filestr.charAt(e) == '\n'){
String file = filestr.substring(b,e);
b = e+1;
addFile(file);
}
}
}
-
Im pretty sure you get the error because you are writing an ArrayList then trying to read it back and cast it to a String[]. Instead of trying to reinvent the wheel why dont you just use the toArray() method from the Collections interface to convert your ArryList to a String[]?
String[] f = (String[]) files.toArray(new String[0]);
-
I see ! that is a very stupid mistake indeed. I just forgot I was using an ArrayList instead of a String[]. Oh well.
Problem solved at least.
Thank you for your response.
PS:
"Instead of trying to reinvent the wheel why dont you just use the toArray() ..." should be "It is impossible to typecast an ArrayList to a String[]. Instead, you can use the toArray() to solve your problem." But let us not discuss about cultural differences such as the importance-ratio content/words.
Thank you ;)
-
Funny. I actually just read your response on my way to editing my post. I thought you might take it as if i was annoyed or i was being condesending, both of which i was not. My apologies. If i may ask. What is go? :confused:
-
No hard feelings at all ;).
---
Go ... lol, thanks for asking :)
Go is an asian boardgame. 2 players play eachother on a 19x19 grid, placing black and white stones on the board. with those stones you need to surround as much empty intersections as possible. It also involves capturing stones, but that is not really the main goal of the game.
Some people like to compare it to chess. The main differences are. Go you can learn in 5 minutes. But to learn chess you need to spend at least half an hour. Chess has a 8x8 board and only has about 100 possible moves at most. While go has a 19x19 board which leaves you with about 361 possible moves. Therefore go is a harder mindgame than chess. Also when playing chess you only use 1 side of your brains, but to play Go you need to use both parts. This is because in Go a lot of moves are played on feeling because it is impossible to read out all possible moves. Also Go is is older (about 5000 years old) and it is more popular (...on worldscale).
And ofcourse Go is also my hobby ;) I have been playing it for about 1,5 year now, a real addiction. (... and like you have noticed now I could talk about it for hours and hours, but I am not going to annoy you with that :))
Thanks again for your help with my java. See you in other posts probably ;) Take care.
-
Sounds like a fun game. Ill have to see what medium it is played on. ie Board or online. Probably both. The only game i really play that requires thinking is Scrabble everything else is X Box GTA Vice City. :D
-
hehe :)
I play pc games too now and then. But recently not having that much time to do so.
Go can be played online and offline indeed. :)
http://kgs.kiseido.com is a nice server to play go online
http://dragongoserver.net is also nice but has huge time limits like 50 days/game. :bigyello:
there are also some computer programs that play go. Like Igowin.
Who knows maybe I see you around on some server sooner or later :) :afrog: