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 trialKyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsCan someone verify I understand this concept correctly?
So I'm learning about ArrayLists in Java. I just want to make sure I understand this concept correctly. One of the exercise wanted to find any words that started with "http", inorder to find links.
So it asked to import two things. java.util.ArrayList; and java.util.List; which I feel like I understand importing about 80%, meaning I know I need to use it and I know how to use I just don't have a full complete knowledge of why it really is necessary for Java to have these things. Which is okay, I'll pick up on that more later. My real question is this method.
public List<String> getExternalLinks () { List<String> links = new ArrayList<String> (); for (String word : getWords()) { if (word.startsWith("http")) { links.add(word); } } return links; }
in the exercise I got it right, but I want to explain what I know what is going on in this method and have anyone else who is more experienced with programming, which should be most of you, to fill in the gaps. K, here I go.
The first line is making the method public, I get that. The next is stating that this method will be a List of String, I get that. The name of the method is getExternalLinks, makes sense to me. The next line is creating a new list of strings referencing to a variable I invented myself called links. We are getting this reference from the ArrayList<String>??? I think, like I feel that I'm on the right track with that but please correct me if I'm wrong there. The next line is what gets me. the for is stating that for each word in the string from the previous method getWords. So there was a previous method called getWords and this is the list of words we are sorting through to find words that start with "http". Am I on the right track so far? I think I am but please insert comment if i'm not. I don't understand the "word" part though in the for statement. Is word a variable that we just created? Is it Java language so I will always use the word "word" in this context? More clarification on the word "word" in the for statement would be great. Thanks. Moving on, so for the string of words from the getWords method, if the word starts with "http" then add this word to the list we made called links. Then return links. Did I get it mostly right? I appreciate any help. Thanks!
1 Answer
Nico Julian
23,657 PointsHello there,
It appears to me that you've got it down just fine. All that I could see to mention is that when the List links is declared, you could use a generic to specify what will populate the list. Since the word variable that is added to it will be of type String, you could let the list this know when declaring it.
public List getExternalLinks() {
List<String> links = new ArrayList<>();
// List links = new ArrayList ();
for (String word : getWords()) {
if (word.startsWith("http")) {
links.add(word);
}
return links;
}
}
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsKyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsSo I just read over that yes, the "word" in there is a variable that was made up by Craig that I followed to write.