java的foreach简单理解

    网上有关于foreach用法的详细介绍,小白并看不懂,自己简单理解了一下遍历数组时候的用法:
package foreach;

public class foreach {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		foreach exam = new foreach();
		exam.test01();
		exam.test02();
		exam.test03();
	}
	public void test01(){
		int arr1[]={1,2,3,4,5};
		for(int x : arr1){
			System.out.print(x+" ");    //按顺序输出arr1数组的数值
		}
		System.out.println();
	}
	public void test02(){
		int arr2[][]={{1,2},{3,4},{5,6},{6,7},{8,9}};
		for (int y[] : arr2){
			System.out.print(y[0]+" "); //相当于遍历5个一维数组的第一个数
		}
		System.out.println();
	}
	public void test03(){
		int arr3[][]={{1,2},{3,4},{5,6},{6,7},{8,9}};
		for (int y : arr3[0]){
			System.out.print(y +" ");   //只输出第一个数组数字
		}
		System.out.println();
	}
}
  

结果为

1 2 3 4 5 
1 3 5 6 8 
1 2 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值