简体   繁体   English

ArrayList或Map? 也帮助如何存放孩子

[英]ArrayList or Map? Also help on how to store the children

I'm working on a project that has two classes. 我正在一个有两个类的项目中。 One that creates a class that defines a Person and another called FamilyInfo that creates either an ArrayList or Map. 一个创建一个定义Person的类,另一个创建名为FamilyInfo的类,创建一个ArrayList或Map。 FamilyInfo reads in a Scanner that is from a text file of names and their mother father relationships. FamilyInfo从名称及其父父关系的文本文件中读取扫描程序。

The names.txt file first has a list of all the names. names.txt文件首先包含所有名称的列表。 Then lists a name, the mother, then the father. 然后列出一个名字,母亲,然后是父亲。 Example: 例:

Henry VII 亨利七世
Arthur 亚瑟
Elizabeth of York 约克的伊丽莎白
etc 等等
etc 等等
... ...
END OF NAMES, CHILD/MOTHER/FATHER records follow: 名字的结尾,孩子/母亲/父亲的记录如下:
- --
Arthur 亚瑟
Elizabeth of York 约克的伊丽莎白
Henry VII 亨利七世
- --
Henry VIII 亨利八世
Elizabeth of York 约克的伊丽莎白
Henry VII 亨利七世
- --
etc 等等
etc 等等

This is my Person class so far: 到目前为止,这是我的Person类:

import java.util.*;

public class Person {

    private String name;
    private String mother;
    private String father;
    private ArrayList<String> kids;

    public Person(String name, String mother, String father) {
        this.name = name;
        this.mother = mother;
        this.father = father;
    }

    public Person(String name, String mother, String father, ArrayList<String> kids) {
        this.name = name;
        this.mother = mother;
        this.father = father;
        this.kids = kids;
    }

    public String getName() {
        return name;
    }

    public String getMother() {
        return mother;
    }

    public String getFather() {
        return father;
    }

    public String nthKid(int i) {
        return kids.get(i);
    }

    public int numKids() {
        return kids.size();
    }
}

This is what I have for FamilyInfo: 这是我对FamilyInfo的拥有:

import java.util.*;

public class FamilyInfo {

    private Map<String, Person> personMap;

    public FamilyInfo() {
        personMap = new HashMap<String, Person>();
    }

    public void read(Scanner input) {
        ArrayList<String> kids = new ArrayList<String>();

        while(input.hasNextLine()) {
            String name = input.nextLine();
            if(name.equals("-")) {
                continue;
            }
            if(personMap.containsKey(name)) {
                String mother = input.nextLine();
                String father = input.nextLine();

                personMap.put(name, new Person(name, mother, father));
            } else {
                personMap.put(name, null);
            }
        }
    }

    public Person getPerson(String names) {
        return personMap.get(names);
    }
}

The issue that I have is figuring out how to store the kids. 我遇到的问题是弄清楚如何存放孩子。 I have it passing an ArrayList of them but I have no idea how to get them and pass it. 我让它们传递了一个ArrayList,但是我不知道如何获取它们并传递它。 Also wondering if using a map is the best way to go about this or should I maybe change it to an ArrayList? 还想知道使用地图是否是解决此问题的最佳方法,还是应该将其更改为ArrayList? Any help is appreciated! 任何帮助表示赞赏!

Ok I think I see what you want to do. 好吧,我想我明白了你想做什么。 Consider the following refactoring: 考虑以下重构:

First make your instance variables in the Person class of type Person: 首先在Person类型的Person类中创建实例变量:

private String name;
private Person mother;
private Person father;
private ArrayList<Person> kids = new ArrayList<Person>();

Then remove the constructors add this one: 然后删除构造函数,添加以下内容:

public Person(String name) {
    this.name = name;
}

but in addition to this, have setters to set the mother and father, and a way to add kids to the list of kids: 但除此之外,还有设置者来设置父亲和母亲,以及将孩子添加到孩子列表中的方法:

public void setMother(Person mother) {
    this.mother = mother;
}

public void getFather(Person father) {
    this.father = father;
}

public void addKid(Person kid) {
    this.kids.add(kid);
}


Now moving on to the FamilyInfo class, using a map is good, because you want to be able to look up the people in the database quickly. 现在转到FamilyInfo类,使用地图很好,因为您希望能够快速查找数据库中的人员。 The real magic happens in the while loop: 真正的魔力发生在while循环中:

 while(input.hasNextLine()) { String name = input.nextLine(); if(name.equals("-")) { continue; } if(personMap.containsKey(name)) { // find the people Person mother = personMap.get(input.nextLine()); Person father = personMap.get(input.nextLine()); Person kid = personMap.get(name); // link them up kid.setMother(mother); kid.setFather(father); mother.addKid(kid); father.addKid(kid); } else { personMap.put(name, new Person(name)); // create the people objects from the initial list } } 

Are you expected to use these classes, or are they of your own making? 您应该使用这些类,还是您自己制作的?

In your model, you have described a person. 在模型中,您描述了一个人。 A person has parents (mother, father), but parents are people too - you should probably model them as such. 一个人有父母(母亲,父亲),但父母也是人-您可能应该这样建模。

So we can say a person has one father and one mother, each parent can have 1 or more children. 所以我们可以说一个人有一个父亲和一个母亲,每个父母可以有一个或多个孩子。 Parents know their children and children know their parents - this should tell you how to model the relationships. 父母知道他们的孩子,孩子也知道他们的父母-这应该告诉您如何建立关系模型。 This would make the FamilyInfo redundant as each person would manage it's own relationships. 这将使FamilyInfo多余,因为每个人都将管理自己的关系。

You should make the process of reading the data and creating the objects external to the model. 您应该进行读取数据和创建模型外部对象的过程。

@user2864740 comment is still valid and you will need to understand the difference @ user2864740评论仍然有效,您将需要了解区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将arrayList存储为Map&#39;string作为键以及如何检索它。 - how to store a arrayList to Map 'string as key and how to retrieve it..! 如何在Java中的映射中存储链接到字符串键的ArrayList - How to store an ArrayList linked to String keys in a Map in Java Java + Postgresql:如何在 HashMap 中存储所有子项的父项和列表<String,ArrayList<String> &gt; - Java + Postgresql : How to store Parent and List of all children in a HashMap<String,ArrayList<String>> 使用Castor,如何将Java类“ java.util.ArrayList”映射到元素“ ArrayList”,又如何为其包含的对象生成元素? - Using Castor, how do you map Java class “java.util.ArrayList” to element “ArrayList”, but also generate elements for the objects that it contains? 如何在arraylist中存储线程 - How to store threads in arraylist 如何在SharedPreferences中存储ArrayList? - How to store an ArrayList in SharedPreferences? 如何将对象存储到arraylist中? - how to store objects into an arraylist? 如何在文件中存储ArrayList? - How to store an ArrayList in a file? 尝试“逻辑”处理某些内容时遇到麻烦(还有语法/ ArrayList帮助) - Trouble trying to 'logic' something out (also the syntax/ArrayList help) 如何在 Java 中的 arraylist 中让所有子级递归 - How to get all children recursive in arraylist in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM