Java進階-Java學習路線課程第一課:Java集合框架-ArrayList和LinkedList的使用

本博客地址 | GitHub | 小站坐坐 | 更多資源免費下載

        小夥伴們,大家晚上好!今天我給大家帶來Java集合框架Collection之List講解,今天主要講解List接口下的兩個實現類ArrayList和LinkedList,今天的講解非常重要,請大家仔細看噢!

Java集合框架-ArrayList和LinkedList的使用

       前面我們學過了數組(不熟悉數組的小夥伴點擊:Java入門-Java學習路線課程第五課:一維數組),存取數據都非常方便,但是聲明數組時要給一個固定的長度,如果用來儲存每天的新聞的話,好象有點尷尬,因爲新聞每天可能不一樣的噢!如果新聞太少,浪費空間,如果新聞很多,又會造成空間不夠,那麼怎麼辦呢?還有怎麼存儲更復雜的數據(類型)對象,這些我們都可以用Java集合框架解決。

1、首先,讓大家看看一個簡潔的Java集合框架結構體系圖:
2、再看看這些接口和類有什麼特徵。
  • Collection(接口)

特徵: 不唯一,無序

  • List(子接口)

特徵: 不唯一,有序

  • Set(子接口)

特徵: 唯一,無序

  • Map(接口)

特徵: 鍵值對

key --->value  map.put("userName",uName);
key是拿的Set的特性,而value是拿的Collection的特性。
  • ArrayList(List的實現類)

特徵:底層就是數組 查詢、更新效率比較高

  • LinkedList(List的實現類)

特徵:底層就是鏈表 刪除和增加效率比較高

3、ArrayList演示案例代碼(含List遍歷(或打印輸出)五種方式):
News.java  實例類

package com.javaxyz.entity;

/**
 * @ClassName:News.java
 * @Description:新聞實例類
 * @Author:DongGaoYun
 * @AuthorEnglishName:Andy
 * @URL:www.javaxyz.com 或 www.gyun.org
 * @Email:[email protected]
 * @QQ:1050968899
 * @WeiXin:QingYunJiao
 * @WeiXinGongZhongHao: JavaForum
 * @Date:2019-10-22 下午2:23:38
 * @Version:1.0 完成以下基本代碼:  1.屬性  2.get和set方法   3.構造方法  4.toString
 * 
 */
public class News {
	//屬性
	private int id;
	private String title;//標題
	private String author;//作者

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the author
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * @param author
	 *            the author to set
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	// toString
	@Override
	public String toString() {
		return "News [id=" + id + "," + " title=" + title + "," + " author="
				+ author + "]\n";
	}

	/**
	 * 無參構造方法
	 */
	public News() {
	}

	/**
	 * 有參構造方法
	 * @param id
	 * @param title
	 * @param author
	 */
	public News(int id, String title, String author) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
	}

}
package com.javaxyz.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.javaxyz.entity.News;

/**
 * @ClassName:ArrayListDemo.java
 * @Description:ArrayList的使用
 * @Author:DongGaoYun 
 * @AuthorEnglishName:Andy
 * @URL:www.javaxyz.com 或   www.gyun.org
 * @Email:[email protected]
 * @QQ:1050968899
 * @WeiXin:QingYunJiao
 * @WeiXinGongZhongHao: JavaForum
 * @Date:2019-10-22 下午2:38:25
 * @Version:1.0
 * ArrayList 底層數組 優勢: 查詢 更新
 */
