新聞中心
一、集合的概念
1.概述:

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:主機域名、網絡空間、營銷軟件、網站建設、宣城網站維護、網站推廣。
在學習集合前,先回憶一下數(shù)組的一個特征---數(shù)組有固定的長度,定義一個數(shù)組:int[] array = new int[];而針對數(shù)據(jù)長度可變的情況,產生了集合,java集合就是為了應對動態(tài)增長數(shù)據(jù),在編譯時無法知道具體的數(shù)據(jù)量而產生的。
集合類又叫容器類。
2.集合和數(shù)組的區(qū)別
- 都是容器,數(shù)組時固定的長度,集合時可變的;
- 數(shù)組存放的數(shù)據(jù)都是基本數(shù)據(jù)類型(四類八種)集合存放的數(shù)據(jù)都是引用數(shù)據(jù)類型(String、Integer、自定義數(shù)據(jù)類型)
- 集合中對于基本數(shù)據(jù)類型會轉換位引用數(shù)據(jù)類型再存儲。
3.集合包含內容、集合的框架
- 接口:Collection,Map,Set,List等(其中Set和List繼承了Collection)
- 抽象類:AbstractCollection,AbstractList等(實現(xiàn)了部分方法)
- 實現(xiàn)類:ArrayList,LinkedList,HashMap等
- 迭代器:Iterator(集合的訪問迭代,返回集合中的元素的迭代器)
二、List集合
1.概述
List集合是一個有序的、可重復的集合,集合中每一個元素都有對應的順序索引。
List允許加入重復元素是應為可以通過索引來訪問指定位置的元素。
List集合默認按照元素的添加順序增加元素的索引。
2.ArrayList
1>概述
ArrayList是基于數(shù)組實現(xiàn)的List類,實現(xiàn)所有可選列表操作,允許所有元素包括null
2>初始化
ArrayList arrayList = new ArrayList(); =>初始容量為10的列表集合
ArrayList
3>主要方法
boolean add(E e) -->將指定的元素追加到此列表的末尾。
void add(int index, E element) -->在此列表中的指定位置插入指定的元素。
boolean addAll(Collection<? extends E> c) -->按指定集合的Iterator返回的順序將指定集合中的所有元素追加到此列表的末尾。
boolean addAll(int index, Collection<? extends E> c) -->將指定集合中的所有元素插入到此列表中,從指定的位置開始。
boolean contains(Object o) -->如果此列表包含指定的元素,則返回 true 。
E get(int index) -->返回此列表中指定位置的元素。
E remove(int index) -->刪除該列表中指定位置的元素。
E set(int index, E element) -->用指定的元素替換此列表中指定位置的元素。
Object[] toArray() -->以正確的順序(從第一個到最后一個元素)返回一個包含此列表中所有元素的數(shù)組。
/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 0:18
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class ArrayListDemo01 {
 public static void main(String[] args) {
 ArrayList arr = new ArrayList();
 arr.add("123");
 System.out.println(arr);
 ArrayList person = new ArrayList();
 Person p1 = new Person("Wyh",18);
 Person p2 = new Person("Hunter", 40);
 person.add(p1);
 person.add(p2);
 for (int i=0;i    3.List集合遍歷的4種方法
- 通過List.size()方法作為for循環(huán)的條件,同數(shù)組遍歷
- 通過迭代器Iterator  Iteratorit = arrayList.iterator(); while循環(huán),hasNext作為判斷條件,next()獲取集合元素再輸出。 
- 增強for循環(huán)
- jdk1.8新特性foreach
/**
 * @ author: PrincessHug
 * @ date: 2019/2/12, 2:43
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class PrintArrayList {
  public static void main(String[] args) {
    ArrayList students = new ArrayList<>();
    Student s1 = new Student("001", "Wyh", '男');
    Student s2 = new Student("002", "Fyh", '男');
    Student s3 = new Student("003", "Zxy", '男');
    students.add(s1);
    students.add(s2);
    students.add(s3);
 
    System.out.println("通過size()方法作為for循環(huán)條件遍歷:");
    for (int i=0;i iterator = students.iterator();
    while (iterator.hasNext()){
      System.out.print(iterator.next() + "\t");
    }
 
    System.out.println("通過增強for循環(huán)遍歷集合:");
    for (Student stu:students){
      System.out.println(stu);
    }
    System.out.println("通過jdk1.8新特性forEach遍歷集合:");
    students.forEach(student -> System.out.println(student));
  }
}  4.LinkedList
1.概述:LinkedList指的是鏈表類的數(shù)據(jù)結構
2.LinkedList與ArrayList的區(qū)別:
a)鏈表中的元素可以任意的增加和刪除,但是查詢效率不如列表
b)鏈表將對象存放在獨立的空間中,而且每個空姐保存了下一個鏈接的索引
構造方法 LinkedList
3.主要方法
void addFirst(E e) -->在該列表開頭插入指定的元素。
void addLast(E e) -->將指定的元素追加到此列表的末尾。 
E peekFirst() -->檢索但不刪除此列表的第一個元素,如果此列表為空,則返回 null 。
E peekLast() -->檢索但不刪除此列表的最后一個元素,如果此列表為空,則返回 null 。 
E pollFirst() -->檢索并刪除此列表的第一個元素,如果此列表為空,則返回 null 。 
E pop() -->從此列表表示的堆棧中彈出第一個元素。相似于removeFirst()  
void push(E e) -->將元素推送到由此列表表示的堆棧上。相似于addFirst()
/**
 * @ author: PrincessHug
 * @ date: 2019/2/10, 2:12
 * @ Blog: https://www.cnblogs.com/HelloBigTable/
 */
public class LinkedListDemo {
 public static void main(String[] args) {
 LinkedList linkedList = new LinkedList<>();
 linkedList.addFirst("is");
 linkedList.addFirst("Wyh");
 linkedList.addLast("cool");
 System.out.println(linkedList);
 System.out.println(linkedList.peekFirst());
 System.out.println(linkedList.pollFirst());
 System.out.println(linkedList);
 System.out.println(linkedList.pop());
 System.out.println(linkedList);
 linkedList.push("Wyh is");
 System.out.println(linkedList);
 }
} 以上所述是小編給大家介紹的Java集合及LIst接口詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網站的支持!
分享題目:淺析Java集合及LIst接口
文章來源:http://m.biofuelwatch.net/article/jigope.html

 建站
建站
 咨詢
咨詢 售后
售后
 建站咨詢
建站咨詢 
 