Returning custom objects from an ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shredder249
    New Member
    • Jan 2008
    • 22

    Returning custom objects from an ArrayList

    Hi,

    I have one class called Track and another class called TrackList. TrackList is an ArrayList made up of Track objects. I can add Track objects to a TrackList but I cannot retrieve them as actual Track objects from the ArrayList, I can only retrieve them as generic objects. This means that I cannot use any methods from the Track class on them.

    How can I get Tracks out of a TrackList as actual Track objects?

    Cheers
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Are you using generics so that you can retrieve Track objects from your TrackList(s) without an explicit cast? As in:

    Code:
    Track aTrack= new Track( ... );
    List<Track> trackList= new ArrayList<Track>();
    
    list.add(aTrack);
    
    aTrack= list.get(0);
    kind regards,

    Jos

    Comment

    • shredder249
      New Member
      • Jan 2008
      • 22

      #3
      Fixed

      Ah I wasn't, I was using:

      ArrayList TrackList = new ArrayList();

      But now I've added the <Track> bits it works, many thanks!

      Comment

      Working...