Foreach是for语句的特殊简化版本,但任何时候的foreach语句都可以改写成for语句。Foreach语句在遍历数组等方面为程序员提供了很大的方便

语法如下:

For(元素变量 x:遍历对象obj){

    引用了x的java语句

}

实例:

package Foreach1;

public class Foreach1 {

    public static void main(String[] args) {

        int [] arr={1,3,5,7,9,11,13,15};    //声明一维数组

        System.out.println("一维数组中的数为:");

        for(int x:arr){    //int x引用的变量,arr制定要循环遍历的数组,最后输出x值

            System.out.print(x+" ");

        }

    }

}

相关文章:

  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2019-03-24
  • 2021-09-09
  • 2021-08-05
  • 2021-09-09
猜你喜欢
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2021-08-08
相关资源
相似解决方案