Java ArrayList 集合

Java ArrayList 集合

ArrayList

  • 繼承自 AbstractList ,實現了 IterableCollectionList 接口。

  • 內部類 ListItr 實現了 ListIterator 接口。

  • ArrayList 可以使用 IteratorListIterator 兩種迭代器訪問元素

  • ArrayList 底層是數組結構,可以使用索引直接訪問集合中的元素,調用 get()set() 方法即可。

  • JDK 1.2 版本開始。

  • ArrayList 集合內元素可以重複。

  • 特點 : 增刪慢,改查快。 ArrayList 是線程不安全的

方法

  • 示例

    ArrayList<Integer> list = new ArrayList<>();
    
    // 將指定的元素添加到此列表的尾部
    list.add(10);
    list.add(20);
    System.out.println("add(E e) :" + list);
    
    // 將指定的元素插入此列表中的指定位置
    list.add(1, 30);
    System.out.println("add(int index, E e) :" + list);
    
    // 將指定的Collection集合添加到此列表的尾部
    ArrayList<Integer> newList = new ArrayList<>();
    newList.add(1);
    newList.add(2);
    
    list.addAll(newList);
    System.out.println("addAll(Collection<? extends E> c) :" + list);
    
    // 將指定的Collection集合插入到此列表的指定位置
    newList = new ArrayList<>();
    newList.add(3);
    newList.add(4);
    
    list.addAll(1, newList);
    System.out.println("addAll(int index, Collection<? extends E> c) :"
            + list);
    
    // 判斷集合列表中是否包含指定的元素
    boolean result = list.contains(10);
    System.out.println("contains(Object o) :" + result);
    
    // 返回集合列表中指定位置的元素
    System.out.println("get(int index) :" + list.get(4));
    
    // 返回此列表中首次出現的指定元素的索引,或如果此列表不包含元素,則返回 -1
    int index = list.indexOf(3);
    System.out.println("indexOf(Object o) :" + index);
    
    // 判斷集合列表是否爲空
    System.out.println("isEmpty() :" + list.isEmpty());
    
    // 返回此列表中最後一次出現的指定元素的索引,或如果此列表不包含索引,則返回 -1
    index = list.lastIndexOf(20);
    System.out.println("lastIndexOf(Object o) :" + index);
    
    // 移除集合列表中指定位置的元素,返回被移除的元素
    Integer i = list.remove(1);
    System.out.println("remove(int index) :" + list + " return:" + i);
    
    // 移除此列表中首次出現的指定元素(如果存在)
    result = list.remove(Integer.valueOf(4));
    System.out.println("remove(Object o) :" + list + " return:" + result);
    
    // 用指定的元素替代此列表中指定位置上的元素,返回被覆蓋的元素
    i = list.set(3, 40);
    System.out.println("set(int index, E e) :" + list + " return:" + i);
    
    // 獲得集合列表中的元素數量
    int size = list.size();
    System.out.println("size() :" + size);
    
    // 將集合轉成數組
    Integer[] ints = list.toArray(new Integer[list.size()]);
    System.out.println("toArray(E[] e) :" + Arrays.toString(ints));
  • 運行結果
    運行結果

遍歷元素

  • for 循環

    • 示例

      ArrayList<Character> list = new ArrayList<>();
      // 添加元素
      for (char c = 'A'; c <= 'Z'; c++) {
          list.add(c);
      }
      
      // 遍歷元素
      for (int i = 0; i < list.size(); i++) {
          System.out.print(list.get(i) + ", ");
      }
    • 運行結果
      運行結果

  • 先將集合轉成數組,遍歷數組

    • 示例

      ArrayList<Character> list = new ArrayList<>();
      // 添加元素
      for (char c = 'a'; c <= 'z'; c++) {
          list.add(c);
      }
      
      // 將集合轉成數組
      Character[] cs = list.toArray(new Character[list.size()]);
      
      // 遍歷數組
      for (int i = 0; i < cs.length; i++) {
          System.out.print(cs[i] + ", ");
      }
    • 運行結果
      運行結果

  • foreach(增強for)

    • 示例

      ArrayList<Character> list = new ArrayList<>();
      // 添加元素
      for (char c = 'A'; c <= 'G'; c++) {
          list.add(c);
      }
      
      // 可以寫Character,因自動拆箱,所以寫char是可以的
      for (char c : list) {
          System.out.print(c + ", ");
      }
    • 運行結果
      運行結果

  • 迭代器(Iterator)

    • 示例

      ArrayList<Character> list = new ArrayList<>();
      // 添加元素
      for (char c = 'H'; c <= 'N'; c++) {
          list.add(c);
      }
      
      // 獲得集合的迭代器
      Iterator<Character> it = list.iterator();
      
      // 遍歷
      while (it.hasNext()) {
          char c = it.next();
          System.out.print(c + ", ");
      }
    • 運行結果
      運行結果

  • 迭代器(ListIterator)

    • 示例

      ArrayList<Character> list = new ArrayList<>();
      // 添加元素
      for (char c = 'a'; c <= 'g'; c++) {
          list.add(c);
      }
      
      // 獲得ListIterator迭代器
      ListIterator<Character> li = list.listIterator();
      
      // 遍歷
      while (li.hasNext()) {
          char c = li.next();
          System.out.print(c + ", ");
      }
    • 運行結果
      運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章