Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the easiest way I can think of for you. This answer can certainly be improved or done in a completely different way. I'm taking this approach because you mentioned that you are not completely familiar with <code>Map</code> (I'm also guessing with <code>Set</code>). Anyway let's dive in. </p> <p>In your <code>AttendanceRecord</code> class you are going to need the following instance variables: two <code>LinkedHashSet</code> and one <code>LinkedHashMap</code>. <code>LinkedHashSet</code> #1 will store all conferences and <code>LinkedHashSet</code> #2 will store all participants. The <code>LinkedHashMap</code> will store the the conferences as <code>keys</code> and participants <strong>list</strong> as <code>values</code>. The reason for this will be clear in a minute. I'll first explain why you need the <code>LinkedHashSet</code>. </p> <p><strong>Purpose of LinkedHashSet</strong></p> <p>Notice in your 2d array, the rows (conferences) and columns (participants) are arranged in the order they were read. Not only that, all duplicates read from the file are gone. To preserve the ordering and eliminate duplicates a <code>LinkedHashSet</code> fits this purpose perfectly. Then, we will have a one-to-one relationship between the row positions and the column positions of the 2d array and each <code>LinkedHashSet</code> via their array representation. Let's use <code>Jhon</code> from <code>ConferenceA</code> for example. <code>Jhon</code> will be at position 0 in the array representation of the participant <code>Set</code> and <code>ConferenceA</code> will be at position 0 in the array representation of the conference <code>Set</code>. Not only that, the size of each array will be used to determine the size of your 2d array (2darray[conferenceArrayLength][participantArrayLength])</p> <p><strong>Purpose of the LinkedHashMap</strong></p> <p>We need the <code>LinkedHashMap</code> to preserve the ordering of the elements (hence <code>Linked</code>). The elements will be stored internally like this. </p> <pre><code>ConferenceA :Jhon Joe Mary ConferenceB :Jhon Ted ConferenceC :Jessica </code></pre> <p>We will then iterate through the data structure and send each <code>key</code> <code>value</code> pair to a function which returns the position of each element from each array returned from each <code>LinkedHashSet</code>. As each row and column position is returned, we will add a 1 to that position in the 2d array. </p> <p><strong>Note:</strong> I used an Integer array for my example, substitute as needed. </p> <p><em><strong>AttendanceRecord.java</em></strong> </p> <pre><code>public class AttendanceRecord { private Map&lt;String, ArrayList&gt; attendanceRecordMap = new LinkedHashMap&lt;String, ArrayList&gt;(); private Set&lt;String&gt; participants = new LinkedHashSet&lt;String&gt;(); private Set&lt;String&gt; conferences = new LinkedHashSet&lt;String&gt;(); public AttendanceRecord() { } public Map&lt;String, ArrayList&gt; 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&lt;String&gt; tempList = attendanceRecordMap.get(title); tempList.add(employee); } else { ArrayList&lt;String&gt; attendees = new ArrayList&lt;String&gt;(); attendees.add(employee); attendanceRecordMap.put(title, attendees); } } } </code></pre> <p><strong><em>Test.java</em></strong></p> <pre><code>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&lt;String, ArrayList&gt; recordMap = record.getAttendanceRecordMap(); for (String key : recordMap.keySet()) { ArrayList&lt;String&gt; 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 &lt; jaccardArray.length; i++) { for (int j = 0; j &lt; 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 &lt; 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 &lt; conferenceArray.length; i++) { if (employee.equals(conferenceArray[i])) { position = i; break; } } return position; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload