java - 我为了好玩而创建了一个类,但是它很快就耗尽了堆空间?

标签 java linked-list heap-memory nodes

这是我在大学的第二个学期,我们学习了 linkedList(节点)。我有一个想法在 java 中做类似的事情,它是一个 Room 类,有 4 个指向其他 Room 对象的指针:北、南、西、东,每个 Room 对象还包含一个 char 对象,所以我可以跟踪它。

我的主要功能要求从扫描仪输入 w/a/s/d 输入,然后创建/指向相应的房间,并用一个字符填充每个房间。

但是,由于某种原因,它很快就会耗尽堆空间(例如当字符到达“?”时)。

这是我的代码:

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

最佳答案

我没有遇到任何堆空间问题,但在达到 '?' 后,所有新房间仍然有 '?',很可能是因为 (char) ('?' + 1) 将继续返回 '?'。也许这是您的 Java 版本的错误。您的操作系统和 JDK 版本是什么?

关于java - 我为了好玩而创建了一个类,但是它很快就耗尽了堆空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209866/

相关文章:

c - 从链表中正确删除元素?指针的内存错误

performance - 在 Rust 中用零值初始化结构体向量

java - 使用 ORMLite 更新对象及其关系

java - Jenkins 中的 Java Ant 任务构建失败

java - 如何使用 JPA 和 hibernate 映射 Java/Kotlin 字符串数组和 Postgres SQL 数组

java - 有没有办法将 Flux<String> 从 Spring REST 端点返回到 Web 浏览器/前端应用程序(即 Angular 6)?

创建队列列表

c - C语言中最长的单词有多少个字符

java - 我无法在以独立模式配置的 hadoop 上执行 map-reduce 作业

c++ - 当进程内存大小更大时,更多 TLB 未命中?