Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Changing Course

Passing a List into a class object as the second element.

i believe I have the first of these todo's done, correctly. That wasn't difficult. But Im stuck on the second todo -- passing a video into a class Course as the second element in the list.

Can somebody point me in the right direction, as this will help me finish the chapter.

com/example/model/Course.java
package com.example.model;

import java.util.List;

public class Course {
  private String mName;
  private List<Video> mVideos; 

  public Course(String name, List<Video> videos) {
    mName = name;
    mVideos = videos;
  }

  public String getName() {
    return mName;
  }

  public List<Video> getVideos() {
    return mVideos;
  }

}
com/example/model/Video.java
package com.example.model;

public class Video {
  private String mTitle;

  public Video(String title) {
    mTitle = title;
  }

  public String getTitle() {
    return mTitle;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

}
QuickFix.java
import com.example.model.Course;
import com.example.model.Video;

import java.util.Map;

public class QuickFix {
  private Video video;

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
      video = new Video("The Beginning Bits");

    // TODO(2):  Add the newly created video to the course videos as the second video.

  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {

  }

  public Map<String, Video> videosByTitle(Course course) {
    return null;
  }

}

2 Answers

Steven Parker
Steven Parker
229,644 Points

You're not quite done with TODO(1). The compiler error that the "Preview" button will show you might help. But I'll also hint that when creating new variables, the declaration must include the type of the variable.

Then for TODO(2), what method does the "course" have that will allow you to access the list of videos in it?
And then, what other method might you chain on that will allow you to add something to a list?

Steven Parker
Steven Parker
229,644 Points

You can disregard my first suggestion, I overlooked the field declaration because I wasn't expecting any new work to be done above the "TODO{1}" line.

But you won't neede "setVideo". Using the list returned by "getVideos", you can call the "add" method which allows you to specify the index where the new item will be inserted.

Hello, that didn't quite do it for me, i think. For TODO #1 --
I delcare the variable in the scope of the class:

 private Video video;

And for the second TODO, I can use the getVideos method to get a list of all videos, and then the setVideo method to set the video, but Im confused on how to get the second element in the list.

Can you provide a little more detail here?

Thanks Chris