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 trialGrigorij Schleifer
10,365 PointsFor each loops are killing me !
Can someone pleas explain me this foe each loop.
I have real pain to understand them...
String<Set> propNames = System.getProperties().stringPropertieNames();
for (String propertyName : propNames) {//why do we use propNames here? what is this line doing???
System.out.printf("%s is %s %n",
propertyName,
System.getPropertie(propertyName));//what is going on here??? Why we put propertyName inside?
}
2 Answers
Steve Hunter
57,712 PointsHi there,
This isn't my strongest part of coding but I'll have a go!
for (String propertyName : propNames)
This line is setting up the loop. The propNames
bit is the name of the set, defined in the first line.
The loop is iterating through every property in the set of propNames
and at each pass, storing the name in the String being defined here, called propertyName
.
Then, as each new property is contained in the propertyName
variable, the output is contructed that says what the property is, then what state it is in.That's this line:
System.out.printf("%s is %s %n",
propertyName,
System.getProperty(propertyName));
Something like "Operating System is Windows" or "RAM is 8GB" and stuff like that.
I hope that makes some sense!
Steve.
Grigorij Schleifer
10,365 PointsThanks Steve, now I understand :)
But when I define the for each. loop In the case above we defined a String. So I always should know the type of the object that the loop i going through? If I want to loop through an "int[] intArray" array I should say:
for (int loopThougIntArray: intArray) {
System.out.printf(loopThougIntArray);
}
Right?
Grigorij
Steve Hunter
57,712 PointsYep! That declares a local variable to hold each element of the array. :-)
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHi Steve,
thanks for quick response:
The loop is iterating through every property in the set of propNames and at each pass, storing the name in the String being defined here, called propertyName.
So the loop goes through every single set member in propNames (first, second, third .....) and storing the name of the property in the String propertyName , am I right? After it the System prints the name of the property and Info regarding the property?
Grigorij
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi,
Yes, you're on the right track. The line
System.getProperties().stringPropertyNames()
brings back a whole list (set) of different properties such as the computer name, operating system name, OS version ... loads of different things. They are all stored inpropNames
- a big long list of names.The loop goes through each of those one by one. At each pass, the variable
propertyName
takes the next property name from the list, 1st, 2nd, and so on. ust like you said.The method
System.getProperty(propertyName)
takes that property and asks for it's value or status. Like the property of a lightbulb can be on or off. The OS can be Windows, OS version could be Windows 7, or whatever.The
for
loop goes through each property in the list until it is finished. Each one gets its name and status printed out by the code. At each pass, the value stored inpropertyName
is only there for that oe pass, it then gets replaced.Make sense now?
Steve.