java 将 ArrayList 添加到类中 - 它有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/769680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:41:18  来源:igfitidea点击:

Adding an ArrayList into a class - what does it do?

javalistattributesarraylist

提问by

Following on from this Q of mine: What's the best way to make this Java program?

继我的这个问题之后:制作这个 Java 程序的最佳方法是什么?

I was recommended to store a list in Lecturer class and Course class. So I did, and it looks something like this:

我被建议在讲师班和课程班中存储一个列表。所以我做了,它看起来像这样:

public class Lecturer
{
    private String id;  
    private String name;  
    List<Course> courses = new ArrayList<Course>();  // a list to hold the courses


    public Lecturer(String idIn, String nameIn)     // arguments of the constructor
    {
        id = idIn;
        name = nameIn;

    }

}

Same thing for the Course class except it has Lecturer list. But what I dont get is what does placing a list there exactly do? cos I dont know where to put the methods of the ArrayList such as adding and removing lecturers from it?

Course 类也一样,除了它有讲师列表。但我不明白的是,在那里放置一个列表到底有什么作用?因为我不知道将ArrayList的方法(例如从中添加和删除讲师)放在哪里?

Can someone explain the purpose of this?

有人可以解释一下这样做的目的吗?

I use another method which is basically placing the arraylists and its methods in two seperate classes for the lecturer and course, and then I simply add into the Course and Lecturer class as an attribute eg:

我使用另一种方法,它基本上将数组列表及其方法放在讲师和课程的两个单独的类中,然后我简单地将作为属性添加到课程和讲师类中,例如:

public class Lecturer
{
    private String id;  
    private String name;  
    private CourseList courses;  // COurseList is the class with the arraylist and methods


    public Lecturer(String idIn, String nameIn)     // arguments of the constructor
    {
        id = idIn;
        name = nameIn;
        courses = new CourseList();
    }

}

I hope i'm making sense, cos ive been stuck on one thing for the last 2 weeks which no seems to understand.

我希望我说得有道理,因为过去 2 周我一直坚持做一件事,这似乎没有人理解。

Thank you

谢谢

回答by Outlaw Programmer

With the first method, you need to expose methods that allow client code to add stuff to those lists. So, you could have:

使用第一种方法,您需要公开允许客户端代码向这些列表添加内容的方法。所以,你可以有:

public class Lecturer
{
    List<Course> courses = new ArrayList<Course>();  // a list to hold the courses

    public Lecturer(String idIn, String nameIn)
    {
        /* do stuff */
    }

    public void addCourse(Course newCourse)
    {
        this.courses.add(newCourse);
    }
}

You can do something similar for the Course class. Once you have those set up, you can do something like this:

您可以为 Course 类做类似的事情。完成这些设置后,您可以执行以下操作:

public static void main(String[] args)
{
    Lecturer bob = new Lecturer(1, "Bob Smith");
    Course math = new Course("Math 101");

    // wire them together such that Bob teaches Math 101
    bob.addCourse(math);
    math.addLecturer(bob);
}

I think that solves what you're asking, but having this 2-way, circular relationship is sometimes a sign of a bad design. Only you know what you're real assignment is, though, so I hope this helps!

我认为这可以解决您的问题,但是这种双向循环关系有时是设计不佳的标志。不过,只有你知道你真正的任务是什么,所以我希望这会有所帮助!

回答by Humphrey Bogart

I'd advise using Map<Lecturer, List<Course>>, as someone replied in the previous question, which means, "an association (Map) between different lecturers (Lecturer) to a list of the courses they teach (List<Course>), instead, which you would instantiate as new HashMap<Lecturer, List<Course>>. Otherwise, storing a list for each, you'll be duplicating functionality.

我建议使用Map<Lecturer, List<Course>>,正如有人在上一个问题中回答的那样,这意味着“Map不同讲师 ( Lecturer)之间的关联 ( ) 与他们教授的课程列表 ( List<Course>) 相反,您可以将其实例化为 new HashMap<Lecturer, List<Course>>。否则,存储一个列出每个,您将复制功能。

Once you declared the Courses c1, c2, ..., cna Lecturerlteaches, you associate them in the Mapmas m.put(l, c)where cis a Listof the courses, declared as new LinkedList<Course>(), and added as c.add(c1); c.add(c2); ... c.add(cn);.

声明Courses c1, c2, ..., cna 后Lecturerl,您将它们关联到课程的Mapmas m.put(l, c)where cis aList中,声明为new LinkedList<Course>(),并添加为c.add(c1); c.add(c2); ... c.add(cn);

If I explain myself clearly.

如果我解释清楚。

You can read http://java.sun.com/docs/books/tutorial/collections/interfaces/map.htmland http://java.sun.com/docs/books/tutorial/collections/implementations/map.htmlfor more help on using Maps.

您可以阅读http://java.sun.com/docs/books/tutorial/collections/interfaces/map.htmlhttp://java.sun.com/docs/books/tutorial/collections/implementations/map.html有关使用Maps 的更多帮助。

To obtain the reverse association, you can easily use the following code:

要获得反向关联,您可以轻松使用以下代码:

Collection<Course> courses = m.values();
Map<Course, List<Lecturer>> reverseAssociation = new HashMap<Course, List<Lecturer>>;

    for (Course course : courses) {
        List<Lecturer> lecturersTeachingCourse = new LinkedList<Lecturer>();

        for (Lecturer lecturer : m.keySet()) {
            if (lecturer.teaches(course)) {
                lecturersTeachingCourse.add(lecturer);
            }
        }

        coursesTaught.put(course, lecturersTeachingCourse);
    }

Which, as long as Lecturer.teaches(Course)queries whether the lecturer teaches the passed course, sets reverseAssociationas the courses-lecturers association. (Naturally, you should encapsulate that code as a method in Lecturers).

其中,只要Lecturer.teaches(Course)查询讲师是否教授通过的课程,设置reverseAssociation为课程讲师协会。(当然,您应该将该代码封装为 Lecturers 中的方法)。

Good luck JavaNoob! We were all there once!

祝 JavaNoob 好运!我们都去过一次!