我有一个类型为:
的.csv文件Event Participant
ConferenceA John
ConferenceA Joe
ConferenceA Mary
ConferenceB John
ConferenceB Ted
ConferenceC Jessica
我想创建一个以下格式的2D布尔矩阵:
Event John Joe Mary Ted Jessica
ConferenceA 1 1 1 0 0
ConferenceB 1 0 0 1 0
ConferenceC 0 0 0 0 1
我首先阅读csv并使用它初始化类型为
的ArrayListAttendaceRecord(String title, String employee)
如何迭代这个ArrayList来创建一个像Java上面那样的布尔矩阵?
答案 0 :(得分:1)
基本上,您需要首先搜索输入字符串以查找每个名称(String.contains)并设置每个字段名称的布尔数组。
然后你将创建一个这些布尔数组的数组(或列表,无论如何)。
然后你只需对它们进行排序,寻找T / F并打印相应的信息。
我包含了一些非常粗略的伪代码,假设我正确理解你的问题。
// For first row
List labelStrings[];
labelStrings = {"Event", "John", "Joe", "Mary", "Ted", "Jessica"};
// For the matrix data
// List to iterate horizontally EDIT: Made boolean!
List<Boolean> strList= new ArrayList()<List>;
// List to iterate vertically
List<List> = listList new ArrayList()<List>;
/* for all the entries in AttendanceRecord (watch your spelling, OP)
for all data sets mapping title to employee
add the row data to strList[entry_num] */
for (int i = 0; i < listList.size()-1; i++)
for (int j = 0; j < labelStrings.size()-1; j++)
{
if (i == 0)
System.out.println(strList[j] + "\t\n\n");
else
{
// print listLists[i][j]
}
// iterate row by row (for each horizontal entry in the column of entries)
}
对不起,我现在只是阅读评论。
您肯定希望以易于迭代的方式安排数据。由于你有一个固定的表大小,你可以为每个条目硬编码一个布尔数组,然后在验证时打印它们被映射到事件,如输入字符串所示。
答案 1 :(得分:1)
尝试创建包含
的哈希映射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
}
}
答案 2 :(得分:1)
这是我能想到的最简单的方法。这个答案当然可以以完全不同的方式改进或完成。我采用这种方法是因为你提到你并不完全熟悉Map
(我也猜测Set
)。无论如何,让我们潜入。
在AttendanceRecord
课程中,您需要以下实例变量:两个LinkedHashSet
和一个LinkedHashMap
。 LinkedHashSet
#1将存储所有会议,LinkedHashSet
#2将存储所有参与者。 LinkedHashMap
会将会议存储为keys
,参与者列出为values
。其原因将在一分钟内明确。我首先要解释为什么你需要LinkedHashSet
。
LinkedHashSet的目的
请注意,在您的二维数组中,行(会议)和列(参与者)按读取顺序排列。不仅如此,从文件中读取的所有重复项都消失了。为了保持排序并消除重复,LinkedHashSet
完全符合此目的。然后,我们将在2d数组的行位置和列位置之间建立一对一的关系,并通过它们的数组表示形成每个LinkedHashSet
。我们使用Jhon
中的ConferenceA
作为示例。 Jhon
将位于参与者Set
的数组表示中的位置0,ConferenceA
将位于会议Set
的数组表示中的位置0。不仅如此,每个数组的大小将用于确定2d数组的大小(2darray [conferenceArrayLength] [participantArrayLength])
LinkedHashMap的目的
我们需要LinkedHashMap
来保留元素的排序(因此Linked
)。元素将像这样存储在内部。
ConferenceA :Jhon Joe Mary
ConferenceB :Jhon Ted
ConferenceC :Jessica
然后我们将遍历数据结构并将每个key
value
对发送到一个函数,该函数返回从每个LinkedHashSet
返回的每个数组中每个元素的位置。返回每个行和列位置时,我们将在2d数组中向该位置添加1。
注意:我为我的示例使用了一个Integer数组,根据需要替换。
AttendanceRecord.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);
}
}
}
<强> Test.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;
}
}