学习JAVA增强FOR循环

import java.util.ArrayList;
import java.util.Collection;

import org.junit.Test;

import com.sun.org.apache.xpath.internal.operations.String;

public class ForEach {

	@Test
	public void Test1() {
		Collection coll1 = new ArrayList();
		//add是添加元素
		coll1.add("lrq");
		coll1.add(111);
		coll1.add("mmm");
		//for(集合对象 局部变量 : 集合对象)
		for(Object obj: coll1) {
			System.out.println(obj);
		}
		
		}
	//面试题
	@Test
	public void Test2() {
		String arr1[] = new String[] {"mm","mm","mm"};
		
		//方式一 普通FOR循环 输出的是mm
		for(int i = 0; i<arr1.length; i++) {
			arr1[i] = "GG";
			
		}
		//方式二 增强for循环 输出的是GG
		//【注】 因为增强for循环是重新定义了一个S
		for(String S : arr1) {
			S = "GG";
		}
		for(int i= 0 ; i < arr1.length; i++) {
			System.out.println(arr1[i]);
		}
	}
	
}

发布了29 篇原创文章 · 获赞 3 · 访问量 862

猜你喜欢

转载自blog.csdn.net/My_name_PeterLiu/article/details/104118547