一、介绍
ArrayList是实现List接口的动态数组,注意,ArrayList实现不是同步的。如果多个线程同时访问一个ArrayList实例,而其中至少一个线程从结构上修改了列表,那么它必须保持外部同步。
二、操作
初始化:
import java.util.ArrayList;
ArrayList<String> webList=new ArrayList<String>();
存:
webList.add("a",);
webList.addAll(otherList); //将两个list连起来
取:
webList.get(0);
是否存在:
webList.contains("a");
是否为空:
webList.isEmpty();
大小:
webList.size();
清空:
webList.clear();
删除:
webList.remove("a");//删对象
webList..remove(1);//根据index删
将数组转成ArrayList:
ArrayList<String> temp =new ArrayList<String>(Arrays.asList( kmean.split(" ") ) );
三、排序、遍历
排序:
//对加入的近邻进行排序,并提取前面的k个
Collections.sort(kmean,new SortByVal());publicstaticclassSortByValimplementsComparator{publicintcompare(Object o1, Object o2){
GenericPair<String,Integer> k1 = (GenericPair<String,Integer>) o1;
GenericPair<String,Integer> k2 = (GenericPair<String,Integer>) o2;//升序if(k1.getSecond()>k2.getSecond()){return1;
}else{return -1;//一定返回1的相反数-1
}
}
}
List<Map.Entry<String,Integer>> top=new ArrayList<Map.Entry<String,Integer>>(count.entrySet());
Collections.sort(top,new Comparator<Map.Entry<String,Integer>>() {public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2) {//降序return o2.getValue().compareTo(o1.getValue());
}
});
遍历:
for(String item:temp){
}for(inti=index;i<dataset.size();i++){
dataset.get(i);
}