public class ArrayListDemo{
	//需求:
	//增加新聞元素
	//獲取新聞總數
	//操作ArrayList容器,移除元素   
	//打印輸出五種方式	
 	@SuppressWarnings("all")
	public static void main(String[] args){
 		//創建對象
		List list=new ArrayList();//多態的形式。//1.父子關係 2.重寫方法 3.父類的引用指向子類對象
//		ArrayList list=new ArrayList();
		News news1=new News(1, "張卓1", "green1");
		News news2=new News(2, "張卓2", "green2");
		News news3=new News(3, "張卓3", "green3");
		News news4=new News(4, "張卓4", "green4");
		News news5=new News(5, "張卓5", "green5");
		News news6=new News(6, "張卓6", "green6");
		News news7=new News(7, "張卓7", "green7");
		News news8=new News(8, "張卓8", "green8");
		News news9=new News(9, "張卓9", "green9");
		News news10=new News(10, "張卓10", "green10");
		News news11=new News(11, "張卓11", "green11");
		//插入數據
		list.add(news1);
		list.add(news2);
		list.add(news3);
		list.add(news5);
		list.add(news6);
		list.add(news9);
		list.add(news10);
		list.add(news11);
		//插入下標爲1的位置,實際是第二個地方
		list.add(1,news4);
		list.add(2,news7);
		list.add(3,news8);
		//總條數
		System.out.println(list.size());
		
		//操作ArrayList容器,移除元素
	//	list.remove(1);
	//	list.remove(news1);
		//判斷是否包含此元素
		System.out.println(list.contains(news7));
		//list
		System.out.println("-------第一種打印輸出方法---start----");
		System.out.print(list);
		System.out.println("-------第一種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第二種打印輸出方法-普通for--start----");
		for (int i = 0; i <list.size(); i++) {
			News newss= (News) list.get(i);
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第二種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第三種打印輸出方法-增強for--start----");
		for (Object object : list) {
			News newss= (News) object;
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第三種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第四種打印輸出方法-轉換成數組--start----");
		/**
		 *  toArray
			Object[] toArray()
		 *  返回包含此 collection 中所有元素的數組。如果 collection 對其迭代器返回的元素順序做出了某些保證,那麼此方法必須以相同的順序返回這些元素。 
			返回的數組將是“安全的”,因爲此 collection 並不維護對返回數組的任何引用。(換句話說,即使 collection 受到數組的支持,此方法也必須分配一個新的數組)。因此,調用者可以隨意修改返回的數組。 
			
			此方法充當了基於數組的 API 與基於 collection 的 API 之間的橋樑。 		
			
			返回:
			包含此 collection 中所有元素的數組  更多免費資料請加微信公衆號:javaforum
		 */
		Object[] obj=list.toArray();
		for (Object object : obj) {
			News newss= (News) object;
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第四種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第五種打印輸出方法-迭代方式--start----");
		/**
		 * iterator
		   Iterator<E> iterator()
		   
		   boolean hasNext() 
		          如果仍有元素可以迭代,則返回 true。 
		          
		   E next()           更多免費資料請加微信公衆號:javaforum
		          返回迭代的下一個元素。 
		          
		   void remove() 
		          從迭代器指向的 collection 中移除迭代器返回的最後一個元素(可選操作)。 

		 */
		Iterator it=list.iterator();
		while(it.hasNext()){
			News newss= (News) it.next();
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第五種打印輸出方法---end----");		
 	} 
 }
4、輸出結果:
11
true
-------第一種打印輸出方法---start----
[News [id=1, title=張卓1, author=green1]
, News [id=4, title=張卓4, author=green4]
, News [id=7, title=張卓7, author=green7]
, News [id=8, title=張卓8, author=green8]
, News [id=2, title=張卓2, author=green2]
, News [id=3, title=張卓3, author=green3]
, News [id=5, title=張卓5, author=green5]
, News [id=6, title=張卓6, author=green6]
, News [id=9, title=張卓9, author=green9]
, News [id=10, title=張卓10, author=green10]
, News [id=11, title=張卓11, author=green11]
]-------第一種打印輸出方法---end----

-------第二種打印輸出方法-普通for--start----
News [id=1, title=張卓1, author=green1]
News [id=4, title=張卓4, author=green4]
News [id=7, title=張卓7, author=green7]
News [id=8, title=張卓8, author=green8]
News [id=2, title=張卓2, author=green2]
News [id=3, title=張卓3, author=green3]
News [id=5, title=張卓5, author=green5]
News [id=6, title=張卓6, author=green6]
News [id=9, title=張卓9, author=green9]
News [id=10, title=張卓10, author=green10]
News [id=11, title=張卓11, author=green11]
-------第二種打印輸出方法---end----

-------第三種打印輸出方法-增強for--start----
News [id=1, title=張卓1, author=green1]
News [id=4, title=張卓4, author=green4]
News [id=7, title=張卓7, author=green7]
News [id=8, title=張卓8, author=green8]
News [id=2, title=張卓2, author=green2]
News [id=3, title=張卓3, author=green3]
News [id=5, title=張卓5, author=green5]
News [id=6, title=張卓6, author=green6]
News [id=9, title=張卓9, author=green9]
News [id=10, title=張卓10, author=green10]
News [id=11, title=張卓11, author=green11]
-------第三種打印輸出方法---end----

-------第四種打印輸出方法-轉換成數組--start----
News [id=1, title=張卓1, author=green1]
News [id=4, title=張卓4, author=green4]
News [id=7, title=張卓7, author=green7]
News [id=8, title=張卓8, author=green8]
News [id=2, title=張卓2, author=green2]
News [id=3, title=張卓3, author=green3]
News [id=5, title=張卓5, author=green5]
News [id=6, title=張卓6, author=green6]
News [id=9, title=張卓9, author=green9]
News [id=10, title=張卓10, author=green10]
News [id=11, title=張卓11, author=green11]
-------第四種打印輸出方法---end----

-------第五種打印輸出方法-迭代方式--start----
News [id=1, title=張卓1, author=green1]
News [id=4, title=張卓4, author=green4]
News [id=7, title=張卓7, author=green7]
News [id=8, title=張卓8, author=green8]
News [id=2, title=張卓2, author=green2]
News [id=3, title=張卓3, author=green3]
News [id=5, title=張卓5, author=green5]
News [id=6, title=張卓6, author=green6]
News [id=9, title=張卓9, author=green9]
News [id=10, title=張卓10, author=green10]
News [id=11, title=張卓11, author=green11]
-------第五種打印輸出方法---end----
5、ArrayList初始化存儲空間對象Object的長度爲0,增加第一個元素時候,存儲空間對象Object由0變成10個,10個存儲空間用完之後增至15個如圖

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

6、LinkedList演示案例代碼(含List遍歷(或打印輸出)五種方式):
package com.javaxyz.test;

import java.util.Iterator;
import java.util.LinkedList;

import com.javaxyz.entity.News;

/**
 * @ClassName:LinkedListDemo.java
 * @Description:LinkedList的使用
 * @Author:DongGaoYun 
 * @AuthorEnglishName:Andy
 * @URL:www.javaxyz.com 或   www.gyun.org
 * @Email:[email protected]
 * @QQ:1050968899
 * @WeiXin:QingYunJiao
 * @WeiXinGongZhongHao: JavaForum
 * @Date:2019-10-22 下午2:38:25
 * @Version:1.0
 * LinkedList 底層鏈表 優勢: 刪除 增加    
 * 特有功能:
 * addFirst(); 增加頭條元素
 * addLast(); 在新聞的最尾部插入新聞元素
 * getFirst(); 獲取頭條新聞
 * getLast(); 獲取最尾部新聞
 * removeFirst();刪除置頂元素
 * removeLast(); 刪除最尾部新聞
 */
public class LinkedListDemo{
	//需求:
	//增加新聞元素
	//獲取新聞總數
	//操作LinkedList容器,移除元素   
	//打印輸出五種方式	
 	@SuppressWarnings("all")
	public static void main(String[] args){
 		//創建對象
 		//創建多態的形式,要注意的點:只能調用父類與子類重寫的方法,子類特有方法無法調用
		//List list=new LinkedList();//多態的形式。//1.父子關係 2.重寫方法 3.父類的引用指向子類對象
		
 		//初始化時只有first last      更多免費資料請加微信公衆號:javaforum
 		
		LinkedList list=new LinkedList();
		News news1=new News(1, "張卓1", "green1");
		News news2=new News(2, "張卓2", "green2");
		News news3=new News(3, "張卓3", "green3");
		News news4=new News(4, "張卓4", "green4");
		News news5=new News(5, "張卓5", "green5");
		News news6=new News(6, "張卓6", "green6");
		News news7=new News(7, "張卓7", "green7");
		News news8=new News(8, "張卓8", "green8");
		//插入數據
		list.add(news1);
		list.add(news2);
/*		list.add(news3);*/
		//插入下標爲1的位置,實際是第二個地方
		list.add(1,news4);
		//特有的方法:
		//置頂新聞
		list.addFirst(news8);
		//在新聞的最尾部插入新聞元素
		list.addLast(news5);
		//插入數據
		list.add(news6);
		//總條數
		System.out.println(list.size());
		//獲取頭條新聞
		System.out.println(list.getFirst());
		//獲取最尾部新聞
		System.out.println(list.getLast());
		//操作ArrayList容器,移除元素
		list.remove(0);
		list.remove(news1);
		//刪除置頂元素
		list.removeFirst();
		//刪除最尾部新聞
		list.removeLast();
		//判斷是否包含此元素
		System.out.println(list.contains(news7));
		//list
		System.out.println("-------第一種打印輸出方法---start----");
		System.out.print(list);
		System.out.println("-------第一種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第二種打印輸出方法-普通for--start----");
		for (int i = 0; i <list.size(); i++) {
			News newss= (News) list.get(i);
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第二種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第三種打印輸出方法-增強for--start----");
		for (Object object : list) {
			News newss= (News) object;
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第三種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第四種打印輸出方法-轉換成數組--start----");
		/**
		 *  toArray
			Object[] toArray()
		 *  返回包含此 collection 中所有元素的數組。如果 collection 對其迭代器返回的元素順序做出了某些保證,那麼此方法必須以相同的順序返回這些元素。  更多免費資料請加微信公衆號:javaforum
			返回的數組將是“安全的”,因爲此 collection 並不維護對返回數組的任何引用。(換句話說,即使 collection 受到數組的支持,此方法也必須分配一個新的數組)。因此,調用者可以隨意修改返回的數組。 
			
			此方法充當了基於數組的 API 與基於 collection 的 API 之間的橋樑。 		
			
			返回:
			包含此 collection 中所有元素的數組   
		 */
		Object[] obj=list.toArray();
		for (Object object : obj) {
			News newss= (News) object;
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第四種打印輸出方法---end----");
		System.out.println();
		System.out.println("-------第五種打印輸出方法-迭代方式--start----");
		/**
		 * iterator
		   Iterator<E> iterator()
		   
		   boolean hasNext() 
		          如果仍有元素可以迭代,則返回 true。 
		          
		   E next() 
		          返回迭代的下一個元素。 
		          
		   void remove() 
		          從迭代器指向的 collection 中移除迭代器返回的最後一個元素(可選操作)。 

		 */
		Iterator it=list.iterator();
		while(it.hasNext()){
			News newss= (News) it.next();
			System.out.println("News [id=" + newss.getId() + "," + " title=" + newss.getTitle() + "," + " author="
					+ newss.getAuthor() + "]");
		}
		System.out.println("-------第五種打印輸出方法---end----");		
 	} 
 }
7、輸出結果:
6
News [id=8, title=張卓8, author=green8]

News [id=6, title=張卓6, author=green6]

false
-------第一種打印輸出方法---start----
[News [id=2, title=張卓2, author=green2]
, News [id=5, title=張卓5, author=green5]
]-------第一種打印輸出方法---end----

-------第二種打印輸出方法-普通for--start----
News [id=2, title=張卓2, author=green2]
News [id=5, title=張卓5, author=green5]
-------第二種打印輸出方法---end----

-------第三種打印輸出方法-增強for--start----
News [id=2, title=張卓2, author=green2]
News [id=5, title=張卓5, author=green5]
-------第三種打印輸出方法---end----

-------第四種打印輸出方法-轉換成數組--start----
News [id=2, title=張卓2, author=green2]
News [id=5, title=張卓5, author=green5]
-------第四種打印輸出方法---end----

-------第五種打印輸出方法-迭代方式--start----
News [id=2, title=張卓2, author=green2]
News [id=5, title=張卓5, author=green5]
-------第五種打印輸出方法---end----
8、LinkedList 初始化是first對象和 last對象都爲 null ,增加元素後也是在first和 last之間增加。增加第一個元素後,又產生了三個item對象、next和prev對象。如圖:

在這裏插入圖片描述
在這裏插入圖片描述

9、ArrayList和LinkedList的異同:
  • 初始化時,存儲對象不同,分配空間方式也不一樣,ArrayList是數組方式,而LinkedList是鏈表方式,如上圖。

  • 優勢不一樣,ArrayList適合查詢和更新,而LinkedList更適合刪除和增加。

       小夥伴們,本章已經結束,你們都懂了嗎?記得關注、點贊、分享和關注微信公衆號(javaforum)噢!有疑問或好的建議請與我聯繫噢!持續更新!敬請關注!

——————— 精 選 文 章 ———————
  1. Java入門-Java學習路線課程第一課:初識JAVA
  2. Java入門-Java學習路線課程第二課:變量與數據類型
  3. Java入門-Java學習路線課程第三課:選擇結構
  4. Java入門-Java學習路線課程第四課:循環結構
  5. Java入門-Java學習路線課程第五課:一維數組
  6. Java入門-Java學習路線課程第六課:二維數組
  7. Java入門-Java學習路線課程第七課:類和對象
  8. Java入門-Java學習路線課程第八課:方法和方法重載
  9. Java入門-Java學習路線擴展課程:equals的使用
  10. Spring框架-Java學習路線課程第一課:Spring核心
  11. Spring框架-Java學習路線課程:Spring的擴展配置
  12. java學習:在給學生演示用Myeclipse10.7.1工具生成War時,意外報錯:SECURITY: INTEGRITY CHECK ERROR
  13. 使用jquery發送Ajax請求的幾種異步刷新方式
  14. idea Springboot啓動時內嵌tomcat報錯- An incompatible version [1.1.33] of the APR based Apache Tomcat Native
  15. 一個簡單的SSM框架Demo(登錄(包含攔截器)和註銷

更多免費資源請關注微信公衆號:

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