代码之家  ›  专栏  ›  技术社区  ›  Bapho

ArrayList还是Map?还有关于如何存放孩子的帮助

  •  0
  • Bapho  · 技术社区  · 10 年前

    我正在做一个有两个类的项目。一个创建定义Person的类,另一个名为FamilyInfo的类创建ArrayList或Map。FamilyInfo从包含姓名及其父母关系的文本文件中读取扫描仪。

    names.txt文件首先包含所有名称的列表。然后列出一个名字,母亲,然后是父亲。 例子:

    亨利七世
    亚瑟
    约克的伊丽莎白


    ...
    姓名、子女/母亲/父亲记录如下:
    -
    亚瑟
    约克的伊丽莎白
    亨利七世
    -
    亨利八世
    约克的伊丽莎白
    亨利七世
    -


    这是我迄今为止的个人课程:

    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();
        }
    }
    

    这是我为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);
        }
    }
    

    我面临的问题是如何存放孩子。我让它传递一个它们的ArrayList,但我不知道如何获取它们并传递它。我还想知道,使用地图是实现这一点的最佳方式,还是我应该将其更改为ArrayList?感谢任何帮助!

    2 回复  |  直到 10 年前
        1
  •  2
  •   nmore    10 年前

    好的,我想我明白你想做什么了。考虑一下以下重构:

    首先在Person类型的Person类中创建实例变量:

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

    然后移除构造函数,添加此构造函数:

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

    但除此之外,还有设置父母的设置器,以及将孩子添加到孩子列表中的方法:

    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);
    }
    


    现在转到FamilyInfo类,使用map是很好的,因为您希望能够快速查找数据库中的人员。真正的魔力发生在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
        }
    }
    
        2
  •  0
  •   Romski    10 年前

    你应该使用这些类吗,还是你自己制作的?

    在你的模型中,你描述了一个人。一个人有父母(母亲、父亲),但父母也是人——你可能应该这样做。

    所以我们可以说一个人有一个父亲和一个母亲,每个父母可以有一个或多个孩子。父母了解他们的孩子,孩子了解他们的父母——这应该告诉你如何建立关系。这将使 FamilyInfo 因为每个人都会管理自己的关系。

    您应该进行读取数据和创建模型外部对象的过程。

    @user2864740注释仍然有效,您需要了解其中的区别