import?static?java.lang.System.out;//靜態(tài)導(dǎo)入
import?java.util.HashMap;
import?java.util.Iterator;
import?java.util.LinkedHashMap;
import?java.util.List;
import?java.util.ArrayList;
import?java.util.Map;
import?java.util.Map.Entry;
import?java.util.Set;
import?org.junit.Test;
public?class?Demo1?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
}
@Test
public?void?test1(){
int?arr[]={1,2,3,4};
for(int?i:arr){
System.out.print(i+"?");
}
}
@Test
public?void?test2(){
//傳統(tǒng)方式1,通過key取出value
Map?mp=new?HashMap();?//無序輸出
mp=new?LinkedHashMap();?//正序存入正序輸出
mp.put("1",?"a");
mp.put("2",?"b");
mp.put("3",?"c");
Set?set=mp.keySet();
Iterator?it=set.iterator();
while(it.hasNext()){
String?key=(String)it.next();
String?value=(String)mp.get(key);
System.out.println(key+"="+value);
}
}
@Test
public?void?test3(){
//傳統(tǒng)方式2,通過?鍵值對?去取出
Map?mp=new?HashMap();?//無序輸出
mp=new?LinkedHashMap();?//正序存入正序輸出
mp.put("1",?"a");
mp.put("2",?"b");
mp.put("3",?"c");
Set?set=mp.entrySet();
Iterator?it=set.iterator();
while(it.hasNext()){
Map.Entry?entry=(Entry)it.next();
String?key=(String)?entry.getKey();
String?value=(String)?entry.getValue();
System.out.println(key+"="+value);
}
}
@Test
public?void?test4(){
Map?mp=new?HashMap();?//無序輸出
mp=new?LinkedHashMap();?//正序存入正序輸出
mp.put("1",?"a");
mp.put("2",?"b");
mp.put("3",?"c");
//增強(qiáng)for循環(huán)取map的第一種方式(Map不能迭代)
for(Object?obj:mp.keySet()){
String?key=(String)obj;
String?value=(String)mp.get(key);
System.out.println(key+"="+value);
}
}
@Test
public?void?test5(){
Map?mp=new?HashMap();?//無序輸出
mp=new?LinkedHashMap();?//正序存入正序輸出
mp.put("1",?"a");
mp.put("2",?"b");
mp.put("3",?"c");
//增強(qiáng)for循環(huán)取map的第二種方式(Map不能迭代)
for(Object?obj:mp.entrySet()){
Map.Entry?entry=(Entry)obj;
String?key=(String)entry.getKey();
String?value=(String)entry.getValue();
System.out.println(key+"="+value);
}
}
}