java基礎筆記(9)----集合之list集合

來源:互聯網
上載者:User

標籤:margin   empty   必須   封裝類   shuffle   .so   array   添加   font   

  1. 集合
    1. 對於集合的理解,集合是一個容器,用於儲存和管理其它對象的對象
  2. 集合,首先瞭解所有集合的父介面----collection
    1. 特點:儲存任意object元素
    2. 方法
      1. boolean add(Object o) // 把元素o添加到集合中,成功true,否則false
      2. boolean addAll(Collection c) //把集合c中的所有元素 添加到當前集合中
      3. void clear() // 清空當前集合中的所有元素
      4. boolean contains(Object o) //判斷對象o 在當前集合中是否存在
      5. boolean containsAll(Collection c) //判斷集合c中的元素 在當前集合中是否都存在
      6. boolean isEmpty() // 判斷當前集合中的元素個數是否為0
      7. boolean remove(Object o) // 把對象o 從當前集合中刪除,返回是否成功
      8. int size() // 擷取集合元素的實際個數
      9. Object[] toArray() // 把集合轉成對應的數組
  3. list介面
    1. 特點:儲存任意object元素,有序,有下標元素內容可以重
    2. 方法:
      1. 繼承父介面Collection中的所有方法
      2. void add(int idx, Object obj) //在指定下標idx位置 插入元素obj
      3. Object get(int idx) // 擷取指定下標idx位置上的元素
      4. int indexOf(Object o) // 返回o在當前集合中下標,如果不存在 返回-1
      5. Object remove(int idx) //刪除指定下標idx位置的元素,並且返回該元素
      6. Object set(int idx,Object o) //修改idx位置的元素為o,並且返回修改前的元素
      7. List subList(int beginIdx, int toIdx)//截取子集合,從下標beginIdx(含)到toIdx(不含)
    3. 遍曆:下標遍曆,forEach遍曆,迭代遍曆
      1. 如下:

package com.lvsling.test;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

publicclass TestArrayList {

    publicstaticvoid main(String[] args) {

        //student--有三個屬性,name,age,clazz

        List<Student> list = new ArrayList<Student>();

        

        Student s1 = new Student("jerry",20,"1");

        list.add(s1);

        list.add(new Student("tom",18,"2"));

        list.add(new Student("mike",15,"1"));

        

        // 下標遍曆

        for(int i=0; i<list.size(); i++){

            System.out.println(list.get(i));

        }

        System.out.println("-------------------------");

        

        // forEach遍曆

        for(Object obj : list){

            Student s = (Student)obj;

            System.out.println(s.getName());

        }

        System.out.println("-------------------------");

        

        // 迭代遍曆

        Iterator it = list.iterator(); // 擷取集合對象的迭代器

        while(it.hasNext()){ // 判斷是否有下一元素

            Object obj = it.next(); // 擷取下一元素

            System.out.println(obj);

        }

    }

 

}

 

  1. list實作類別
    1. ArrayList
      1. 數組實現,可變長數組
      2. 線程不安全,效率高
      3. 查詢快,增刪慢
    2. Vector
      1. 數組實現,可變長數組
      2. 安全執行緒,效率高
    3. LinkedList
      1. 鏈表實現
      2. 查詢慢,增刪快
  1. 泛型集合
    1. 型別安全的集合,限制集合元素的類型,必須是相同的。
      1. 泛型型別,前後一致
      2. 泛型型別必須是參考型別(基本類型使用封裝類)
  2. 拓展與提升
    1. 工具類
      1. Collections類,是工具類,提供了一組static方法,用於 對集合進行操作。
      2. 如:
        1. Collections.sort(list) // 按升序排----注意:集合元素類型 必須實現java.lang.Comaparable介面。
        2. Collections.reverse(list); // 集合元素反轉
        3. Collections.shuffle(list); // 隨機顯示集合元素

java基礎筆記(9)----集合之list集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.