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 - Retired Efficiency! Changing Course

Arthur Podkowiak
Arthur Podkowiak
3,633 Points

Java Data Structures "The Beginning Bits" help

I'm not sure why my code is not working, this is a lot tougher than I expected. I thought I'd know so much, but now I'm getting stuck on simple things like this! Anyways, I'm getting an error saying index out of bounds, which makes sense, because I created a new list with nothing in it and am trying to place the video at the second index. Help!!

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.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;


public class QuickFix {

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

    // TODO(2):  Add to the course videos as the second video.
    List<Video> videoList = new ArrayList<Video>();
    videoList.add(1, video);
  }

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

  }

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

}

1 Answer

It looks like the task wants you to add the video to the relevant course's pre-existing list of videos. Right now, your code is adding the video to a new list.

Note that addForgottenVideo has a parameter named course... so you can get its video list like so:

List<Video> videoList = course.getVideos();

Then you just need to use that list instead of the new one you made.

Arthur Podkowiak
Arthur Podkowiak
3,633 Points

Where is this already created list then? has it been created in the background, like we don't know about it? I see no list created. If there was one I think Craig should mention that in the directions:\ But thank you for the reply it worked!

You're right, I think it's happening behind the scenes in some of the testing/grading code that Craig is using. It is an unclear problem setup. Glad to hear that you got it working.