简体   繁体   English

为什么在 Java (BlueJ) 中使用 ArrayList 作为构造函数参数?

[英]Why use an ArrayList as a constructor parameter in Java (BlueJ)?

I'm trying to complete an assignment in BlueJ for uni and I've hit a snag at the first hurdle.我正在尝试在 BlueJ 中为大学完成一项任务,但我在第一个障碍上遇到了障碍。

In the assignment, we are given a class, as well as the names of the constructor, methods, and parameters of that class.在赋值中,我们得到一个类,以及该类的构造函数、方法和参数的名称。 We're not allowed to change these because the assignments are partially marked by a test unit (or something to that effect).我们不允许更改这些,因为分配部分由测试单元(或类似的东西)标记。

One of the constructors for this class is given as此类的构造函数之一给出为

 public class PlayList
 {
    public PlayList(String name, ArrayList<Track> tracks) {
    }

And I have (partially) completed it as我已经(部分)完成了它

public class PlayList
{
   private String listName;
   private ArrayList<Track> listTracks = new ArrayList<Track>();

   /**
    * Constructs a playlist with a title and an ArrayList of tracks
    * 
    * @param name The name of the playlist
    * @param tracks An ArrayList of tracks in the playlist
    */
   public PlayList(String name, ArrayList<Track> tracks) {
       listName = name;
       //I really don't know what to do with the tracks parameter yet
   }

Okay, so, I know from this question ( How do I enter parameters for an ArrayList in BlueJ? ) that I have to create an instance of an ArrayList in order to pass it as a parameter in BlueJ.好的,所以,我从这个问题( 如何在 BlueJ 中为 ArrayList 输入参数? )知道我必须创建一个 ArrayList 的实例才能将它作为参数在 BlueJ 中传递。

What I don't understand is why - why have they used ArrayList<Track> as a parameter for the constructor?我不明白的是为什么- 为什么他们使用ArrayList<Track>作为构造函数的参数? What is the benefit of doing this?这样做有什么好处?

(I figure there must be a benefit to doing it like this (if there wasn't the functionality wouldn't exist in the first place), but I don't understand what it is, and if someone could explain it to me, I'd be greatly appreciative.) (我认为这样做肯定有好处(如果没有功能一开始就不存在),但我不明白它是什么,如果有人可以向我解释,我将不胜感激。)

why have they used ArrayList<Track> as a parameter for the constructor?为什么他们使用ArrayList<Track>作为构造函数的参数?

They did it to allow callers to pass an arbitrary number of Track s in a single parameter.他们这样做是为了允许调用者在单个参数中传递任意数量的Track

Java offers several options to do this: you could pass a collection, an array, or even an iterator. Java 提供了多种选项来执行此操作:您可以传递一个集合、一个数组,甚至一个迭代器。 If I were designing a signature for the constructor, I would strongly prefer Collection<Track> or at least a List<Track> to ArrayList<Track> , in order to give callers more options as far as what collection they could pass to my constructor.如果我正在为构造函数设计一个签名,我会非常喜欢Collection<Track>或至少一个List<Track>ArrayList<Track> ,以便为调用者提供更多选择,因为他们可以传递给我的构造函数的集合.

Going back to what to do with the array list, you should make a defensive copy of it.回到如何处理数组列表,您应该制作它的防御性副本 One way would be using Collections.copy , like this:一种方法是使用Collections.copy ,如下所示:

Collections.copy(this.tracks, tracks);

Once the copy is complete, you should walk through elements of this.track , and ensure that they are not null .复制完成后,您应该遍历this.track元素,并确保它们不为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM