首页 > 其他分享 >增强for循环

增强for循环

时间:2023-02-14 21:57:16浏览次数:32  
标签:语句 增强 int 循环 数组 声明 public

增强for循环

  • 数组重点使用

  • java5引入了一种主要用于数组或集合的增强型for循环。

  • 格式:

    for(声明语句 : 表达式){
        //代码句子
    }
    
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

  • 表达式是要访问的数组名,或者是返回为数组的方法。

package com.earl.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义一个数组

        for(int i=0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("==========");
        for(int x:numbers){
            System.out.println(x);
        }
    }
}

标签:语句,增强,int,循环,数组,声明,public
From: https://www.cnblogs.com/blogearl/p/17120997.html

相关文章