国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

淺析Java集合及LIst接口

2024-07-14 08:43:30
字體:
供稿:網(wǎng)友

一、集合的概念

1.概述:

在學(xué)習(xí)集合前,先回憶一下數(shù)組的一個特征---數(shù)組有固定的長度,定義一個數(shù)組:int[] array = new int[];而針對數(shù)據(jù)長度可變的情況,產(chǎn)生了集合,java集合就是為了應(yīng)對動態(tài)增長數(shù)據(jù),在編譯時無法知道具體的數(shù)據(jù)量而產(chǎn)生的。
集合類又叫容器類。

2.集合和數(shù)組的區(qū)別

  1. 都是容器,數(shù)組時固定的長度,集合時可變的;
  2. 數(shù)組存放的數(shù)據(jù)都是基本數(shù)據(jù)類型(四類八種)集合存放的數(shù)據(jù)都是引用數(shù)據(jù)類型(String、Integer、自定義數(shù)據(jù)類型)
  3. 集合中對于基本數(shù)據(jù)類型會轉(zhuǎn)換位引用數(shù)據(jù)類型再存儲。

3.集合包含內(nèi)容、集合的框架

  1. 接口:Collection,Map,Set,List等(其中Set和List繼承了Collection)
  2. 抽象類:AbstractCollection,AbstractList等(實現(xiàn)了部分方法)
  3. 實現(xiàn)類:ArrayList,LinkedList,HashMap等
  4. 迭代器:Iterator(集合的訪問迭代,返回集合中的元素的迭代器)

二、List集合

1.概述

List集合是一個有序的、可重復(fù)的集合,集合中每一個元素都有對應(yīng)的順序索引。

List允許加入重復(fù)元素是應(yīng)為可以通過索引來訪問指定位置的元素。

List集合默認按照元素的添加順序增加元素的索引。

2.ArrayList

1>概述

ArrayList是基于數(shù)組實現(xiàn)的List類,實現(xiàn)所有可選列表操作,允許所有元素包括null

2>初始化

ArrayList arrayList = new ArrayList();  =>初始容量為10的列表集合

ArrayList<E> arrayList = new ArrayList<E>(); =>數(shù)據(jù)類型為E,初始容量為10

3>主要方法

 boolean add(E e) -->將指定的元素追加到此列表的末尾。  

void add(int index, E element) -->在此列表中的指定位置插入指定的元素。  

boolean addAll(Collection<? extends E> c) -->按指定集合的Iterator返回的順序?qū)⒅付现械乃性刈芳拥酱肆斜淼哪┪病?nbsp; 

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<String> arr = new ArrayList<String>(); arr.add("123"); System.out.println(arr); ArrayList<Person> person = new ArrayList<Person>(); Person p1 = new Person("Wyh",18); Person p2 = new Person("Hunter", 40); person.add(p1); person.add(p2); for (int i=0;i<person.size();i++) { System.out.println(person.get(i)); } System.out.println(person.contains(p2)); person.remove(1); person.set(0,p2); Person[] persons = new Person[2]; person.toArray(persons); System.out.println(persons[0]); System.out.println(persons[1]); }} public class Person { private String name; private int age;  public Person(){}  public Person(String name, int age) { this.name = name; this.age = age; }  public String getName() { return name; }  public void setName(String name) { this.name = name; }  public int getAge() { return age; }  public void setAge(int age) { this.age = age; }  @Override public String toString() { return "["+this.name+","+this.age+"]"; }}

3.List集合遍歷的4種方法

  1. 通過List.size()方法作為for循環(huán)的條件,同數(shù)組遍歷
  2. 通過迭代器Iterator  Iterator<Integer> it = arrayList.iterator(); while循環(huán),hasNext作為判斷條件,next()獲取集合元素再輸出。
  3. 增強for循環(huán)
  4. 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<Student> 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<students.size();i++){      System.out.println(students.get(i));    }     System.out.println("通過迭代器遍歷集合:");    Iterator<Student> 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ù)結(jié)構(gòu)

2.LinkedList與ArrayList的區(qū)別:

a)鏈表中的元素可以任意的增加和刪除,但是查詢效率不如列表
b)鏈表將對象存放在獨立的空間中,而且每個空姐保存了下一個鏈接的索引
構(gòu)造方法 LinkedList<E> linkedList = new LinkedList<E>();

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<String> 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接口詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巍山| 黎城县| 株洲市| 万山特区| 临海市| 瑞金市| 信丰县| 太湖县| 墨竹工卡县| 新巴尔虎左旗| 石河子市| 冷水江市| 边坝县| 沈阳市| 盘山县| 南宫市| 漾濞| 宝山区| 原阳县| 慈利县| 聊城市| 亳州市| 绥棱县| 尉犁县| 新津县| 铜陵市| 黄陵县| 汪清县| 汝城县| 龙胜| 遂溪县| 丹巴县| 修文县| 呼图壁县| 土默特左旗| 思茅市| 郧西县| 濉溪县| 崇信县| 宁南县| 武安市|