Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从表数据在Java中创建二维布尔数组_Java_Data Structures_Boolean_Multidimensional Array_Indicator - Fatal编程技术网

从表数据在Java中创建二维布尔数组

从表数据在Java中创建二维布尔数组,java,data-structures,boolean,multidimensional-array,indicator,Java,Data Structures,Boolean,Multidimensional Array,Indicator,我有一个.csv文件类型: Event Participant ConferenceA John ConferenceA Joe ConferenceA Mary ConferenceB John ConferenceB Ted ConferenceC Jess

我有一个.csv文件类型:

Event                     Participant  
ConferenceA               John   
ConferenceA               Joe  
ConferenceA               Mary    
ConferenceB               John  
ConferenceB               Ted  
ConferenceC               Jessica  
我想创建以下格式的二维布尔矩阵:

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并使用它初始化类型为的ArrayList:

AttendaceRecord(String title, String employee)

我如何迭代这个ArrayList来创建一个类似上面Java中的布尔矩阵

基本上,您需要从搜索输入字符串开始,找到每个名称(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)
   }
//用于第一行
列出标签串[];
标签字符串={“事件”、“约翰”、“乔”、“玛丽”、“特德”、“杰西卡”};
//对于矩阵数据
//要水平迭代编辑的列表:设为布尔值!
List strList=new ArrayList();
//要垂直迭代的列表
List=listList new ArrayList();
/*对于AttendanceRecord中的所有条目(注意拼写,OP)
用于将标题映射到员工的所有数据集
将行数据添加到strList[entry_num]*/
对于(int i=0;i
对不起,我正在浏览评论


您肯定希望以易于迭代的方式排列数据。因为您有固定的表大小,所以可以为每个条目硬编码一个布尔数组,然后在验证时打印它们映射到输入字符串中指示的事件。

尝试创建一个包含

HashMap map = new HashMap<conferenceStr, HashMap<nameStr, int>>()
当然,您需要一些初始化逻辑,比如您可以检查innerMap.get(nameStr)是否存在,如果不存在,则迭代每个innerMap和innerMap.put(nameStr,0)

该结构可用于生成最终的二维布尔矩阵

精化编辑:

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
ArrayList attendanceList=new ArrayList();
//使用csv中的信息填充列表(您暗示可以这样做)
HashMap=newHashMap();
//映射到存储每个参与者,但这似乎效率低下
HashMap>participantMap=新建HashMap();
for(AttendanceRecord记录:attendanceList){
String title=record.getTitle();
字符串employee=record.getEmployee();
participantMap.put(员工,0);
HashMap innerMap=map.get(标题);
if(innerMap==null){
innerMap=新的HashMap();
}
innerMap.put(员工,1);
}
//现在我们有了我们需要的所有数据,这只是关于如何格式化它
例如,如果您只想打印这样一个表,您可以迭代映射的每个元素,这样做:

for (HashMap<String, Integer> innerMap : map.values()) {
  for (String employee : participantMap.values()) {

    if (innerMap.get(employee)) {
      //print 1
    }
    else 
      //print 0
  }
}
for(HashMap-innerMap:map.values()){
for(字符串employee:participantMap.values()){
if(innerMap.get(employee)){
//打印1
}
其他的
//打印0
}
}

这是我能想到的最简单的方法。这个答案当然可以改进或以完全不同的方式完成。我采用这种方法是因为您提到您不完全熟悉
Map
(我也在猜测
Set
)。不管怎样,我们开始吧

attendencecord
类中,需要以下实例变量:两个
LinkedHashSet
和一个
LinkedHashMap
LinkedHashSet
#1将存储所有会议,
LinkedHashSet
#2将存储所有参与者。
LinkedHashMap
将会议存储为
,参与者列表存储为
。原因很快就会弄清楚。我将首先解释为什么需要
LinkedHashSet

LinkedHashSet的目的

请注意,在二维数组中,行(会议)和列(参与者)是按读取顺序排列的。不仅如此,从文件中读取的所有副本都消失了。为了保留顺序并消除重复项,
LinkedHashSet
非常适合此目的。然后,我们将在2d数组和每个
LinkedHashSet
的行位置和列位置之间通过它们的数组表示建立一对一的关系。例如,让我们使用
ConferenceA
中的
Jhon
Jhon
将在参与者
Set
的数组表示中位于位置0,
ConferenceA
将在会议
Set
的数组表示中位于位置0。不仅如此,每个数组的大小还将用于确定2d数组的大小(2darray[conferenceArrayLength][participantArrayLength])

LinkedHashMap的目的

我们需要
LinkedHashMap
来保持元素的顺序(因此
Linked
)。元素将像这样存储在内部

ConferenceA :Jhon Joe Mary 
ConferenceB :Jhon Ted 
ConferenceC :Jessica 
然后,我们将遍历数据结构,并将每个
对发送到一个函数,该函数从每个
LinkedHashSet
返回的每个数组中返回每个元素的位置。当返回每个行和列位置时,我们将在2d数组中的该位置添加1

注意:我在示例中使用了整数数组,根据需要替换

attendencecord.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);
        }
    }
} 
公共课堂考勤记录{
私有映射AttendanceCordMap=新建LinkedHashMap();
私有集参与者=新LinkedHashSet();
private Set conferences=新建LinkedHashSet();
公众出席人数{
}
公共地图getAttendanceCordMap(){
返回坐标图;
}
公共对象[]
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);
        }
    }
} 
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;
    }
}