I've made a class for fun, but it runs out of heap space really quickly?

This is my second semester at college and we've learned about linkedLists (Nodes). I had an idea to do something like that for fun in java which is a Room class with 4 pointers to other Room objects: north, south, west, east, and every Room object also contains a char object, so I can keep track of it.

My main function asks for w/a/s/d input from scanner and then creates/points to the corresponding room, and fills each room with a char.

However, for some reason, it runs out of heap space really quickly (like around when the chars reach '?').

Here's my code:

import java.util.*;

public class Room {
    // instance variables
    private Room north, west, east, south;
    private char object;
    private static char counter = ' ';

    // constructors
    public Room() {
    }

    public Room(char object) {
        this.object = object;
    }

    // methods
    public Room newRoomNorth() {
        north = new Room();
        north.south = this;
        return north;
    }

    public Room newRoomWest() {
        west = new Room();
        west.east = this;
        return west;
    }

    public Room newRoomEast() {
        east = new Room();
        east.west = this;
        return east;
    }

    public Room newRoomSouth() {
        south = new Room();
        south.north = this;
        return south;
    }

    public Room linkRoomNorth(Room linkedRoom) { // link a given room to given direction of this room, returns what room was overwritten (if any)
        Room overwritten = north;
        north = linkedRoom;
        north.south = this;
        return overwritten;
    }

    public Room linkRoomWest(Room linkedRoom) {
        Room overwritten = west;
        west = linkedRoom;
        west.east = this;
        return overwritten;
    }

    public Room linkRoomEast(Room linkedRoom) {
        Room overwritten = east;
        east = linkedRoom;
        east.west = this;
        return overwritten;
    }

    public Room linkRoomSouth(Room linkedRoom) {
        Room overwritten = south;
        south = linkedRoom;
        south.north = this;
        return overwritten;
    }

    public Room getRoomNorth() {
        return this.north;
    }

    public Room getRoomWest() {
        return this.west;
    }

    public Room getRoomEast() {
        return this.east;
    }

    public Room getRoomSouth() {
        return this.south;
    }

    public char getObject() {
        return this.object;
    }

    public void setObject(char object) {
        this.object = object;
    }

    public void fill() { // puts a character as object
        this.setObject(counter);
        counter = (char) (counter + 1);
    }

    // main
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = "";
        Room currentRoom = new Room('™');
        while (!input.equalsIgnoreCase("exit")) {
            System.out.println("This room's treasure is: " + currentRoom.getObject());
            System.out.println("Which way? (w/a/s/d)");
            input = scan.next();
            switch (input.charAt(0)) {
                case 'w':
                    if (currentRoom.getRoomNorth() == null) {
                        currentRoom = currentRoom.newRoomNorth();
                        currentRoom.fill();
                    } else {
                        currentRoom = currentRoom.getRoomNorth();
                    }
                    break;
                case 'a':
                    if (currentRoom.getRoomWest() == null) {
                        currentRoom = currentRoom.newRoomWest();
                        currentRoom.fill();
                    } else {
                        currentRoom = currentRoom.getRoomWest();
                    }
                    break;
                case 'd':
                    if (currentRoom.getRoomEast() == null) {
                        currentRoom = currentRoom.newRoomEast();
                        currentRoom.fill();
                    } else {
                        currentRoom = currentRoom.getRoomEast();
                    }
                    break;
                case 's':
                    if (currentRoom.getRoomSouth() == null) {
                        currentRoom = currentRoom.newRoomSouth();
                        currentRoom.fill();
                    } else {
                        currentRoom = currentRoom.getRoomSouth();
                    }
                    break;
            }
        }
    }
} 

I'm not getting any issues with heap space, but after reaching '?' all the new rooms still have '?' , most likely because (char) ('?' + 1) will keep returning '?' . Perhaps this is a bug with your version of Java. What is your OS and JDK version?

链接地址: http://www.djcxy.com/p/32030.html

上一篇: 跟踪Java冒险游戏中的房间?

下一篇: 我为了好玩而创造了一堂课,但它真的很快用完了堆空间?