【发布时间】:2016-06-25 17:12:22
【问题描述】:
我有一个问题......
这里我们得到一个二维字节数组:
byte[][] duengonMap = new byte[500][500];
因为我想将它从客户端发送到服务器或 反过来,我需要将它放入 int/long 中。它将从服务器发送到其他连接的客户端,然后它将转换回二维数组。听起来很简单……但是我该怎么做呢?
我试过类似的东西:
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);
}
}
}
它只是被转换成一个“文件”,也许我做错了, 但我不知道如何将其转换为 int 或 long ... 有任何想法吗 ?还是例子?
【问题讨论】:
-
为什么您认为需要转换为 int?
byte[][]是一个对象;所以你可以简单地发送它;使用writeObject()。还是我在这里遗漏了什么? -
是的,因为我使用的是 Kryonet,没有提到这一点。而且你不能通过 udp 或 tcp 发送字节 [] [] 或大对象,而不是服务器将崩溃:/
-
没有人解决?
标签: java arrays serialization libgdx server