foreach keyword in java?

瘦欲@ 提交于 2019-12-30 04:20:34

问题


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.


回答1:


foreach is not a java keyword (IDE recognizes it and puts the "For-each" loop).




回答2:


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.




回答3:


As of Java 1.8, Java now has a foreach loop ...

package org.galarik.arick;

import java.util.ArrayList;
import java.util.List;

public class ExampleForEach {

    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        strings.add("Mom");
        strings.add("Dad");
        strings.add("Dad's Java");
        strings.add("Mom's Java");

        // Original for loop
        int stringsSize = strings.size();
        Object[] oldStrings = strings.toArray();
        for(int stringIndex = 0; stringIndex < stringsSize; ++stringIndex ) {
            System.out.println("Original Content: " + oldStrings[stringIndex]);
        }

        // For loop, as of Jova 1.5
        for (String string : strings) {
            System.out.println("All Content: " + string);
        }

        // forEach loop as of Java 1.8
        strings.stream().forEach((string) -> {
            System.out.println("For Content: " + string);
        });

        // Using forEach loop to do a filter
        strings.parallelStream().filter(someString -> someString.contains("Java"))
                .forEach((someOtherString) -> {
                    System.out.println("Content With Java: " + someOtherString);
                });
    }
}



回答4:


No. To do a "foreach", you write for( Type variable : collection ){ .... }

(Yes, I know it doesn't have to be a Collection, just an Iterable or an array - I'm just trying to write clearly)




回答5:


No, There is no keyword like foreach. And just wanted to add that enhanced for loop was added just for coding convenience. Compiler puts it back to old java format. Like:

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

Will be converted to:

    List list = new ArrayList();
    String str;
    for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(str))
    {
        str = (String)iterator.next();
    }



回答6:


NO. foreach is not a keyword in Java.

The foreach loop is a simple syntax for iterating through arrays or collections—implementing the java.lang.Iterable interface.

In Java language, the for keyword and the : operator are used to create a foreach loop.

// Java example
String[] oddBalls = {"one", "three", "five", "seven"};

for (String currentBall : oddBalls)
{
   System.out.println (currentBall + " is an odd number.");
}

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 foreach.



来源:https://stackoverflow.com/questions/9478273/foreach-keyword-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!