What is the syntax of the enhanced for loop in Java?

Enhanced for loop: for (String element : array) { // rest of code handling current element } Traditional for loop equivalent: for (int i=0; i < array.length; i++) { String element = array[i]; // rest of code handling current element } Take a look at these forums: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

How does Q_FOREACH (= foreach) macro work and why is it that complex?

The GCC version The GCC one is really quite simple. First of all it is used like this: Q_FOREACH(x, cont) { // do stuff } And that will be expanded to for (QForeachContainer<__typeof__(cont)> _container_(cont); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (x = *_container_.i;; __extension__ ({–_container_.brk; break;})) { // do stuff … Read more

foreach loop with a where clause

Yes, it is possible: Method Syntax: foreach (var person in people.Where(n => n.sex == “male”)) { } Or the rather lengthy Query Syntax: foreach (var person in from person in people where person.sex == “male” select person)

foreach in kotlin

For other Kotlin newbees like me who are coming here just wanting to know how to loop through a collection, I found this in the documentation: val names = listOf(“Anne”, “Peter”, “Jeff”) for (name in names) { println(name) }

Terraform failing with Invalid for_each argument / The given “for_each” argument value is unsuitable

Explanation This error is often caused by passing a list to for_each, but for_each only works with unordered data-types, i.e. with sets and maps. Solution The resolution depends on the situation. List of strings If the list is just a list of strings, the easiest fix is to add a toset()-call to transform the list … Read more

Looping through Regex Matches

class Program { static void Main(string[] args) { string sourceString = @”<box><3> <table><1> <chair><8>”; Regex ItemRegex = new Regex(@”<(?<item>\w+?)><(?<count>\d+?)>”, RegexOptions.Compiled); foreach (Match ItemMatch in ItemRegex.Matches(sourceString)) { Console.WriteLine(ItemMatch); } Console.ReadLine(); } } Returns 3 matches for me. Your problem must be elsewhere.

In detail, how does the ‘for each’ loop work in Java?

for (Iterator<String> i = someIterable.iterator(); i.hasNext();) { String item = i.next(); System.out.println(item); } Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred. As was noted by Denis Bueno, this … Read more