标签:javawb学习01
增强for循环:package Day01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;
//文件复制
public class Demo1 {
//
@Test
public void main1() {
int[] is = {1,2,3,4,5,6};
for (int i = 0;i < is.length; i++)
{
System.out.println(is[i]);
}
}
//遍历内容使用
@Test
public void test1()
{
int[] is = {1,2,3,4,5};
for(int value : is){
System.out.print(value+"\t");
}
}
//传统方式
@Test
public void testSet()
{
Set<Integer> set = new HashSet<Integer>();
set.add(100);
set.add(200);
set.add(300);
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
Integer key = it.next();
System.out.print(key+"\t");
}
}
@Test
public void testlist()
{
List<String> list = new ArrayList<String>();
list.add("jack");
list.add("marry");
list.add("sisi");
for(String name : list){
System.out.print(name+"\t");
}
}
//传统方法
@Test
public void testMap1(){
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(100,"jack");
map.put(200,"marry");
map.put(300,"sisi");
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
Integer key = it.next();
String value = map.get(key);
System.out.println(key+"-"+value);
}
}
@Test
public void testMap2(){
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(100,"jack");
map.put(200,"marry");
map.put(300,"sisi");
Set<Entry<Integer,String>> set = map.entrySet();
Iterator<Entry<Integer,String>> it = set.iterator();
while(it.hasNext()){
Entry<Integer,String> entry = it.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"<->"+value);
}
}
}
标签:javawb学习01
原文地址:http://blog.51cto.com/357712148/2103586