2

I'm trying to complete an assignment in BlueJ for uni and I've hit a snag at the first hurdle.

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.

What I don't understand is why - why have they used ArrayList<Track> as a parameter for the constructor? 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.)

Community
  • 1
  • 1
Prussan
  • 23
  • 2
  • It's quite clear. If you have a list of tracks, you can create a playlist by passing the name and the list to `Playlist`. If you just pass the name, you'll have to add the tracks separately, which can be unnecessary extra code if you already have the list of tracks you want to include. – Kayaman Oct 03 '15 at 08:09
  • A PlayList is a list of tracks to play, and a name. So it's quite natural to pass the name and the list of tracks to play when constructing the PlayList. It should be a List rather than an ArrayList, but that's another matter. What to do with thelist? The same thing you did with the name: initialize (listTracks) a field with the list of tracks passed as argument. – JB Nizet Oct 03 '15 at 08:10
  • @Kayaman But can't the same thing be achieved by just creating tracks and then adding them to the playlist using `tracks.add(track1)`? – Prussan Oct 03 '15 at 08:18
  • @Prussan Of course. But unless you're getting paid by lines of code, you probably shouldn't write excessively verbose code. – Kayaman Oct 03 '15 at 08:30

1 Answers1

2

why have they used ArrayList<Track> as a parameter for the constructor?

They did it to allow callers to pass an arbitrary number of Tracks in a single parameter.

Java offers several options to do this: you could pass a collection, an array, or even an iterator. 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.

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(this.tracks, tracks);

Once the copy is complete, you should walk through elements of this.track, and ensure that they are not null.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • or `listTracks.addAll(tracks)` to copy the tracks into its own collection. – Jerry101 Oct 03 '15 at 08:21
  • Right, so essentially, doing this avoids all the rigmarole of having to add each track to the ArrayList individually. I wish they'd mentioned this in class! – Prussan Oct 03 '15 at 08:29