Java类集-list
Collection 子接口:
ArrayList是List 接口和Collection接口的一个子类,用于实例化两种接口
package leiji;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
public class ArryList {
public static void main(String args[]){
List<String> arrlt=new ArrayList<String>();
Collection<String> acoll=new ArrayList<String>();
arrlt.add("Hello");
arrlt.add(0,"World");
System.out.println(arrlt);
//添加元素
acoll.add("jerry");
acoll.add("weibo");
//添加对象
arrlt.addAll(acoll);
arrlt.addAll(0,acoll);
System.out.println(arrlt);
//移除元素
arrlt.remove(1);
arrlt.remove("weibo");
System.out.println(arrlt);
//获取元素
for(int i=0;i<=arrlt.size()-1;i++){
//System.out.println(arrlt.get(i));
}
//String a[] =new String[]{"1","2"};
//将集合变为对象数组
String str[]=arrlt.toArray(new String[]{}); //
for(int i=0;i<=str.length-1;i++){
//System.out.println(str[i]);
}
Object obj[]=arrlt.toArray();
for(int i=0;i<=obj.length-1;i++){
System.out.println((String)obj[i]);
}
//推断是否为空
System.out.println(arrlt.isEmpty());
//推断是否包括某一个元素
System.out.println(arrlt.contains("Hello")?"Hello is contains":"Hello is not contains");
//截取子集合
List l=arrlt.subList(1, 4);
for(int i=0;i<l.size();i++){
System.out.println(l.get(i));
}
System.out.println(l.indexOf("Hello"));
}
}