Java Foreach with a condition

后端 未结 6 2038
一个人的身影
一个人的身影 2020-12-18 18:27

Is it possible for Java foreach to have conditions?

For example,

for(Foo foo : foos && try == true)
{
//Do something
}

Is t

6条回答
  •  情歌与酒
    2020-12-18 18:42

    No, foreach is specially designed only for iterating all the elements of an array or collection.

    If you want you can check condition inside it and use break keyword for getting out of loop in middle.

提交回复
热议问题