[英]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);
}
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.