【问题标题】:Adding an ArrayList into a class - what does it do?将 ArrayList 添加到类中 - 它有什么作用?
【发布时间】:2009-04-20 19:07:00
【问题描述】:

继续我的这个Q:What's the best way to make this Java program?

建议我在 Lecturer 类和 Course 类中存储一个列表。所以我做了,它看起来像这样:

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;

    }

}

除了有讲师列表外,Course 课程也是如此。但我不明白的是,在那里放置一个列表到底有什么作用?因为我不知道ArrayList的添加和删除讲师等方法放在哪里?

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

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

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

}

我希望我是有道理的,因为过去 2 周以来我一直坚持一件事,似乎没有人能理解。

谢谢

【问题讨论】:

    标签: java list attributes arraylist


    【解决方案1】:

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

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

    您可以为 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);
    }
    

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

    【讨论】:

    • 您好,谢谢您的回复,我已经有一些非常相似的东西但它不起作用,我发布了这个但似乎没有人明白我在说什么,这里是:stackoverflow.com/questions/765937/…跨度>
    【解决方案2】:

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

    一旦您将 Courses c1, c2, ..., cn 声明为 Lecturer l 教授,您将它们关联到 Map mm.put(l, c) 其中 c 是课程的 List ,声明为new LinkedList&lt;Course&gt;(),并添加为c.add(c1); c.add(c2); ... c.add(cn);

    如果我解释清楚的话。

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

    要获得反向关联,可以很方便地使用以下代码:

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

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

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

    【讨论】:

    • 所以我不应该有讲师名单吗?因为我还需要能够查看教授课程的讲师列表。谢谢你的回复
    猜你喜欢
    • 1970-01-01
    • 2011-10-06
    • 2015-03-30
    • 1970-01-01
    • 2021-06-11
    • 2020-11-08
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多