java 入土--集合詳解

java 集合集合是對象的容器,實現了對對象的常用的操作 , 類似數組功能 。和數組的區別:

  • 數組長度固定,集合長度不固定
  • 數組可以存儲基本類型和引用類型,集合只能存儲引用類型
  • 使用時需要導入類
Collection 接口
java 入土--集合詳解

文章插圖
Collection 繼承 Iterable 接口,實現子類可放多個 object 元素,Collection 接口無直接實現類,是通過子類接口 Set 與 List 實現 。是單列集合的頂層實現接口,他表示一組對象,這些對象也稱 Collectioin 的元素 。JDK 不提供此接口的任意直接實現,他提供更具體的子接口(如 Set 和 List)的實現 。
Listlist 集合是 collection 的子接口,元素有序,且添加順序與取出順序一致,支持索引 。
List 是一個接口,繼承自 Collection 接口 。
List 是一個有序集合,用戶可以精確的控制列表中的每一個元素的插入位置 。可以通過整數索引訪問元素,并搜索列表中的元素 。與 Set 集合不同,列表通常允許重復元素 。List 集合特點:
  • 有序
  • 可重復
List 集合的特有方法:
  • add(index,E)指定位置添加元素
  • remove(index)刪除指定位置的元素
  • set(index,E)修改指定位置的元素
  • get(index)獲取指定位置的元素
  • List 集合的幾種常用方法
List<> list = new ArraryList();list.add(obj)//末尾添加元素list.add(index,obj)//在指定位置添加元素list.remove(obj)//刪除元素list.indexof(obj)//返回第一次出現的位置list.lastIndexOf(obj)//返回最后出現的位置list.set(indes,obj)//設置指定位置的元素,相當于替換list.get(index)//返回指定位置的元素list.contains(obj)//查找當前元素是否存在 。list.size()//返回list的長度list.clear()//清空集合list.isEmpty()//判斷是否為空list.removeAll(list)//刪除多個元素 。ArraylistArrarylist 的底層維護了一個 Object 類型的 elementData 數組 , 其底層就是一個數組 。
ArrayList 集合:可調整大小的數組的實現 List 接口 。實現所有可選列表操作,并允許所有元素,包括 null。
底層是數組 ,  查詢快,增刪慢于 LinkedList 集合
  • 無參構造擴容若使用無參構造,初始的 elementData 為 0,第一次添加元素,擴容為 10,若再次擴容,則擴容為 elementData 的 1.5 倍 。0->10->15->22....
  • 有參構造擴容若使用 new Arrarylist(int n)初始化數組,則為指定大小 , 擴容時也是按照 elementData 的 1.5 倍
VectorVector 是 List 的子類,繼承 AbstractList 接口,實現 List 接口,底層也是一個 Object[] elementData 數組 。
Vector 是線程同步的,是線程安全的,開發中若需要線程同步,用 Vextor 集合 。
LinkedListLinked 的意思是鏈接,字面意思來看,該集合是一個鏈表,事實也正是如此,LinkedList 的底層是實現了雙向鏈表和雙端隊列 ??梢蕴砑尤我獾闹貜驮兀?null 。由于是鏈表,所以不需要擴容,增刪效率高 。
//鏈表實現演示public class Node {private Object item;//對象public Node first;public Node last;public Node(Object name) {this.item = name;}}LinkedList 集合特有功能
java 入土--集合詳解

文章插圖
LinkedList<String> link = new LinkedList<String>();link.add("hello");link.add("world");link.add("hello world");//添加元素link.addFirst("fist");link.addLast("last");System.out.println(link);System.out.println("------------------");//獲取元素System.out.println(link.getFirst());System.out.println(link.getLast());System.out.println("------------------");//刪除元素link.removeFirst();link.removeLast();System.out.println(link);System.out.println("------------------");List 的最常用的三種遍歷方法
//以ArraryList為例 public static void main(String[] args) {List list = new ArrayList();List list = new ArrayList();list.add("11");list.add("12");//普通for循環for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}//增強forfor (Object o : list) {System.out.println(o.toString());}//iterator循環//通過列表迭代器添加元素,不會出現并發修改異常Iterator it = list.iterator();while (it.hasNext()) {System.out.println(it.next());} }實例:將 Student 對象添加到集合,三種方式遍歷
Student student = new Student("張三",18);Student student2 = new Student("wangjiaqi",13);ArrayList<Student> array = new ArrayList<Student>();array.add(student);array.add(student2);//增強forfor (Student students : array) {System.out.println(students.getName()+students.getAge());}//迭代器Iterator<Student> it = array.iterator();while (it.hasNext()) {Student students = it.next();System.out.println(students.getName()+students.getAge());}//普通forfor(int i = 0; i < array.size(); i++){Student students = array.get(i);System.out.println(students.getName()+students.getAge());}

推薦閱讀