超过 65535 字节限制的构造函数

constructor exceeding the 65535 bytes limit

我有一个 class 存储游戏世界地形的地方。游戏世界由立方体组成,每个立方体都有一个地形。信息存储为一个相当大的整数 3D 数组。

我现在遇到的问题是我的 'map' 太大而无法存储在 class(或枚举)中。因为我真的不知道 classes 和枚举之外的任何东西,所以我不知道该怎么做。

我应该用它制作一个 tekst 文件并读出来吗?如果是这样,我该怎么做?

一个不同的解决方案是将地图拆分为多个 classes 或枚举,然后在运行时再次合并它们,但我也不知道该怎么做,这看起来很糟糕解决方案。

编辑:这是世界地图的一部分:

 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 2 2 0 0 0 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 2 2 0 0 0 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 2 2 2 2 1 1 1 1 1 1 1
 0 0 0 0 1 0 0 0 0 2 2 2 2 1 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1
 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 0 0
 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0
 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0
 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

这些方块有 40 个(所以还有 38 个),每个方块都代表某个 z 坐标上的世界。我也有这个数组格式。 如果您能提供一些(示例)代码来说明如何从文件中读取它,那就太好了。

你能展示你的错误吗?

如果您不需要加载所有地图,您可以将地图分成块,并将所有地图保存在 Xml 中,例如,如下所示:

<map>
   <chunk x=0 y=0> 
      <!--chunk informations>
   </chunk>
   <chunk x=0 y=1> 
      <!--chunk informations>
   </chunk>
   <!--other chunks>
</map>

如果在您的用例中可能,您可以使用 byte[] 块,甚至 short[]。那么你应该在 65535 字节限制内。

size för byte[] : 20 x 20 x 40 x 1 = 16000 bytes
size för short[]: 20 x 20 x 40 x 2 = 32000 bytes

放到资源里就好多了。只需在您的 class 旁边创建一个文本文件(例如命名为 map.txt),将您的世界地图以您在问题中使用的相同格式放在那里,然后使用如下代码加载它:

public int[][][] loadMap() {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass()
            .getResourceAsStream("map.txt"), StandardCharsets.ISO_8859_1))) {

        List<int[][]> result = new ArrayList<>();
        List<int[]> cur = new ArrayList<>();
        String line;
        while((line = reader.readLine()) != null) {
            line = line.trim();
            if(line.isEmpty() && !cur.isEmpty()) {
                result.add(cur.toArray(new int[0][]));
                cur.clear();
            } else {
                String[] cells = line.split("\s+");
                int[] row = new int[cells.length];
                for(int i=0; i<cells.length; i++) row[i] = Integer.parseInt(cells[i]);
                cur.add(row);
            }
        }
        if(!cur.isEmpty())
            result.add(cur.toArray(new int[0][]));
        return result.toArray(new int[0][][]);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}