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 trialGabriel E
8,916 PointsHow do I create Lists for this challenge?
Hi there,
I need to create a method so that my code does something special with all words starting with "http". I'm really not sure where to go from here. Thanks for the help!!
package com.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
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<String> getExternalLinks() {
return getWordsPrefixedWith("http");
}
}
1 Answer
Logan R
22,989 PointsFor this challenge, we will be using a special list, called an ArrayList. It's basically an array with no set length. We can continually add items to it, and it will expand to our needs.
First we want to create our method, called getExternalLinks()
which will returns a List.
public List<String> getExternalLinks() {
ArrayList<String> results = new ArrayList<String>();
return results;
}
We want to get a list of words that have http in them from the body. Our body may look like this:
Hello! How are you? Have you seen http://teamtreehouse.com today?
There is already a cool method written for us that we can use to easily get each word in the body:
public String[] getWords() {
return mBody.split("\\s+");
}
So now all we need to do is loop through each word and check if the first 4 characters in the word are 'http'
public List<String> getExternalLinks() {
ArrayList<String> results = new ArrayList<String>();
for (String word : getWords()) {
if (word.startsWith("http")) {
results.add(word);
}
}
return results;
}
I hope this helps you out! If you still have any other questions or need any clarification, feel free to ask :)
Quick note: Make sure you import both util.List and util.ArrayList.