1.概念:Java容器類類庫的用途是保存對象,容器中不能存儲(chǔ)基本數(shù)據(jù)類型,必須是對象(引用數(shù)據(jù)類型) 2.為什么需要容器:主要是在以數(shù)組作為數(shù)據(jù)的存儲(chǔ)結(jié)構(gòu)中,其長度難以擴(kuò)充,同時(shí)數(shù)組中元素類型必須相同。而容器可以彌補(bǔ)數(shù)組的這兩個(gè)缺陷 3.容器框架 各接口的特點(diǎn)
4.ArrayList List承諾可以將元素維護(hù)在特定的序列中。List接口在Collection的基礎(chǔ)上加入了大量的方法,使得可以在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 //創(chuàng)建集合對象, 接口 new 實(shí)現(xiàn)類 10 List list=new ArrayList(); 11 12 //(1)添加add (Object obj) 13 list.add("hello"); 14 list.add(123);//自動(dòng)裝箱(基本數(shù)據(jù)類型自動(dòng)轉(zhuǎn)對應(yīng)的包裝類) 15 list.add(new Scanner(System.in)); 16 17 //(2)集合中元素的個(gè)數(shù) 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集合中元素的個(gè)數(shù):" list.size());//5 27 System.out.println(list); 28 //[hello, 123, java.util.Scanner[...], hello, 123] 29 30 //(4)刪除 31 System.out.println("根據(jù)對象去刪除:"); 32 list.remove("hello"); 33 System.out.println(list);//[123, java.util.Scanner[...], hello, 123] 34 //list.remove(123);//認(rèn)為123是索引 35 list.remove(new Integer(123)); 36 System.out.println(list);//[java.util.Scanner[...], hello, 123] 37 list.remove(0);//根據(jù)索引去刪除 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)設(shè)置 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)遍歷集合中元素的內(nèi)容 69 /**(1)使用加強(qiáng)for循環(huán)遍歷集合中的元素*/ 70 System.out.println("\n使用加強(qiáng)for循環(huán)遍歷集合中的元素\n"); 71 for(Object obj:list){ 72 System.out.println(obj); 73 74 } 75 /**(2)使用普通for循環(huán)遍歷集合中的元素對象*/ 76 System.out.println("\n使用普通for循環(huán)遍歷集合中的元素對象\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("到達(dá)集合的末尾,后面還有元素對象嗎?" listIte.hasNext()); 96 System.out.println("到達(dá)集合的末尾,前面有元素對象嗎?" 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 |
|