Java_84_数组遍历_for-each

package Test;
/**
 * 数组遍历
 * 数组元素下标的合法区间[0,length-1]
 * 我们可以通过下标来遍历数组中的元素,遍历时可以读取元素的值或者修改元素的值。
 * for-each循环
 * for(type name:arrayName){}
 * 增强for循环for-each是JDK1.5新增加的功能,专门用于读取数组或集合中所有的元素,即对数组进行遍历。
 * @author pmc
 *
 */
public class ArrayEach {
	public static void main(String[] args){
		int[] a=new int[4];
		/**
		 * 初始化数组元素的值
		 */
		for(int i=0;i<a.length;i++){
			a[i]=100*i;
		}
		/**
		 * 读取元素的值
		 */
		for(int i=0;i<a.length;i++){
			System.out.println(a[i]);
		}
		/**
		 * for-each循环,读取数组元素的值,不能修改元素的值
		 */
		for(int temp:a){
			System.out.println(temp);
		}
	}
}
发布了136 篇原创文章 · 获赞 11 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/pmcasp/article/details/104988096