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
Harry James
14,780 PointsWhere did "friend" come from?
Hello,
In this section of code:
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
// list returned - look for a match
for (int i = 0; i < mUsers.size(); i++) {
ParseUser user = mUsers.get(i);
for (ParseUser friend : friends) {
if (friend.getObjectId().equals(user.getObjectId())) {
getListView().setItemChecked(i, true);
}
}
}
}
I was wondering where the "friend" came from in the for statement. I don't quite understand it.
So, it's checking whether friend (Which is a ParseUser) is inside of the list of friends which got passed in to done. Now, where does this friend word come from? How does it check if this "friend" is inside of friends, I don't see where "friend" comes from or how it gets given a value.
If someone could explain this, It would be greatly appreciated!
Thanks!
5 Answers
Kristen Law
16,244 PointsYes, if you did for (ParseUser person : friends), it would run some code for each ParseUser in your List<ParseUser> friends.
In the case of this for-each loop, the variable person can be used within the loop to reference each ParseUser in friends. Parse does not "know" what a person is, but Java is aware that the variable person will refer to each ParseUser in friends within the context of the for-each loop.
Hope that makes it more clear, but feel free to ask for more clarification!
Kristen Law
16,244 Pointsfriend is kind of like a placeholder variable. It can be named anything. The for loop is saying: for each friend in friends, do something. It loops through the items in friends, calling each item one at a time. friend is used as the variable that references the item that the for loop is currently accessing.
The for loop is basically iterating through friends and comparing each item's object id to the user object id, and then executing some code if they are equal.
Edit: As Roland stated, the correct term is for-each loop. I wrote for loop for brevity. Sorry for any confusion.
Harry James
14,780 PointsSo, it's basically saying: "For each user that exists, check if the user is in the list of their friends. If they are, run the code in the loop." Is that right?
Kristen Law
16,244 PointsYup! You've got the idea down of what's going on in the code.. I'll try to explain the for-each loop in more detail below.
Harry James
14,780 PointsThanks! I appreciate your help by the way :)
rll
4,166 PointsThis is not a for loop, but a for EACH or enhanced for loop. In other languages, you actually spell out foreach instead of for. Since you are using a list TYPE with a ParseUser OBJECT, in Java it comes across as more or less a syntax shortcut that, as Kristen states, does something for each individual frienD in the frienDS list. You usually use it for what are called typed collections, and you can use it on arrays.
Harry James
14,780 PointsOk, what I don't understand now is how friend can be anything.
So, I could have for(ParseUser person : friends) which would mean for each person in friends, run some code? So, everyone?
How does Parse actually know what a "friend" is. It hasn't been declared anywhere, has it?
Sorry if I'm being obnoxious here but, I just completely don't understand what is going on :P
rll
4,166 PointsThe variable "friend" can be anything that is USEFUL to the foreach loop. Your code would work, person is fine. The reason friends is used is because it just makes sense to name it that, and it's probably convention. For instance, each cat in cats, each dog in dogs, or each animal in animals, et al. Parse doesn't know what a friend is, the Java language is processing the content of the list by way of the enhanced for loop. Using enhanced for tells Java you have a COLLECTION of objects.You could write this out to NOT use shorthand, and the result would be the same, but it would be WAY more work.
Harry James
14,780 PointsHarry James
14,780 PointsPerfect! Yes, that explains it great! Thanks for the explanation!