作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想做的是使用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/
安装并修复我的 VS2015 实例后,我仍然无法让智能感知(服务器端)在我的 MVC View 中工作。当我在 session 中第一次打开 .cshtml 文件并找到 Activitylog 文件时
我是一名优秀的程序员,十分优秀!