日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 数据结构详解,数组,集合,HashMap

發(fā)布時(shí)間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 数据结构详解,数组,集合,HashMap 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)組的特性:

?

數(shù)組在內(nèi)存中是一塊連續(xù)的存儲(chǔ)單元存儲(chǔ)起來(lái)的,聲明數(shù)組的時(shí)候我們必須聲明其長(zhǎng)度,這樣才會(huì)為我們聲明一個(gè)連續(xù)的存儲(chǔ)區(qū)域。

這種存儲(chǔ)方式造成我們想要往數(shù)組中存儲(chǔ)一個(gè)數(shù)據(jù)時(shí)那么其后面各個(gè)元素都要往后移動(dòng),同樣的,刪除數(shù)據(jù)后面的數(shù)據(jù)都要往前移動(dòng)。

但是同樣也帶來(lái)好處,我們要想獲取數(shù)組中第i個(gè)元素,直接通過(guò)角標(biāo)獲取即可,同理修改也是。

數(shù)組獲取某一數(shù)據(jù)很簡(jiǎn)單通過(guò)角標(biāo)i直接獲取即可,但是增刪比較低效,在內(nèi)存中用一塊連續(xù)的存儲(chǔ)區(qū)域來(lái)存儲(chǔ),查找數(shù)組中是否包含某一元素比較低效。

?

數(shù)據(jù)結(jié)構(gòu)之鏈表

與數(shù)組不同,鏈表不用非要一塊連續(xù)的存儲(chǔ)區(qū)域,鏈表是一種離散存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)之間通過(guò)指針鏈接,每個(gè)數(shù)據(jù)元素包含數(shù)據(jù)域指針域,數(shù)據(jù)域存儲(chǔ)對(duì)應(yīng)數(shù)據(jù)即可,而指針域則指向下一個(gè)數(shù)據(jù)元素(對(duì)于單項(xiàng)鏈表來(lái)說(shuō)),針對(duì)指針域還可以分為單向鏈表,雙向鏈表,循環(huán)鏈表。

鏈表增刪效率高,查找效率低。每一個(gè)數(shù)據(jù)項(xiàng)與數(shù)組相比更耗內(nèi)存。不需要整塊內(nèi)存塊,不會(huì)造成碎片化。

?

數(shù)據(jù)結(jié)構(gòu)之哈希表

哈希表就是一種以鍵-值(key-indexed) 存儲(chǔ)數(shù)據(jù)的結(jié)構(gòu),我們只要輸入待查找的值即key,即可查找到其對(duì)應(yīng)的值。

?

LinkedList的隊(duì)列與棧性質(zhì)

這里簡(jiǎn)單提一下。

隊(duì)列:一種數(shù)據(jù)結(jié)構(gòu),最明顯的特性是只允許隊(duì)頭刪除,隊(duì)尾插入。

:同樣是一種數(shù)據(jù)結(jié)構(gòu),特性是插入刪除都在棧的頂部。

?

存儲(chǔ)時(shí)key類型是不確定的,可能是int,可能是String,也可能是其他任意對(duì)象。Hash函數(shù)的作用就是把這些對(duì)象通過(guò)合理的方式轉(zhuǎn)為int類型,從而完成數(shù)據(jù)的存儲(chǔ)。

1、數(shù)組

數(shù)組聲明方式有兩種

(1)

String [] arrays = {"ab","ac","cc","66","+++"};

arrays[3]="99";//更改值

(2)

String [] arrays =new ?String[5];

arrays[3]="99";//賦值

1、2刪除元素方法。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

?? ? ? public static String[] delete(int index, String array[]) {
?? ? ? ? ? ?//數(shù)組的刪除其實(shí)就是覆蓋前一位
?? ??? ? ? String[] arrNew = new String[array.length - 1];
?? ? ? ? ? ?for (int i = index; i < array.length - 1; i++) {
?? ? ? ? ? ? ? ?array[i] = array[i + 1];
?? ? ? ? ? ?}
//?? ? ? ? ? ?System.arraycopy(array, 0, arrNew, 0, arrNew.length);
?? ? ? ? ? ?return arrNew;
?? ? ? ?}
?? ? ??
?? ? ? public static String[] delete2(int index, String array[]) {
?? ? ? ? ? ?//數(shù)組的刪除其實(shí)就是覆蓋前一位
?? ??? ? ? String[] arrNew = new String[array.length - 1];
?? ? ? ? ? ?for (int i = 0; i < array.length - 1; i++) {
?? ? ? ? ? ? ? ?if (i < index) {
?? ? ? ? ? ? ? ??? ?array[i] = array[i];
?? ? ? ? ? ? ? ?} else {
?? ? ? ? ? ? ? ??? ?array[i] = array[i + 1];
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ?}
?? ? ? ?
?? ? ? ? ? ?return arrNew;
?? ? ? ?}

1、3調(diào)用刪除。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

?? ? delete2(1,arrays);//調(diào)用delete方法
?? ??? ??? ??
?? ??? ??? ? for(int i=0;i<arrays.length;i++){
?? ??? ? ? ? ? ? ? ?System.out.print(" ? ?de==="+arrays[i]);
?? ??? ? ? ? ? ?}

結(jié)果:? de===ab ? ?de===cc ? ?de===66 ? ?de===+++ ? ?de===+++

1、4數(shù)組排序。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

? int[] a={1,4,-1,5,0};
?? ??? ? ? ? ? ?Arrays.sort(a);

?? ? for(int i=0;i<a.length;i++){
?? ??? ? ? ? ? ? ? ?System.out.print(" ? ?de==="+a[i]);
?? ??? ? ? ? ? ?}

結(jié)果:? ? de===-1 ? ?de===0 ? ?de===1 ? ?de===4 ? ?de===5

1、5數(shù)組倒序。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

?? ? ?int[] a={1,4,-1,5,0};
?? ??? ? ? ? ? ?Arrays.sort(a);
?? ??? ? ? ? ? ?
?? ??? ? ? ? ? ?int [] daoxu =new int[a.length];
?? ??? ??? ??
?? ??? ??? ? for(int i=0;i<a.length;i++){
?? ??? ??? ??? ? daoxu[a.length-i-1] = a[i];
//?? ??? ? ? ? ? ? ? ?System.out.print(" ? ?de==="+a[i]);
?? ??? ? ? ? ? ?}
?? ??? ??? ? for(int i=0;i<daoxu.length;i++){
?? ??? ? ? ? ? ? ? ?System.out.print(" ? ?daoxu==="+daoxu[i]);
?? ??? ? ? ? ? ?}

結(jié)果:?daoxu===5 ? ?daoxu===4 ? ?daoxu===1 ? ?daoxu===0 ? ?daoxu===-1

2、集合

list集合聲明

//?? ?static List<String> list2=new ArrayList<>();
//?? ?static List<String> list2=new LinkedList<>();
?? ?static List<String> list2=new Vector<>();

set集合說(shuō)明

Set<Integer> test = new HashSet<>();

可以?test.add(null);

?Set<Integer> test = new TreeSet();

不可以??test.add(null);

說(shuō)明:

List中的元素有存放順序,并且可以存放重復(fù)元素,檢索效率高,插入刪除效率低

LinkedList中元素位置是任意的,所以執(zhí)行插入刪除操作效率較高,查詢效率較低

Vector多個(gè)線程同時(shí)訪問(wèn)不會(huì)發(fā)生不確定的結(jié)果,但是它的效率會(huì)比較低,如果要考慮線程安全的話可以用它。

?

Set沒(méi)有存放順序,而且不可以存放重復(fù)元素,后來(lái)的元素會(huì)把前面重復(fù)的元素替換掉,檢索效率低,插入刪除效率高

Set存儲(chǔ)位置是由它的HashCode碼決定的,所以它存儲(chǔ)的對(duì)象必須有equals()方法,而且Set遍歷只能用迭代,因?yàn)樗鼪](méi)有下標(biāo)

HashSet是使用Hash表實(shí)現(xiàn)的,集合里面的元素是無(wú)序得,可以有null值,但是不能有重復(fù)元素。

TreeSet是用二叉樹(shù)結(jié)構(gòu)實(shí)現(xiàn)的集合,集合中的元素是有順序得,不允許放入null,同樣不能放入重復(fù)元素。

?

2、2集合操作

Sr.No.Method & Description
1

add( ) ? ? ? ??向集合中添加元素

2

clear( ) ? ? ? ?去掉集合中所有的元素

3

contains( ) ? ?判斷集合中是否包含某一個(gè)元素

4

isEmpty( ) ? ?判斷集合是否為空

5

iterator( ) ? ?主要用于遞歸集合,返回一個(gè)Iterator()對(duì)象

6

remove( ) ? ?從集合中去掉特定的對(duì)象

7

size( ) ? ? ? ?返回集合的大小

?? ??? ?list2.add("aaa"); ? ?
?? ??? ??? ?list2.add("bbb"); ??
?? ??? ??? ?list2.add("ccc"); ??
?? ??? ??? ?list2.add("111"); ??
?? ??? ??? ?list2.add("111"); ?

?? ??? ??? ?list2.remove(2);//刪除元素
?? ??? ??? ?list2.add(2, "插入icom");//插入元素。先刪除再插入就是替換元素
?? ?
?? ?
?? ??? ??? ? for(int i=0;i<list2.size();i++){
?? ??? ??? ??? ? System.out.print(" ???==="+list2.get(i));
?? ??? ??? ??? ? }

