【发布时间】:2015-07-16 12:51:42
【问题描述】:
我有 4 个班级 Township、Society、Flat 和 Owner。如果我输入所有者名称,我将如何从Owner 类中获取所有数据?
public class TestSociety {
public static void main(String[] args) {
HashMap<Integer, TownShip> township = initializeTownShip();
Scanner scanner = new Scanner(System.in);
System.out.println("enter name of owner");
String name = scanner.nextLine();
}
private static HashMap<Integer, TownShip> initializeTownShip() {
// putting township into the hashmap
HashMap<Integer, TownShip> township = new HashMap<Integer, TownShip>();
for (int tid = 1; tid < 5; tid++) {
township.put(tid, new TownShip("topaz", getListOfSociety(tid)));
}
return township;
}
private static List<Society> getListOfSociety(int tid) {
// putting societies into the list
List<Society> listOfSociety = new ArrayList<Society>();
for (int societyId = 1; societyId < 5; societyId++) {
listOfSociety.add(new Society(societyId,
"society name" + societyId, getListofFlat(societyId)));
}
return listOfSociety;
}
private static List<Flat> getListofFlat(int societyId) {
// putting flats into the list
List<Flat> listOfFlat = new ArrayList<Flat>();
for (int flatId = 1; flatId <= 10; flatId++) {
listOfFlat.add(new Flat(flatId, flatId + 1, new Owner("firstname"
+ flatId, "lastname" + flatId, flatId * 1234)));
}
return listOfFlat;
}
}
【问题讨论】:
-
请发布相关代码(您的课程)。
-
您也应该发布您的
Township和Flat课程。 -
也发布
Owner。Owner包含您想要获取的哪些数据?Owners 是如何填充/初始化的?对于初学者,您可能希望实现一种方法来检索Owner的实例以“获取所有数据”(注意:根据您的用例,您可能需要辅助工具来选择Owner如果重名是可能的)。 -
谢谢你们,抱歉代码不完整
标签: java arraylist collections hashmap