java 如何对图及其关系进行编码(未加权图)

kupeojn6  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(118)

我被分配了一个算法分析小组项目,我们的任务是为一系列人创建一个推荐系统。我们得到了两个文件,一份人员名单及其信息,以及一份人员名单及其参与的活动。我们将根据谁被视为密切接触者以及此人是否愿意保护隐私来提出建议。密切接触者是指在同一个社区、学校或雇主。如果一个人要求隐私,那么不会向那个人发送任何建议。我已经将文件加载到一个类对象中,但在捕获后如何结构化数据方面遇到了障碍。我不知道如何编写图表,根据我对问题的理解和可视化,我相信图表是解决此问题的适当数据结构。我将附上我的代码,说明我如何捕获数据和以下文件的格式。
档案格式:
活动文件:

Winston, William, Bought Samsung Galaxy Watch 4 
Thomas, Williams, Watching Blackout on Netflix 
John, Warren, Bought Samsung Galaxy Watch 4
Tanisha, Thompson, Listening I'll be with You by Grace Thrillers
Owen, Mendez, Bought FitBit Charge 5

人员档案:

Rajay, Mccalla, 3151288155, RaMccalla38@gmail.com, Hope Flat, Pap High, eGov, Bronks, N

编码:

public class InfoReader {

    public void ReadInfo(){
        //find file with person data
        try {
            String fileLocation = File.separator + "Users" + File.separator + "user" + File.separator + "Downloads" + File.separator + "SamplefilePersons2022Oct31text.csv";

            File personList = new File(fileLocation);
            //scanner to read from file
            Scanner personScanner = new Scanner(personList);

            while (personScanner.hasNextLine()) {
                String nextline = personScanner.nextLine();
                // split file into parts
                String[] personComponents = nextline.split(",");
                //get components of Persons
                String firstname = personComponents[0];
                String lastname = personComponents[1];
                String phone = personComponents[2];
                String email = personComponents[3];
                String community = personComponents[4];
                String school = personComponents[5];
                String employer = personComponents[6];
                String privacy = personComponents[7];

                //creating new person
                Person newPerson = new Person();

                //set data attributes for persons as we read from file
                newPerson.setFirstname(firstname);
                newPerson.setLastname(lastname);
                newPerson.setPhone(phone);
                newPerson.setEmail(email);
                newPerson.setCommunity(community);
                newPerson.setSchool(school);
                newPerson.setEmployer(employer);
                newPerson.setPrivacy(privacy);
                //System.out.println(newPerson);

            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //find file with activity data
        try {
            String fileLocation = File.separator + "Users" + File.separator + "user" + File.separator + "Downloads" + File.separator + "SamplefileActivities2022Oct31text.csv";

            File activityList = new File(fileLocation);
            //scanner to read from file
            Scanner activityScanner = new Scanner(activityList);
            while (activityScanner.hasNextLine()) {
                String nextLine = activityScanner.nextLine();
                // split file into parts
                String[] activityComponents = nextLine.split(",");
                //get components of Persons Activities
                String firstname = activityComponents[0];
                String lastname = activityComponents[1];
                String Activity = activityComponents[2];
                //creating new person activity
                Activities newActivity = new Activities();
                //set data attributes for persons as we read from file
                newActivity.setFirstname(firstname);
                newActivity.setLastname(lastname);
                newActivity.setActivity(Activity);
                //System.out.println(newActivity);
            }
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
}
6tdlim6h

6tdlim6h1#

您似乎没有在while循环中存储已读人员和活动。

步骤1将2个数组列表(一个用于人员,一个用于活动)标记为:

ArrayList<Person> persons = new ArrayList()
ArrayList<Activities> activities = new ArrayList()

步骤2存储从文件中读取的人员和活动:在第一个循环中,在newPerson之后阅读person。setPrivacy(隐私);添加行

persons.add(newPerson);

在第二个循环中,阅读活动添加

activities.add(newActivity);

步骤3然后搜索人员和活动数组使用for循环列出并匹配联系人

相关问题