結(jié)果是:? ? ===aaa ? ===bbb ? ===插入icom ? ===111 ? ===111

Set遍歷:使用迭代方法

?? ??? ? Set<Integer> test = new HashSet<>();

?? ??? ??? ? int c = 3;
?? ??? ??? ? int d = 9;
?? ??? ??? ? int e = 2;
?? ??? ??? ? ?
?? ??? ????? ? test.add(c);
?? ??? ??? ? test.add(d);
?? ??? ??? ? test.add(e);
?? ??? ??? ? test.add(null);

?? ??? ? Iterator<Integer> value = test.iterator();
?? ??? ??? ? while (value.hasNext()) {
//?? ??? ??? ? ? ? int s = value.next();
?? ??? ??? ? ? ? System.out.print(value.next()+" ");
?? ??? ??? ? }?

結(jié)果:null 2 3 9? ——默認(rèn)排序了

數(shù)組轉(zhuǎn)集合:https://blog.csdn.net/meixi_android/article/details/82221089

3、HashMap

存儲(chǔ)鍵值對(duì)我們首先想到HashMap,它的底層基于哈希表,采用數(shù)組存儲(chǔ)數(shù)據(jù),使用鏈表來(lái)解決哈希碰撞,它是線程不安全的

HashMap允許空鍵值,并且它是非線程安全的,所以插入、刪除和定位元素會(huì)比較快。

? ? ? ? ? ?Map map=new HashMap(); ? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?map.put(3, "sss"); ? ?
?? ? ? ? ? ? ? ?map.put(2, 6666); ? ?
?? ? ? ? ? ? ? ?map.put("c", null); ? ?
?? ? ? ? ? ? ? ?map.put(null, "ddd"); ? ?
?? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?System.out.println(map.get(3));
?? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?//遍歷
?? ? ? ? ? ? ? ?Iterator iterator = map.keySet().iterator(); ? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?while (iterator.hasNext()) { ? ?
?? ? ? ? ? ? ? ? Object key = iterator.next(); ? ?
?? ? ? ? ? ? ? ? System.out.println("map.get(key) is :"+map.get(key)); ? ?
?? ? ? ? ? ? ? ?}? ?

結(jié)果:sss
map.get(key) is :ddd
map.get(key) is :6666
map.get(key) is :sss
map.get(key) is :null

TreeMap不允許空鍵值,TreeMap是基于紅黑樹(shù)實(shí)現(xiàn)的,適用于按自然順序火茲定于順序遍歷key。(鍵需同類型)。如果你需要得到一個(gè)有序的結(jié)果你就應(yīng)該使用TreeMap

?

? ? ? ? ? ?Map map=new TreeMap(); ? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? map.put(2, "aaa"); ? ?
?? ? ? ? ? ? ? ?map.put(1, "cccc"); ? ?
?? ? ? ? ? ? ? ?map.put(4, "bbbbbb"); ? ?
?? ? ? ? ? ? ? ?map.put(5, "ddd"); ? ? ?
?? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?//遍歷
?? ? ? ? ? ? ? ?Iterator iterator = map.keySet().iterator(); ? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?while (iterator.hasNext()) { ? ?
?? ? ? ? ? ? ? ? Object key = iterator.next(); ? ?
?? ? ? ? ? ? ? ? System.out.println("map.get(key) is :"+map.get(key)); ? ?
?? ? ? ? ? ? ? ?} ?

結(jié)果:

map.get(key) is :cccc
map.get(key) is :aaa
map.get(key) is :bbbbbb
map.get(key) is :ddd

?

HashTable是基于HashCode實(shí)現(xiàn)的,但它是線程安全的,所以會(huì)比HashMap效率低,而且不允許null值。

? ? ? ? Hashtable tab=new Hashtable(); ? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?tab.put("a", "aaa"); ? ?
?? ? ? ? ? ? ? ?tab.put("b", "bbb"); ? ?
?? ? ? ? ? ? ? ?tab.put("c", "ccc"); ? ?
?? ? ? ? ? ? ? ?tab.put("d", "ddd"); ?
?? ? ? ? ? ? ? ?
?? ? ? ? ? ? ? ?Iterator iterator_1 = tab.keySet().iterator(); ? ?
?? ? ? ? ? ? ? ?while (iterator_1.hasNext()) { ? ?
?? ? ? ? ? ? ? ? Object key = iterator_1.next(); ? ?
?? ? ? ? ? ? ? ? System.out.println("tab.get(key) is :"+tab.get(key)); ? ?
?? ? ? ? ? ? ? ?} ?

結(jié)果:

tab.get(key) is :bbb
tab.get(key) is :aaa
tab.get(key) is :ddd
tab.get(key) is :ccc
?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的java 数据结构详解,数组,集合,HashMap的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。