Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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_Collections - Fatal编程技术网

Java 创建一个类来为列表建模

Java 创建一个类来为列表建模,java,collections,Java,Collections,我有一个名为Person的类。这个类表示(正如名称所示)一个人。现在我必须创建一个类PhoneBook来表示人的列表。我该怎么做?我不明白“创建一个类来表示一个列表”是什么意思 import java.util.*; 公共阶层人士{ 私家姓; 私有字符串名称; 私有字符串标题; 私人字符串邮件地址; 私人弦公司; 私有字符串位置; 私人家庭电话; 私人国际电话; 私人智能手机; 私人收藏电话簿; 公众人物(字符串姓氏、字符串名称、字符串标题、字符串邮件地址、字符串公司、字符串位置){ 这个。姓=

我有一个名为
Person
的类。这个类表示(正如名称所示)一个人。现在我必须创建一个类
PhoneBook
来表示
人的列表。我该怎么做?我不明白“创建一个类来表示一个列表”是什么意思

import java.util.*;
公共阶层人士{
私家姓;
私有字符串名称;
私有字符串标题;
私人字符串邮件地址;
私人弦公司;
私有字符串位置;
私人家庭电话;
私人国际电话;
私人智能手机;
私人收藏电话簿;
公众人物(字符串姓氏、字符串名称、字符串标题、字符串邮件地址、字符串公司、字符串位置){
这个。姓=姓;
this.name=name;
这个.title=title;
this.mail\u addr=mail\u addr;
这个公司=公司;
这个位置=位置;
otherphonebooklist=新建ArrayList();
}
公共字符串getNames(){
返回姓氏;
}
公共字符串getName(){
返回名称;
}
公共字符串getTitle(){
返回标题;
}
公共字符串getMailAddr(){
返回公司;
}
公共字符串getCompany(){
返回位置;
}
公用无效设置家庭电话(int hp){
homephone=hp;
}
公用无效setOfficePhone(int op){
officephone=op;
}
公共电话(int cp){
手机=cp;
}
public int getHomePhone(){
返回家庭电话;
}
public int getOfficePhone(){
返回电话;
}
公共int GetPhone(){
归还手机;
}
公共集合getOtherPhoneBook(){
返回其他电话簿列表;
}
公共字符串toString(){
字符串temp=“”;
temp+=“\n用户名:”+姓氏;
temp+=“\n名称:”+名称;
temp+=“\n标题:”+标题;
temp+=“\n邮件地址:”+邮件地址;
temp+=“\n公司:”+公司;
温度+=“\n位置:”+位置;
返回温度;
}
}

您的
电话簿
类可能会有这样一个成员:

private List<Person> book = new ArrayList<Person>();
请注意,
列表
并不是电话簿的最佳表示形式,因为(在最坏的情况下)您需要遍历整个列表才能查找号码


您可以进行许多改进/增强。这应该可以让您开始了。

您可以通过创建一个类电话簿来完成

public class PhoneBook{

Private List<Person> personList = new ArrayList<Person>;

public void addPerson(Person person){
this.personList.add(person);
}

public List getPersonList(){
return this.personList;
}

public Person getPersonByIndex(int index){
return this.personList.get(index);
}
}
公共类电话簿{
Private List personList=新建ArrayList;
公众人士(人){
this.personList.add(person);
}
公共列表getPersonList(){
返回此.personList;
}
公众人物getPersonByIndex(int索引){
返回this.personList.get(索引);
}
}

基于名为PhoneBook的类,我假设您最终想要创建一个电话号码和一个人之间的映射。如果这是您需要做的,那么您的电话簿类应该包含一个映射而不是一个列表(但这可能取决于项目的其他参数)

公共类电话簿
{
private Map people=new HashMap();
public void addPerson(字符串phoneNumber,Person)
{
people.put(电话号码、个人);
}
public void getPerson(字符串phoneNumber)
{
返回人物。获取(电话号码);
}
}

在上面的示例中,电话号码表示为字符串,这可能并不理想,因为同一电话号码可能具有不同的字符串表示形式(不同的间距或破折号等)。理想情况下,映射键应该是一个PhoneNumber类,它在hashCode和equals函数中考虑了这一切。

这可能意味着有一个Person对象列表作为电话簿的成员。
import java.util.*/**这个类表示person*/public类电话簿{private person;public PhoneBook(person-person){This.person=person;}public person getPerson(){return person;}
@AljoshaBre你能通过给我看代码来解释你说的话吗?好的,现在更清楚了。但是这个类电话簿必须有一个构造函数?它可以。例如,您的构造函数可能会从一个现有的列表开始编写这本书。
public void add(final Person person) {
    this.book.add(person);
}

public Person get(final Person person) {
    int ind = this.book.indexOf(person);
    return (ind != -1) ? this.book.get(ind) : null;
}
public class PhoneBook{

Private List<Person> personList = new ArrayList<Person>;

public void addPerson(Person person){
this.personList.add(person);
}

public List getPersonList(){
return this.personList;
}

public Person getPersonByIndex(int index){
return this.personList.get(index);
}
}
public class PhoneBook
{
    private Map<String,Person> people = new HashMap<String,Person>();

    public void addPerson(String phoneNumber, Person person)
    {
        people.put(phoneNumber,person);
    }

    public void getPerson(String phoneNumber)
    {
        return people.get(phoneNumber);
    }
}