for循环语句的用法

1、for(int i : index){}用法【又称是foreach用法】:

  比如:

public class Test {
	public static void main(String[] args) {
		//输出结果
		int[] fdx = new int[]{1,8,3,7,9,2,4};
		int[] index = new int[]{0,1,2,0,3,1,4,2,2,5,6};
		String tel = "";
		for(int i :index){
			tel +=fdx[i];
		}
		System.out.println("tel:"+tel);
	}
}

  用法:数组遍历输出,for(int i : index){}---->表示遍历输出index数组中的值,并赋值给了i,然后依次i的值就是0,1,2,0,3,1,4,2,2,5,6,于是才有了下面字符串的拼接;

2、for(int i=0;i<10;i++){}的用法:(表达式;boolean;表达式)

  比如:

public class Test {
	public static void main(String[] args) {
		String tel ="";
		int arr[] = new int[]{1,8,3,1,7,8,9,3,3,2,4};
		for(int i=0;i<11;i++){
			tel +=arr[i];
		}
		System.out.println("tel:"+tel);
	}
}

  用法:给定的初始值,然后给定的循环次数,还有依次变化的频率,然后再依次输出需要的结果即可;

  

posted @ 2016-06-22 15:27  FanSunny  阅读(720)  评论(0编辑  收藏  举报