Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

for loop - foreach keyword in java?

I know the meaning of foreach in programming and when to use it. Is there a foreach keyword in Java? I tried to find a list of keywords but there is only for and not foreach.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To my knowledge, Java does not have a foreach keyword (unlike C# if I am not mistaken). You can however, iterate over all the elements in an Iterable collection using a modified version of the for loop:

List<String> list = new ArrayList<String>();
...
for (String str : list)
{
    System.out.println(str);
}

Check this link for more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...