多维数组的Java序列化 [英] Java serialization of multidimensional array
本文介绍了多维数组的Java序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
时有可能使Java中的二维数组序列化?
Is it possible to make a 2D array in java serializable?
如果不是,我期待翻译一个3x3的二维数组到向量的矢量。
If not, i am looking to "translate" a 3x3 2D array into a Vector of Vectors.
我一直与载体玩弄,我仍然不确定如何重新present这一点。谁能帮我?
I have been playing around with vectors, and I am still unsure of how to represent that. Can anyone help me?
谢谢!
推荐答案
在Java中数组是序列化 - 因此数组的数组序列化太
Arrays in Java are serializable - thus Arrays of Arrays are serializable too.
它们包含可能不是,虽然如此,检查数组的内容是可序列化的对象 - 如果没有,做起来很
The objects they contain may not be, though, so check that the array's content is serializable - if not, make it so.
下面是一个例子,使用int数组。
Here's an example, using arrays of ints.
public static void main(String[] args) {
int[][] twoD = new int[][] { new int[] { 1, 2 },
new int[] { 3, 4 } };
int[][] newTwoD = null; // will deserialize to this
System.out.println("Before serialization");
for (int[] arr : twoD) {
for (int val : arr) {
System.out.println(val);
}
}
try {
FileOutputStream fos = new FileOutputStream("test.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(twoD);
FileInputStream fis = new FileInputStream("test.dat");
ObjectInputStream iis = new ObjectInputStream(fis);
newTwoD = (int[][]) iis.readObject();
} catch (Exception e) {
}
System.out.println("After serialization");
for (int[] arr : newTwoD) {
for (int val : arr) {
System.out.println(val);
}
}
}
输出:
Before serialization
1
2
3
4
After serialization
1
2
3
4
这篇关于多维数组的Java序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文