Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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_Oop - Fatal编程技术网

用Java存储对象集合的最佳方法?

用Java存储对象集合的最佳方法?,java,oop,Java,Oop,我正在为学生信息系统创建一个转储Java应用程序,用于学习和实现OOPS概念,如继承、抽象、多态性和封装 我正在做的是,我创建了教员班、学生班和一个大学班。现在我想在大学里增加新的教员。因此,我的方法是在大学课堂上创建一个方法,即addFaculty(Faculty f)和firefaulty(Faculty f),现在我想在大学课堂上添加学院 最好的方法是什么?如何将教员对象列表存储在学院对象中。因为我可以在大学里增加一个以上的教员和一个以上的学生 在OOPS中解决这个问题的最佳方法是什么 这

我正在为学生信息系统创建一个转储Java应用程序,用于学习和实现OOPS概念,如继承、抽象、多态性和封装

我正在做的是,我创建了
教员
班、
学生
班和一个大学班。现在我想在大学里增加新的教员。因此,我的方法是在大学课堂上创建一个方法,即
addFaculty(Faculty f)
firefaulty(Faculty f)
,现在我想在
大学
课堂上添加
学院

最好的方法是什么?如何将
教员
对象列表存储在
学院
对象中。因为我可以在大学里增加一个以上的教员和一个以上的学生

在OOPS中解决这个问题的最佳方法是什么

这是我已经实现的College.java代码,它工作得很好,但这是我能解决它的最好方法吗

public class College 
{
String name;
String location;
String courses[];
HashMap<String,Faculty> faculties;
int noOfFaculties = 0;
int noOfStudents = 0;

public College(String name,String location,String courses[])
{
    this.name = name;
    this.location = location;
    this.courses = courses;
    faculties = new HashMap<>();
}

public void addFaculty(Faculty faculty)
{
    faculties.put(faculty.getName(),faculty);
}

public void printFaculties()
{
    Set<String> set = faculties.keySet();
    if(set.size()>0)
    {
        for(String s:set)
        {
            System.out.println(faculties.get(s).getName());
        }
    }
    else
    {
        System.out.println("No Faculties Currently Working");
    }
}

public void fireFaculty(Faculty faculty)
{
    faculties.remove(faculty.getName());
}
public String getName()
{
    return name;
}

public String getLocation()
{
    return location;
}

public String[] getCourses()
{
    return courses;
}
}
公立学院
{
字符串名;
字符串位置;
弦乐课程[];
HashMap学院;
int noOfFaculties=0;
int noOfStudents=0;
公立学院(字符串名称、字符串位置、字符串课程[])
{
this.name=名称;
这个位置=位置;
本课程=课程;
faculties=newhashmap();
}
公共教育学院
{
faculties.put(faculty.getName(),faculty);
}
公共印刷学院()
{
Set=faculties.keySet();
如果(set.size()>0)
{
for(字符串s:set)
{
System.out.println(faculties.get(s.getName());
}
}
其他的
{
System.out.println(“目前没有学院工作”);
}
}
公共教育学院
{
remove(faculties.getName());
}
公共字符串getName()
{
返回名称;
}
公共字符串getLocation()
{
返回位置;
}
公共字符串[]getCourses()
{
返回课程;
}
}

有很多方法可以做到这一点。处理存储对象集合的最简单方法可能是使用Java提供的。对于初学者来说,最容易理解的可能是
ArrayList
,它基本上是一个数组,其大小根据集合中对象的数量动态增长

因此,作为AxSample,您的代码可能如下所示:

public class College
{
    private ArrayList<Faculty> faculty;

    public College()
    {
        faculty = new ArrayList<Faculty>();
    }

    public void addFaculty(Faculty f)
    {
        faculty.add(f);
    }

    public void fireFaculty(Faculty f)
    {
        faculty.remove(f);
    }
}
公立学院
{
私立ArrayList学院;
公立学院()
{
faculty=newarraylist();
}
公共教育学院(f学院)
{
增加(f);
}
公共教育学院(f学院)
{
删除(f);
}
}

有很多方法可以做到这一点。处理存储对象集合的最简单方法可能是使用Java提供的。对于初学者来说,最容易理解的可能是
ArrayList
,它基本上是一个数组,其大小根据集合中对象的数量动态增长

因此,作为AxSample,您的代码可能如下所示:

public class College
{
    private ArrayList<Faculty> faculty;

    public College()
    {
        faculty = new ArrayList<Faculty>();
    }

    public void addFaculty(Faculty f)
    {
        faculty.add(f);
    }

    public void fireFaculty(Faculty f)
    {
        faculty.remove(f);
    }
}
公立学院
{
私立ArrayList学院;
公立学院()
{
faculty=newarraylist();
}
公共教育学院(f学院)
{
增加(f);
}
公共教育学院(f学院)
{
删除(f);
}
}

如果您不介意使用
列表,请使用
哈希集

例如:

class College {
    private List<Faculty> listFactories = new ArrayList<>();  // dupes allowed
    private Set<Faculty> setFactories = new HashSet<>(); // no dupes allowed
}
班级学院{
私有列表ListFactorys=new ArrayList();//允许重复
private Set setFactories=new HashSet();//不允许重复
}

.

如果您不介意使用
列表,请使用
哈希集

例如:

class College {
    private List<Faculty> listFactories = new ArrayList<>();  // dupes allowed
    private Set<Faculty> setFactories = new HashSet<>(); // no dupes allowed
}
班级学院{
私有列表ListFactorys=new ArrayList();//允许重复
private Set setFactories=new HashSet();//不允许重复
}

.

我认为这取决于学院提供什么样的服务。如果我正在编码,我将从以下内容开始:-

List<Faculy> faculties = new ArrayList<>();
....
public void addFaculty(Faculty f) {
  faculties.add(f);
 }
 //... etc 
List faculties=new ArrayList();
....
公共教育学院(f学院){
加上(f);
}
//... 等

如果需要的话,以后再换一个助教。

我想这取决于学院提供什么样的服务。如果我正在编码,我将从以下内容开始:-

List<Faculy> faculties = new ArrayList<>();
....
public void addFaculty(Faculty f) {
  faculties.add(f);
 }
 //... etc 
List faculties=new ArrayList();
....
公共教育学院(f学院){
加上(f);
}
//... 等

如果需要的话,请稍后改为altearnative。

你能为你的课程发布代码吗?要在college对象中添加多个教员,只需在college中添加某种集合字段。只需在college中创建教员列表或其他内容作为属性,以跟踪他们。哎呀!绘制UML类图。我已经添加了代码。你可以发布你拥有的类的代码吗?要在college对象中添加多个教员,只需在college中添加某种集合字段。只需在college中创建教员列表或其他内容作为属性,以跟踪他们。哎呀!绘制UML类图。我已经添加了代码