1.概念:Java容器类类库的用途是保存对象,容器中不能存储基本数据类型,必须是对象(引用数据类型) 2.为什么需要容器:主要是在以数组作为数据的存储结构中,其长度难以扩充,同时数组中元素类型必须相同。而容器可以弥补数组的这两个缺陷 3.容器框架 各接口的特点
4.ArrayList List承诺可以将元素维护在特定的序列中。List接口在Collection的基础上加入了大量的方法,使得可以在List中间可以插入和移除元素 1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.List; 4 import java.util.ListIterator; 5 import java.util.Scanner; 6 7 public class TestArrayList { 8 public static void main(String[] args) { 9 //创建集合对象, 接口 new 实现类 10 List list=new ArrayList(); 11 12 //(1)添加add (Object obj) 13 list.add("hello"); 14 list.add(123);//自动装箱(基本数据类型自动转对应的包装类) 15 list.add(new Scanner(System.in)); 16 17 //(2)集合中元素的个数 size() 18 System.out.println(list.size());//3 19 System.out.println("集合是否为空:" list.isEmpty());//false 20 21 //(3)addAll(Collection c) 22 List list2=new ArrayList(); 23 list2.add("hello"); 24 list2.add(123); 25 list.addAll(list2); 26 System.out.println("list集合中元素的个数:" list.size());//5 27 System.out.println(list); 28 //[hello, 123, java.util.Scanner[...], hello, 123] 29 30 //(4)删除 31 System.out.println("根据对象去删除:"); 32 list.remove("hello"); 33 System.out.println(list);//[123, java.util.Scanner[...], hello, 123] 34 //list.remove(123);//认为123是索引 35 list.remove(new Integer(123)); 36 System.out.println(list);//[java.util.Scanner[...], hello, 123] 37 list.remove(0);//根据索引去删除 38 System.out.println(list);//[hello, 123] 39 list.add("world"); //list [hello,123,world] list2[hello,123] 40 //list.removeAll(list2);//[word] 41 //list.retainAll(list2);//[hello, 123] 42 System.out.println(list);//[hello, 123, world] 43 44 //(5)判断 45 System.out.println("hello在集合中是否存在:" list.contains("hello"));//true 46 System.out.println("java在集合中是否存在:" list.contains("java"));//false 47 48 //(6)清空集合中所有的元素对象 49 System.out.println(list);//[hello, 123, world] 50 System.out.println(list2);//[hello, 123] 51 System.out.println(list.containsAll(list2));//true----list是否包含list2 52 //list.clear(); 53 //System.out.println(list); 54 55 //(7)获取指定索引位置上的元素对象 56 System.out.println(list.get(1));//123 57 58 //(8)设置 59 list.set(1, "java"); 60 System.out.println(list);//[hello, java, world] 61 62 //(9)在指定的索引位置上添加元素对象 63 list.add(1, "html"); 64 System.out.println(list);//[hello, html, java, world] 65 66 //(10)查找元素在集合中的位置 67 System.out.println(list.indexOf("java") "\t" list.indexOf("sql"));//2 -1 68 //(11)遍历集合中元素的内容 69 /**(1)使用加强for循环遍历集合中的元素*/ 70 System.out.println("\n使用加强for循环遍历集合中的元素\n"); 71 for(Object obj:list){ 72 System.out.println(obj); 73 74 } 75 /**(2)使用普通for循环遍历集合中的元素对象*/ 76 System.out.println("\n使用普通for循环遍历集合中的元素对象\n"); 77 for(int i=0;i<list.size();i ){ 78 System.out.println(list.get(i)); 79 } 80 /**(3)使用迭代器*/ 81 System.out.println("\n使用迭代器遍历集合中的元素\n"); 82 Iterator ite=list.iterator(); //正向遍历 83 while(ite.hasNext()){//判断集合中是否有元素对象 84 Object obj=ite.next(); 85 System.out.println(obj); 86 } 87 System.out.println("使用listIterator()遍历"); 88 ListIterator listIte=list.listIterator(); 89 System.out.println("正向遍历"); 90 System.out.println("在集合的开头,后面还有元素对象吗?" listIte.hasNext()); 91 System.out.println("在集合的开头,前面有元素对象吗?" listIte.hasPrevious()); 92 while(listIte.hasNext()){ 93 System.out.println(listIte.next()); 94 } 95 System.out.println("到达集合的末尾,后面还有元素对象吗?" listIte.hasNext()); 96 System.out.println("到达集合的末尾,前面有元素对象吗?" listIte.hasPrevious()); 97 System.out.println("\n逆向遍历集合中的元素\n"); 98 while(listIte.hasPrevious()){ 99 System.out.println(listIte.previous()); 100 } 101 102 } 103 } 来源:http://www./content-1-112201.html |
|