0

I am working in programming for last couples on years, but today I saw a new thing that I searched on net but can't find the perfect answer. There is a code which says

for(String string : mCha) // line 1
{
// loop working
}

I want to ask that what does the line 1 mean? As I have studied that there should be 3 parameters in for loop and if we don't want to give that than the following method should follow

for(;;)
{
}

Please explain me as I am totally new to this syntax and I searched on net but can't find any thing useful.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
Najam-us-Saqib
  • 526
  • 3
  • 10
  • 23
  • 2
    Also, see http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html – NPE Dec 24 '13 at 10:49
  • This syntax is defined in [The enhanced `for` statement](http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.14.2) section of the specification. – McDowell Dec 24 '13 at 10:54
  • As a note, the `for-each` does not only work on arrays but on anything implementing [`Iterable`](http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html), which includes all of Java's [`Collection`s](http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html). – JimmyB Dec 24 '13 at 11:01

7 Answers7

9
for(String string : mCha) // line 1
{
  // loop working
}

Is a shorthand for :

for (Iterator<String> i = mCha.iterator(); i.hasNext(); ) {
  String string = (String)i.next();
  //loop working
}

(provided that mCha implements Iterable, arrays are handled differently, see the JLS about the enchanced for statement)

Example :

for(String string : mCha) // line 1
{
  System.out.ptinln(string) // For each element of my collection, display it.
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
2

I want to ask that what does the line 1 mean?

That is a for-each loop in java. It is equivalent to

Iterator<String> it = mCha.iterator();
while(it.hasNext()){
    String string = it.next();
    // loop working
}

The for-each loop was introduced in java 1.5. See The For-Each Loop for details.

René Link
  • 48,224
  • 13
  • 108
  • 140
1

It is called an enhanced for loop. It was introduced in Java 5. It is similar in its working to the old for loop which uses an index. Both can be used to iterate over collections and arrays.

Old for loop : With this format of the for loop, you have access to the index variable and also can skip index as you wish.

for(int i=0;i<size;i++){
   //code
}

Enhanced for loop : This format is quite inflexible as compared to the old for loop. This format is to be used only when you need to iterate through each and every element of the collection and you have no need of knowing the index of any particular element in the collection.

 for(String string : mCha) // This means that "For each String element in the collection/array mCha"
 {
    //code
 }

Read more about enhanced for loops here

Adarsh
  • 3,613
  • 2
  • 21
  • 37
0
for(String string : mCha) // line 1
{
 // loop working 
}

here mCha is a collection could be array, list or anything and the for loop (this is a special case of for loop) above is scanning each string from starting through end of the array or the list

manish
  • 1,450
  • 8
  • 13
0

The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each (because it is called this in other programming languages). I've also heard it called the for-in loop.

Simple example to explain for each loop

class ForEachExample1{  
   public static void main(String args[]){  
   int arr[]={12,13,14,44};  

   for(int i:arr){  
     System.out.println(i);  
   }    
 }   
}  

also see The For-Each Loop

rachana
  • 3,344
  • 7
  • 30
  • 49
0

See this example....

List v = new ArrayList();
v.add("aa");
v.add("bb");
v.add("cc");
for (Object str : v) {
   System.out.println(str.toString());
}

It will give the output

aa
bb
cc


for (Object str : v) it travel on the collection of objects.

Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
  • forget about `Vector`, use `List`: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – zapl Dec 24 '13 at 11:14
0
This is known as for-eachloop introduced in java5.
for(String string : mCha) // line 1
{
// loop working
}
It is used to iterate with both collections and arrays same as normal for loop

Syntax:

for(data_type variable : array | collection){} 

Example:

    int[] array = {1,3,6};
    int sum = 0;
    for (int i : array) {  
        sum += i;
    }
    System.out.println("Total sum":+sum);

I hope it is clear