Thursday, November 15, 2012

Using For-Each Loops with Strings

Someone in class today asked if it was possible to use for-each loops in java with Strings. The answer is "yes," and "no." You can use the for-each loop to parse an array of Strings but you cannot use it to parse as single String. Here is an example of a legal use of a for-each loop for a String array:

String array[] = {"Hi", "There", "My", "Son"};
for(String x : array)
     System.out.print(x);


The above example will print:

HiThereMySon

Mr. Sarkar

No comments:

Post a Comment