gpt4 book ai didi

java - 为什么我不断往 hashMap 中放入东西后,它一直说 hashMap 的大小为零?

转载 作者:行者123 更新时间:2023-12-01 13:34:05 26 4
gpt4 key购买 nike

我想做的是使用addRoom()将房间添加到 HashMap 中(我不想重复)。然后,我使用 getRoom(String)getRooms() 将它们传递给 Controller ​​。

问题是,正如您在我的多个 System.out.prints 中看到的那样,无论我运行 addRoom() 多少次,大小都保持在 0 >。

我做错了什么还是程序中的其他地方有问题?

package calendar;

import java.util.HashMap;
import java.util.Map;

public class Building {

String buildingName = null;

private Map<String, Classroom> rooms =
new HashMap<String, Classroom>();

public Building() {
}
public Building(String name){
buildingName=name;
}

/**
* @return the rooms
*/
public Map<String, Classroom> getRooms() {
System.out.println("room size from building.java:" +
rooms.size());
return rooms;
}

public Classroom getRoom(String roomName) {
if (rooms.containsKey(roomName)) {
return rooms.get(roomName);
}
return new Classroom("null");
}

/**
* @param rooms the rooms to set
*/
public void setRooms(Map<String, Classroom> rooms) {
this.rooms = rooms;
}

public void addRoom(String roomNumber) {
System.out.println("Room added: "+roomNumber +" to "+
buildingName+ " size:"+rooms.size());
rooms.put(roomNumber, new Classroom(roomNumber));
}
}

我从解析器调用此方法,因此代码有点冗长,但我会减少它并尝试仅粘贴相关部分:

private Map<String, Building> currentBuilding = new HashMap<String, Building>();

现在这是一个 for 循环,它遍历需要解析的所有数据 block : currentBuilding.put(建筑物, 新建筑物(建筑物)); currentBuilding.get(building).addRoom(room);

其中建筑物和房间是字符串。建筑物按其应有的方式启动。显然我希望每个建筑物的房间都在其相应的建筑物中,但这似乎并没有按预期发生。

最佳答案

你的类(class)运行得很好!刚刚做了一些修复

import java.util.HashMap;
import java.util.Map;

public class Building {

String buildingName = null;
private static Map<String, Classroom> rooms =
new HashMap<String, Classroom>();

public Building() {
}

public Building(String name) {
buildingName = name;
}

/**
* @return the rooms
*/
public Map<String, Classroom> getRooms() {
System.out.println("room size from building.java:"
+ rooms.size());
return rooms;
}

public Classroom getRoom(String roomName) {
if (rooms.containsKey(roomName)) {
return rooms.get(roomName);
}
return new Classroom("null");
}

/**
* @param rooms the rooms to set
*/
public void setRooms(Map<String, Classroom> rooms) {
this.rooms = rooms; // did you use this method to initialize map?
}

public void addRoom(String roomNumber) {
rooms.put(roomNumber, new Classroom(roomNumber));
System.out.println("Room added: " + roomNumber + " to "
+ buildingName + " size:" + rooms.size()); // print info after action
}
}

和用法

    public static void main(String[] args) {
Building building = new Building("first building");
building.addRoom("1"); // add room 1
building.addRoom("2"); // add room 2
building.addRoom("3"); // add room 3
System.out.println(building.getRooms().size());
}

输出

Room added: 1 to first building size:1
Room added: 2 to first building size:2
Room added: 3 to first building size:3
room size from building.java:3
3

尽管如此,您的类按预期工作,问题可能出在类使用中,您如何处理对 Building 类的引用,可能您希望它是静态的?

关于java - 为什么我不断往 hashMap 中放入东西后,它一直说 hashMap 的大小为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com