-8

I saw the following code on a website but do not know what does this do, when I use it it shows an error.

 for(String next: var)
  • 5
    Only that line of code? What about the rest. You says it "shows an error": what is the error? – fge Jun 10 '13 at 11:41
  • 1
    I _knew_ the answer... But your question is super vague: "when I use it it shows an error" you don't even say how you use it – fge Jun 10 '13 at 11:43
  • 5
    is this really worth being a question on SO? – EvilDuck Jun 10 '13 at 11:46
  • Possible dup of http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work?lq=1 or http://stackoverflow.com/questions/3433282/java-for-each-loop – Shafik Yaghmour Jun 10 '13 at 11:54

6 Answers6

3

Its java for each loop. where var is a reference which implements iterable

An enhanced for loop for the Java™ Programming Language

see:http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

And see how its translates

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    It doesn't have to be a list. It can be any Collection or Array. – NeplatnyUdaj Jun 10 '13 at 11:41
  • 5
    @NeplatnyUdaj it doesn't even have to be a Collection or an array. The only requirement is that the reference implements `Iterable` of some type. That it works on arrays as well is a bonus, as arrays do not implement it. – fge Jun 10 '13 at 11:43
  • Thankyou all.My confusion.Edited. – Suresh Atta Jun 10 '13 at 11:49
2

its called for each loop in Java

for it to work, var should be of type String[] OR Collection<String> which is essentially Iterable<String>

its equivalent of

for(int i=0; i < var.length; i++) {
   String next = var[i];
}

in case of var is String[]

and

for (Iterator<String> itr = var.iterator(); itr.hasNext();)
{
   String next = itr.next();

} 

in case where var is Collection<String>

sanbhat
  • 17,522
  • 6
  • 48
  • 64
2

It's like writing:

for (Iterator<String> itr = var.iterator(); itr.hasNext();)
{
   String str = itr.next();
   ...
} 

See this link for details.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

it will iterate loop till your next variable has values. you can access that value using next variable

shreyansh jogi
  • 2,082
  • 12
  • 20
0

a very interesting question for beginners, this is a foreach loop similar to for loop, it will assign each value of var which is any array of type String to next.

for more information refer this for each loop

Mayank Tiwari
  • 2,974
  • 5
  • 30
  • 52
0

var must be List of String Objects ,

for(Object s:List){} is a For each loop or enhanced for each loop.

It is easy to iterate like this , rather than using

List<String> var=new ArrayList<String>();

for(int i=0;i<var.length();i++)

    {
        String s=var.get(i);   //var is a list of String
        System.out.println(s);

    }

For each is used this way

for(String s:var)
{
   System.out.println(s);   //much easy
}

So to ease up Java allows for each loop to iterate through List through for each.

anshulkatta
  • 2,044
  • 22
  • 30