嘗試創建包含
HashMap map = new HashMap<conferenceStr, HashMap<nameStr, int>>()
哈希地圖,讓您輕鬆的ArrayList迭代,你可以這樣做
innerMap = map.get(conferenceStr)
innerMap.put(nameStr, 1)
當然你需要一些初始化的邏輯,就像你可以檢查如果innerMap.get(nameStr)存在,如果不存在,遍歷每個內部映射和innerMap.put(nameStr,0)
此結構可用於生成最終的2D布爾可以矩陣。
擬訂編輯:
ArrayList<AttendanceRecord> attendanceList = new ArrayList<AttendanceRecord>();
// populate list with info from the csv (you implied you can do this)
HashMap<String, HashMap<String, Integer>> map = new HashMap<String, HashMap<String, Integer>>();
//map to store every participant, this seems inefficient though
HashMap<String, Integer>> participantMap = new HashMap<String, Integer>();
for (AttendanceRecord record : attendanceList) {
String title = record.getTitle();
String employee = record.getEmployee();
participantMap.put(employee, 0);
HashMap<String, Integer> innerMap = map.get(title);
if (innerMap == null) {
innerMap = new HashMap<String, Integer>();
}
innerMap.put(employee, 1);
}
//now we have all the data we need, it's just about how you want to format it
例如,如果你想只打印出一張類似的表格,你可以通過地圖中的每個元素重複這樣做:
for (HashMap<String, Integer> innerMap : map.values()) {
for (String employee : participantMap.values()) {
if (innerMap.get(employee)) {
//print 1
}
else
//print 0
}
}
這個CSV可以有任意數量的會議和/或參與者的? – Andy
將此插入到ArrayList中時,您是否在維護其他變量,例如您擁有多少個不同的員工或事件? –
爲什麼不使用「Map」?關鍵是會議,價值將成爲參與者?它是否必須是一個'ArrayList'?這對你來說會容易得多。 – Andy