java集合框架之ArrayList與LinkedList的區別

?參考http://how2j.cn/k/collection/collection-arraylist-vs-linkedlist/690.html#nowhere

ArrayList和LinkedList的區別

ArrayList 插入,刪除數據慢
LinkedList, 插入,刪除數據快
ArrayList是順序結構,所以定位很快,指哪找哪。 就像電影院位置一樣,有了電影票,一下就找到位置了。
LinkedList 是鏈表結構,就像手里的一串佛珠,要找出第99個佛珠,必須得一個一個的數過去,所以定位慢

插入數據(最前面插入數據)

package collection;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;public class TestCollection {public static void main(String[] args) {List<Integer> l;l = new ArrayList<>();insertFirst(l, "ArrayList");l = new LinkedList<>();insertFirst(l, "LinkedList");}private static void insertFirst(List<Integer> l, String type) {int total = 1000 * 100;final int number = 5;long start = System.currentTimeMillis();for (int i = 0; i < total; i++) {l.add(0, number);}long end = System.currentTimeMillis();System.out.printf("在%s 最前面插入%d條數據,總共耗時 %d 毫秒 %n", type, total, end - start);}}

由此可見,LinkedList插入數據的速度比ArrayList插入數據的速度快了110倍。

插入數據(最后插入數據)

package swordOffer;

import java.util.*;

public class test {
public static void main(String[] args) {
List<Integer> l;
l = new ArrayList<>();
insertFirst(l, "ArrayList");

l = new LinkedList<>();
insertFirst(l, "LinkedList");

}

private static void insertFirst(List<Integer> l, String type) {
int total = 1000 * 100;
final int number = 5;
long start = System.currentTimeMillis();
for (int i = 0; i < total; i++) {
l.add(number);
}
long end = System.currentTimeMillis();
System.out.printf("在%s 最后插入%d條數據,總共耗時 %d 毫秒 %n", type, total, end - start);
}
}

由此可見,LinkedList插入數據的速度比ArrayList插入數據的速度快了差不多倍。

插入數據(從中間插入數據)

package swordOffer;import java.util.*;public class test {public static void main(String[] args) {List<Integer> l;l = new ArrayList<>();insertFirst(l, "ArrayList");l = new LinkedList<>();insertFirst(l, "LinkedList");}private static void insertFirst(List<Integer> l, String type) {int total = 1000 * 100;final int number = 5;long start = System.currentTimeMillis();for (int i = 0; i < total; i++) {l.add(l.size()/2,number);}long end = System.currentTimeMillis();System.out.printf("在%s 最中間插入%d條數據,總共耗時 %d 毫秒 %n", type, total, end - start);}
}

?由此可見,ArrayList中間插入數據的速度比LinkedList中間插入數據的速度快了400倍。

定位數據

