Why use an ArrayList as a constructor parameter in Java (BlueJ)? -
i'm trying complete assignment in bluej uni , i've hit snag @ first hurdle.
in assignment, given class, names of constructor, methods, , parameters of class. we're not allowed change these because assignments partially marked test unit (or effect).
one of constructors class given as
public class playlist { public playlist(string name, arraylist<track> tracks) { }
and have (partially) completed as
public class playlist { private string listname; private arraylist<track> listtracks = new arraylist<track>(); /** * constructs playlist title , arraylist of tracks * * @param name name of playlist * @param tracks arraylist of tracks in playlist */ public playlist(string name, arraylist<track> tracks) { listname = name; //i don't know tracks parameter yet }
okay, so, know question (how enter parameters arraylist in bluej?) have create instance of arraylist in order pass parameter in bluej.
what don't understand why - why have used arraylist<track>
parameter constructor? benefit of doing this?
(i figure there must benefit doing (if there wasn't functionality wouldn't exist in first place), don't understand what is, , if explain me, i'd appreciative.)
why have used
arraylist<track>
parameter constructor?
they did allow callers pass arbitrary number of track
s in single parameter.
java offers several options this: pass collection, array, or iterator. if designing signature constructor, prefer collection<track>
or @ least list<track>
arraylist<track>
, in order give callers more options far collection pass constructor.
going array list, should make defensive copy of it. 1 way using collections.copy
, this:
collections.copy(this.tracks, tracks);
once copy complete, should walk through elements of this.track
, , ensure not null
.
Comments
Post a Comment