Java Java Data Structures Exploring the Java Collection Framework ArrayLists

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Java Collections challenge

I’m kind of lost about interfaces and implementations. But here’s my code to this challenge.Can anyone point out the syntax errors? Thanks!

com/example/BlogPost.java
package com.example;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class BlogPost implements Comparable<BlogPost>, Serializable {
  private String mAuthor;
  private String mTitle;
  private String mBody;
  private String mCategory;
  private Date mCreationDate;

  public BlogPost(String author, String title, String body, String category, Date creationDate) {
    mAuthor = author;
    mTitle = title;
    mBody = body;
    mCategory = category;
    mCreationDate = creationDate;
  }

  public int compareTo(BlogPost other) {
    if (equals(other)) {
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

  public String[] getWords() {
    return mBody.split("\\s+");
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getBody() {
    return mBody;
  }

  public String getCategory() {
    return mCategory;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }
  public List getExternalLinks(){
  List<String> linkList=ArrayList<String>[];
  for(String word:getWords()){
      if word.startsWith("http"){
          linkList.add(word);
      }
  }
    return linkList;
  }
}

1 Answer

Emmanuel C
Emmanuel C
7,624 Points

Hey Leo,

There is quite a few syntax errors with the getExternalLinks(). Here is what is should look like

public List<String> getExternalLinks(){
List<String> linkList= new ArrayList<String>();
  for(String word:getWords()){
      if (word.startsWith("http")){
          linkList.add(word);
      }
  }
    return linkList;
}

The return type of List needs to be a list of String, there was missing parenthesis in the if statement. Also when you created linkList it needed to be set to a new instance of ArrayList, and it also need parentheses not brackets. Also at the top of the file you had to import java.util.ArrayList to use them, but you can just do

import java.util.*;

in order to import everything from the java.util package.