多维数组的Java序列化
是否可以在java中序列化制作2D数组?
如果没有,我希望将3×3 2Darrays“转换”为矢量矢量。
我一直在玩矢量,我仍然不确定如何表示。 谁能帮我?
谢谢!
Java中的数组是可序列化的 – 因此数组数组也是可序列化的。
但是,它们包含的对象可能不是,因此请检查数组的内容是否可序列化 – 如果不是,请将其设置为可序列化。
这是一个使用整数数组的例子。
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
- 为什么要将Class 的类转换为不安全?
- Web应用程序中的Spring线程
- 使用Spring框架的抽象工厂
- Guava EventBus中的注释与接口
- 我该如何防止此exception? java.sql.SQLException:无法转换为内部表示:
- 返回一个列表,我已经有了一个rowmapper实现
- java.lang.NoSuchMethodError:org.slf4j.spi.LocationAwareLogger.log(Lorg / slf4j / Marker; Ljava / lang / String; ILjava / lang / String; [Ljava / lang / Obje
- 从文本文件中读取数据并创建对象
- 在Java中没有转换的两到长