Search code examples
javaarrayslimit

constructor exceeding the 65535 bytes limit


I have a class where I store the terrain of the world of my game. The gameworld consists of cubes and every cube has a terrain. The information is stored as a pretty big 3D-Array of integers.

The problem that I now encounter is that my 'map' is too big to store in a class (or enum). Since I don't really know anything outside of classes and enums, I don't know what to do with this.

Should i make a tekst file out of it and read that out? If so, how do I do that?

A different solution would be to split the map over several classes or enums and then merge them again at runtime, but I don't know how to do that either and it seems like a pretty bad solution.

EDIT: This is part of the worldmap:

 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

There are 40 of of these blocks (so 38 more) and each one represents the world at a certain z-coordinate. I also have this in arrayformat. It would be great if you could provide some (sample) code on how to read this from a file.


Solution

  • It's much better to put it into resource. Just create a text file next to your class (e.g. named map.txt), put there your world map in the same format you've used in the question, then load it using the code like this:

    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);
        }
    }