我的班级中有一个名为 DataStorage 的哈希图:
HashMap<String, Integer> people = new HashMap<String, Integer>();
people.put("bob", 2);
people.put("susan", 5);
如何在不同的类中访问此 HashMap 中的数据?
创建 HashMap 作为实例变量,并提供一个方法将其访问到类 API 中:
public class DataStorage {
private HashMap<String, Integer> people = new HashMap<String, Integer>();
public HashMap<String, Integer> getPeopleMap() {
return people;
}
}
public class AnotherClass {
DataStorage x = new DataStorage();
private void someMethod() {
HashMap<String, Integer> people = x.getPeopleMap();
//work with your map here...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)