这是我能为你想到的最简单的方法。这个答案当然可以改进或以完全不同的方式完成。我采用这种方法是因为您提到您并不完全熟悉Map
(我也在猜测Set
)。无论如何,让我们潜入。
在您的AttendanceRecord
课程中,您将需要以下实例变量:二LinkedHashSet
和一LinkedHashMap
。LinkedHashSet
#1 将存储所有会议,LinkedHashSet
#2 将存储所有参与者。会将会议LinkedHashMap
存储为keys
并将参与者列表存储为values
。原因将在一分钟内清楚。我将首先解释为什么你需要LinkedHashSet
.
LinkedHashSet 的用途
请注意,在您的二维数组中,行(会议)和列(参与者)按读取顺序排列。不仅如此,从文件中读取的所有重复项都消失了。保留顺序并消除重复非常LinkedHashSet
适合此目的。然后,我们将在二维数组的行位置和列位置之间建立一对一的关系,并LinkedHashSet
通过它们的数组表示。我们以Jhon
fromConferenceA
为例。Jhon
将位于参与者数组表示中的位置 0,Set
并且ConferenceA
将位于会议数组表示中的位置 0 Set
。不仅如此,每个数组的大小将用于确定您的二维数组的大小(2darray[conferenceArrayLength][participantArrayLength])
LinkedHashMap 的用途
我们需要LinkedHashMap
保持元素的顺序(因此Linked
)。元素将像这样在内部存储。
ConferenceA :Jhon Joe Mary
ConferenceB :Jhon Ted
ConferenceC :Jessica
然后,我们将遍历数据结构并将每一key
value
对发送到一个函数,该函数返回从 each 返回的每个数组中每个元素的位置LinkedHashSet
。当返回每一行和每一列的位置时,我们将在二维数组中的该位置添加一个 1。
注意:我在示例中使用了整数数组,根据需要替换。
考勤记录.java
public class AttendanceRecord {
private Map<String, ArrayList> attendanceRecordMap = new LinkedHashMap<String, ArrayList>();
private Set<String> participants = new LinkedHashSet<String>();
private Set<String> conferences = new LinkedHashSet<String>();
public AttendanceRecord() {
}
public Map<String, ArrayList> getAttendanceRecordMap() {
return attendanceRecordMap;
}
public Object[] getParticipantsArray() {
return participants.toArray();
}
public Object[] getConferencesArray() {
return conferences.toArray();
}
public void addToRecord(String title, String employee) {
conferences.add(title);
participants.add(employee);
if (attendanceRecordMap.containsKey(title)) {
ArrayList<String> tempList = attendanceRecordMap.get(title);
tempList.add(employee);
} else {
ArrayList<String> attendees = new ArrayList<String>();
attendees.add(employee);
attendanceRecordMap.put(title, attendees);
}
}
}
测试.java
public class Test {
public static void main(String[] args) {
AttendanceRecord attendanceRecord = new AttendanceRecord();
//There are hardcoded. You will have to substitute with your code
//when you read the file
attendanceRecord.addToRecord("ConferenceA", "Jhon");
attendanceRecord.addToRecord("ConferenceA", "Joe");
attendanceRecord.addToRecord("ConferenceA", "Mary");
attendanceRecord.addToRecord("ConferenceB", "Jhon");
attendanceRecord.addToRecord("ConferenceB", "Ted");
attendanceRecord.addToRecord("ConferenceC", "Jessica");
int[][] jaccardArray = new int[attendanceRecord.getConferencesArray().length][attendanceRecord.getParticipantsArray().length];
setUp2dArray(jaccardArray, attendanceRecord);
print2dArray(jaccardArray);
}
public static void setUp2dArray(int[][] jaccardArray, AttendanceRecord record) {
Map<String, ArrayList> recordMap = record.getAttendanceRecordMap();
for (String key : recordMap.keySet()) {
ArrayList<String> attendees = recordMap.get(key);
for (String attendee : attendees) {
int row = findConferencePosition(key, record.getConferencesArray());
int column = findParticipantPosition(attendee, record.getParticipantsArray());
System.out.println("Row inside " + row + "Col inside " + column);
jaccardArray[row][column] = 1;
}
}
}
public static void print2dArray(int[][] jaccardArray) {
for (int i = 0; i < jaccardArray.length; i++) {
for (int j = 0; j < jaccardArray[i].length; j++) {
System.out.print(jaccardArray[i][j]);
}
System.out.println();
}
}
public static int findParticipantPosition(String employee, Object[] participantArray) {
int position = -1;
for (int i = 0; i < participantArray.length; i++) {
if (employee.equals(participantArray[i].toString())) {
position = i;
break;
}
}
return position;
}
public static int findConferencePosition(String employee, Object[] conferenceArray) {
int position = -1;
for (int i = 0; i < conferenceArray.length; i++) {
if (employee.equals(conferenceArray[i])) {
position = i;
break;
}
}
return position;
}
}