我创建的课程ArrayLinearList<T>
与ArrayList
类似,但我无法在其中实现Serializable
,所以我创建了其他class MyArray<T> extends ArrayLinearList<T> implements Serializable
,但是当我尝试保存时并且读取,我的程序读取没有元素的数组
import java.io.Serializable;
import unal.datastructures.*;
public class MyArray <T> extends ArrayLinearList<T> implements Serializable {
private static final long serialVersionUID = -4835247906545846769L;
}
我尝试使用此方法保存并加载
public static void writeObject(Object o, String file){
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(o);
oos.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static Object readObject(String file){
Object ans = null;
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ans = ois.readObject();
ois.close();
}catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
return ans;
}
public static void main(String[] args) {
MyArray<Integer> a = new MyArray<Integer>();
for(int i=0 ; i<100 ; i++)
a.add(i, i);
System.out.println(a);
writeObject(a,"Hola.dat");
System.out.println("Readed array"+readObject("Hola.dat"));
}
它是输出
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, 23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72, 73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97, 98,99]
Readed array []