标签:
//导入List接口和ArrayList实现类
import java.util.List;
import java.util.ArrayList;
publicclassTest{
publicstaticvoid main(String args []){
ArrayList<String> arrayList =newArrayList<String>();
//向arrayList当中添加对象
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
//移除对象
arrayList.remove(1);
//输出数组中的对象,调用arrayList.size()可以知道arranList的长度
for(int i =0; i < arrayList.size(); i++){
String s = arrayList.get(i);
System.out.println(s);
}
}
}
//导入接口和实现类,set接口是collection接口的子接口
import java.util.Set;
import java.util.HashSet;
publicclassTest{
publicstaticvoid main(String args []){
//生成一个HashSet对象,再向上转型为Set类型
//HashSet<String> hashSet = new HashSet<String>();
//Set<String> set = HashSet;
//等同于下面这行代码
Set<String>set=newHashSet<String>();
boolean b1 =set.isEmpty();
System.out.println(b1);
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
boolean b2 =set.isEmpty();
System.out.println(b2);
int i =set.size();
System.out.println("clean之前set对象的长度是"+ i);
set.remove("a");
int j =set.size();
System.out.println("remove之后set对象的长度是"+ j);
set.clear();
int k =set.size();
System.out.println("clean之后set对象的长度是"+ k);
}
}
//导入接口和实现类,set接口是collection接口的子接口
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
publicclassTest{
publicstaticvoid main(String args []){
//HashSet<String> hashSet = new HashSet<String>();
//Set<String> set = HashSet;
//Iterator <-- Collection <-- Set <-- HashSet
// <-- List <-- ArrayList
Set<String>set=newHashSet<String>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
//调用Set对象的Iterator方法,会生成一个迭代器对象,该对象用于遍历整个Set
Iterator<String> it =set.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
import java.util.Map;
import java.util.HashMap;
publicclassTest{
publicstaticvoid main(String args []){
HashMap<String,String> hashMap =newHashMap<String,String>();
//向上转型为Map类型
Map<String,String>map= hashMap;
//可以使用Map的put方法,向映射当中存放数据
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
map.put("3","e");
//使用size()方法,来得到map的长度
int i =map.size();
System.out.println(i);
//根据键来取值
String s =map.get("3");
System.out.println(s);
}
}
标签:
原文地址:http://www.cnblogs.com/arroneve/p/5815473.html