package collection;
?
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
?
public class TestCollection {
??? public static void main(String[] args) {
??????? List<Integer> l;
??????? l = new ArrayList<>();
??????? modify(l, "ArrayList");
?
??????? l = new LinkedList<>();
??????? modify(l, "LinkedList");
?
??? }
?
??? private static void modify(List<Integer> l, String type) {
??????? int total = 100 * 1000;
??????? int index = total/2;
??????? final int number = 5;
??????? //初始化
??????? for (int i = 0; i < total; i++) {
??????????? l.add(number);
??????? }
??????? ?
??????? long start = System.currentTimeMillis();
?
??????? for (int i = 0; i < total; i++) {
???????????? int n = l.get(index);
???????????? n++;
???????????? l.set(index, n);
??????? }
??????? long end = System.currentTimeMillis();
??????? System.out.printf("%s總長度是%d,定位到第%d個數據,取出來,加1,再放回去%n 重復%d遍,總共耗時 %d 毫秒 %n", type,total, index,total, end - start);
??????? System.out.println();
??? }
?
}
package collection;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;public class TestCollection {public static void main(String[] args) {List<Integer> l;l = new ArrayList<>();modify(l, "ArrayList");l = new LinkedList<>();modify(l, "LinkedList");}private static void modify(List<Integer> l, String type) {int total = 100 * 1000;int index = total/2;final int number = 5;//初始化for (int i = 0; i < total; i++) {l.add(number);}long start = System.currentTimeMillis();for (int i = 0; i < total; i++) {int n = l.get(index);n++;l.set(index, n);}long end = System.currentTimeMillis();System.out.printf("%s總長度是%d,定位到第%d個數據,取出來,加1,再放回去%n 重復%d遍,總共耗時 %d 毫秒 %n", type,total, index,total, end - start);System.out.println();}

?由此可見,ArrayList定位數據的速度比LinkedList定位數據的速度快了2200倍。

轉載于:https://www.cnblogs.com/lijingran/p/9079958.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/538596.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/538596.shtml
英文地址,請注明出處:http://en.pswp.cn/news/538596.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python語言是由誰設計并領導開發的_python語言概述 - osc_lt3ocv4d的個人空間 - OSCHINA - 中文開源技術交流社區...

python語言的發展 python語言誕生于1990年&#xff0c;由Guide van Rossum設計并領導開發。 python語言是開源項目的優秀代表&#xff0c;其解釋器的全部代碼都是開源的。 編寫Hello程序 學習編程語言有一個慣例&#xff0c;即運行最簡單的Hello程序&#xff0c;該程序功能是在…

Spark集群安裝

Spark是獨立的&#xff0c;所以集群安裝的時候&#xff0c;不像hive&#xff0c;hbase等需要先安裝hadoop&#xff0c;除非文件保存在hadoop上&#xff0c;才需要安裝hadoop集群。 如果虛擬機安裝&#xff0c;點擊閱讀推薦配置 前提環境&#xff1a; 1、安裝了JDK1.7及以上版…

列表逆序排序_【Python自學筆記】集合——列表

list列表類型是一個與元組tuple類似的有序序列。構造函數是list()切片# 切片 fruit ["Apple", "Hawthorn", "Loquat", "Medlar", "Pear", "Quince"] print(fruit[:2]) print(fruit[-1])語法與字符串和元組中的一…

esp8266 阿里云 arduino_NUCLEO-G071RB通過WiFi與NB連接阿里云

開箱體驗試用背景去年年初&#xff0c;有新項目要讓移動式容器設備的監控數據上云&#xff0c;選型時主要考慮三個系列STM32L0、STM32G0和STM8。最初有意向選用STM32L052RB&#xff0c;主要是為了滿足低功耗需求。恰逢G0系列上市&#xff0c;價格親民&#xff0c;性能卻要高很多…

“云上金融,智創未來” 騰訊“云+未來”峰會金融專場在廣州舉行

5月24日&#xff0c;騰訊“云未來“峰會金融專場在廣州舉行。來自央行、騰訊公司以及銀行、證券、保險、互金公司等騰訊金融云的合作伙伴代表以及行業專家&#xff0c;共同分享了智慧金融、企業數字化轉型、騰訊金融云業務布局以及與合作伙伴取得的最新成績等話題。活動現場&am…

Spark算子reduceByKey深度解析

原文地址&#xff1a;http://blog.csdn.net/qq_23660243/article/details/51435257 -------------------------------------------- 最近經常使用到reduceByKey這個算子&#xff0c;懵逼的時間占據多數&#xff0c;所以沉下心來翻墻上國外的帖子仔細過了一遍&#xff0c;發現一…

繞固定軸分解_3軸 / 5軸 / 3+2到底是什么......??

一、 什么是32定位加工在一個三軸銑削程序執行時&#xff0c;使用五軸機床的兩個旋轉軸將切削刀具固定在一個傾斜的位置&#xff0c;32加工技術的名字也由此而來&#xff0c;這也叫做定位五軸機床&#xff0c;因為第四個軸和第五個軸是用來確定在固定位置上刀具的方向&#xff…

unix環境高級編程 pdf_UNIX環境高級編程——記錄鎖

引言在多進程環境下&#xff0c;多個進程同時讀寫一個文件&#xff0c;如果不進行同步&#xff0c;就可能導致不期待的結果&#xff0c;如后一個進程覆蓋了前一個進程寫的內容。Unix為此提供了一種強大的解決辦法&#xff1a;記錄鎖記錄鎖記錄鎖本質上就是對文件加讀寫鎖&#…

LNMP源碼安裝腳本

LNMP安裝腳本&#xff0c;腳本環境 #LNMP環境搭建centos6.8 2.6.32-696.28.1.el6.x86_64 nginx:1.12.2 mysql:5.6.36 PHP:5.5.36 #!/bin/bash#LNMP環境搭建centos6.8 2.6.32-696.28.1.el6.x86_64 nginx:1.12.2 mysql:5.6.36 PHP:5.5.36trap echo "error line: $LINE…

啟動spark shell

spark集群安裝教程&#xff1a;http://blog.csdn.net/zengmingen/article/details/72123717 啟動spark shell. 在spark安裝目錄bin文件夾下 ./spark-shell --master spark://nbdo1:7077 --executor-memory 2g --total-executor-cores 2 參數說明&#xff1a; --master spark…

python發送excel文件_Python操作Excel, 開發和調用接口,發送郵件

接口開發&#xff1a; importflaskimporttoolsimportjson,redisimportrandom server flask.Flask(__name__)#新建一個服務&#xff0c;把當前這個python文件當做一個服務 ip 118.24.3.40passwordHK139bc&*r redis.Redis(hostip,passwordpassword,port6379,db10, decode_res…

第一個Spark實例:求PI值

向spark提交jar&#xff0c;需要使用 bin下的spark-submit [hadoopnbdo1 bin]$ ./spark-submit --help Usage: spark-submit [options] <app jar | python file> [app arguments] Usage: spark-submit --kill [submission ID] --master [spark://...] Usage: spark-submi…

go conn 讀取byte數組后是否要_【技術推薦】正向角度看Go逆向

Go語言具有開發效率高&#xff0c;運行速度快&#xff0c;跨平臺等優點&#xff0c;因此正越來越多的被攻擊者所使用&#xff0c;其生成的是可直接運行的二進制文件&#xff0c;因此對它的分析類似于普通C語言可執行文件分析&#xff0c;但是又有所不同&#xff0c;本文將會使用…

Confluence 6 選擇一個外部數據庫

2019獨角獸企業重金招聘Python工程師標準>>> 注意&#xff1a; 選擇一個合適的數據庫通常需要花費很多時間。同時 Confluence 自帶的 XML 數據備份和恢復功能通常也不適合合并和備份有大量數據的數據庫。如果你想在系統運行后進行數據合并&#xff0c;你通常需要使用…

spark中saveAsTextFile如何最終生成一個文件

原文地址&#xff1a;http://www.cnblogs.com/029zz010buct/p/4685173.html ----------------------------------------------------------------------- 一般而言&#xff0c;saveAsTextFile會按照執行task的多少生成多少個文件&#xff0c;比如part-00000一直到part-0000n&…

python爬取內容亂碼_python爬取html中文亂碼

環境&#xff1a; python3.6 爬取代碼&#xff1a; import requests url https://www.dygod.net/html/tv/hytv/ req requests.get(url) print(req.text) 爬取結果&#xff1a; / _-如上&#xff0c;title內容出現亂碼&#xff0c;自己感覺應該是編碼的問題&#xff0c;但是不…

前端每日實戰:34# 視頻演示如何用純 CSS 創作在文本前后穿梭的邊框

效果預覽 按下右側的“點擊預覽”按鈕可以在當前頁面預覽&#xff0c;點擊鏈接可以全屏預覽。 https://codepen.io/comehope/pen/qYepNv 可交互視頻教程 此視頻是可以交互的&#xff0c;你可以隨時暫停視頻&#xff0c;編輯視頻中的代碼。 請用 chrome, safari, edge 打開觀看。…

not support mysql_MYSQL出現quot; Client does not support authentication quot;的解決方法

MYSQL 幫助&#xff1a;A.2.3 Client does not support authentication protocolMySQL 4.1 and up uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older clients. If you upgrade the server to 4.1, attemp…

spark shell中編寫WordCount程序

啟動hdfs 略http://blog.csdn.net/zengmingen/article/details/53006541 啟動spark 略安裝&#xff1a;http://blog.csdn.net/zengmingen/article/details/72123717 spark-shell&#xff1a;http://blog.csdn.net/zengmingen/article/details/72162821準備數據 vi wordcount.t…

初級英語02

做客 1 Diana,i havent seen you for ages,how have you been? 2 would you like something to drink? 3 give my best to your parents. 4 did you hear what happened?whats the matter with him? 5 id like to applogize for leaving so early,i brought a little gift,…