1.集合的常用框架
根据数据结构划分:
Collection
list set
ArraryList ,LinkList,Vector TreeSet,HashSet
2.集合的存储
都是存储的对象的引用。
二:程序--Collection的公共方法
1.Collection的公共方法(常用)
增删查该
1 import java.util.ArrayList; 2 3 public class Test39 { 4 5 public static void pubTest(){ 6 ArrayList a1=new ArrayList(); 7 a1.add("java1"); 8 a1.add("java2"); 9 a1.add("java3"); 10 System.out.println(a1); 11 System.out.println(a1.size()); 12 System.out.println(a1.isEmpty()); 13 a1.remove("java1"); 14 System.out.println(a1); 15 System.out.println(a1.contains("java3")); 16 } 17 public static void pubTest2(){ 18 ArrayList a1=new ArrayList(); 19 a1.add("java1"); 20 a1.add("java2"); 21 a1.add("java3"); 22 ArrayList a2=new ArrayList(); 23 a2.add("java1"); 24 a2.add("java2"); 25 a2.add("java4"); 26 //a1.retainAll(a2); 27 //System.out.println(a1);//java1,java2 28 a1.removeAll(a2); 29 System.out.println(a1);//java3 30 31 } 32 public static void main(String[] args) { 33 //pubTest(); 34 pubTest2(); 35 } 36 }
2.Collection的常用方法(迭代器)
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 4 public class Test40 { 5 6 public static void main(String[] args) { 7 ArrayList a1=new ArrayList(); 8 a1.add("java1"); 9 a1.add("java2"); 10 a1.add("java3"); 11 Iterator it=a1.iterator(); 12 while(it.hasNext()){ 13 System.out.println(it.next()); 14 } 15 16 for(Iterator i=a1.iterator();i.hasNext();){ 17 System.out.println(i.next()); 18 } 19 } 20 21 }
三:程序--List
1.ArrayList 增删该查方法
add(index,E)
addAll(index,E)
remove(index);
set(index,E)
get(index);
iterator();
subList(head,tail);
2.程序
1 import java.util.ArrayList; 2 3 public class Test41 { 4 5 public static void main(String[] args) { 6 ArrayList a2=new ArrayList(); 7 a2.add(0, "java0"); 8 a2.add(1, "java1"); 9 a2.add(2, "java2"); 10 a2.add(3, "java3"); 11 System.out.println(a2); 12 a2.remove(3); 13 System.out.println(a2); 14 a2.set(2, "java02"); 15 System.out.println(a2); 16 System.out.println(a2.get(0)); 17 System.out.println(a2.subList(0, 2)); 19 } 20 }
3.附加的方法
indexOf(E)
4.程序
1 public static void main(String[] args){ 2 ArrayList a2=new ArrayList(); 3 a2.add(0, "java0"); 4 a2.add(1, "java1"); 5 a2.add(2, "java2"); 6 System.out.println(a2.indexOf("java1")); 7 }
5.程序--ArrayList特有的迭代器listIterator
1 import java.util.ArrayList; 2 import java.util.ListIterator; 3 4 public class Test42 { 5 6 public static void main(String[] args) { 7 listT(); 8 } 9 10 private static void listT() { 11 ArrayList a2=new ArrayList(); 12 a2.add(0, "java0"); 13 a2.add(1, "java1"); 14 a2.add(2, "java2"); 15 a2.add(3, "java3"); 16 //System.out.println(a2); 17 ListIterator lt=a2.listIterator(); 18 while(lt.hasNext()){ 19 System.out.print(lt.next()); 20 } 21 System.out.println(); 22 while(lt.hasPrevious()){ 23 System.out.print(lt.previous()); 24 } 25 System.out.println(); 26 while(lt.hasNext()){ 27 Object obj=lt.next(); 28 if(obj.equals("java2")){ 29 lt.remove(); 30 } 31 } 32 System.out.println(a2); 33 } 34 35 }
四:程序--Vector
1.
时间: 2024-10-13 10:48:01