java 数据结构详解,数组,集合,HashMap
數組的特性:
?
數組在內存中是一塊連續的存儲單元存儲起來的,聲明數組的時候我們必須聲明其長度,這樣才會為我們聲明一個連續的存儲區域。
這種存儲方式造成我們想要往數組中存儲一個數據時那么其后面各個元素都要往后移動,同樣的,刪除數據后面的數據都要往前移動。
但是同樣也帶來好處,我們要想獲取數組中第i個元素,直接通過角標獲取即可,同理修改也是。
數組獲取某一數據很簡單通過角標i直接獲取即可,但是增刪比較低效,在內存中用一塊連續的存儲區域來存儲,查找數組中是否包含某一元素比較低效。
?
數據結構之鏈表
與數組不同,鏈表不用非要一塊連續的存儲區域,鏈表是一種離散存儲結構,數據之間通過指針鏈接,每個數據元素包含數據域與指針域,數據域存儲對應數據即可,而指針域則指向下一個數據元素(對于單項鏈表來說),針對指針域還可以分為單向鏈表,雙向鏈表,循環鏈表。
鏈表增刪效率高,查找效率低。每一個數據項與數組相比更耗內存。不需要整塊內存塊,不會造成碎片化。
?
數據結構之哈希表
哈希表就是一種以鍵-值(key-indexed) 存儲數據的結構,我們只要輸入待查找的值即key,即可查找到其對應的值。
?
LinkedList的隊列與棧性質
這里簡單提一下。
隊列:一種數據結構,最明顯的特性是只允許隊頭刪除,隊尾插入。
棧:同樣是一種數據結構,特性是插入刪除都在棧的頂部。
?
存儲時key類型是不確定的,可能是int,可能是String,也可能是其他任意對象。Hash函數的作用就是把這些對象通過合理的方式轉為int類型,從而完成數據的存儲。
1、數組
數組聲明方式有兩種
(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[]) {
?? ? ? ? ? ?//數組的刪除其實就是覆蓋前一位
?? ??? ? ? 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[]) {
?? ? ? ? ? ?//數組的刪除其實就是覆蓋前一位
?? ??? ? ? 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調用刪除。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
?? ? delete2(1,arrays);//調用delete方法
?? ??? ??? ??
?? ??? ??? ? for(int i=0;i<arrays.length;i++){
?? ??? ? ? ? ? ? ? ?System.out.print(" ? ?de==="+arrays[i]);
?? ??? ? ? ? ? ?}
結果:? de===ab ? ?de===cc ? ?de===66 ? ?de===+++ ? ?de===+++
1、4數組排序。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
? int[] a={1,4,-1,5,0};
?? ??? ? ? ? ? ?Arrays.sort(a);
?? ? for(int i=0;i<a.length;i++){
?? ??? ? ? ? ? ? ? ?System.out.print(" ? ?de==="+a[i]);
?? ??? ? ? ? ? ?}
結果:? ? de===-1 ? ?de===0 ? ?de===1 ? ?de===4 ? ?de===5
1、5數組倒序。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
?? ? ?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]);
?? ??? ? ? ? ? ?}
結果:?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集合說明
Set<Integer> test = new HashSet<>();
可以?test.add(null);
?Set<Integer> test = new TreeSet();
不可以??test.add(null);
說明:
List中的元素有存放順序,并且可以存放重復元素,檢索效率高,插入刪除效率低
LinkedList中元素位置是任意的,所以執行插入刪除操作效率較高,查詢效率較低
Vector多個線程同時訪問不會發生不確定的結果,但是它的效率會比較低,如果要考慮線程安全的話可以用它。
?
Set沒有存放順序,而且不可以存放重復元素,后來的元素會把前面重復的元素替換掉,檢索效率低,插入刪除效率高
Set存儲位置是由它的HashCode碼決定的,所以它存儲的對象必須有equals()方法,而且Set遍歷只能用迭代,因為它沒有下標
HashSet是使用Hash表實現的,集合里面的元素是無序得,可以有null值,但是不能有重復元素。
TreeSet是用二叉樹結構實現的集合,集合中的元素是有順序得,不允許放入null,同樣不能放入重復元素。
?
2、2集合操作
| 1 | add( ) ? ? ? ??向集合中添加元素 |
| 2 | clear( ) ? ? ? ?去掉集合中所有的元素 |
| 3 | contains( ) ? ?判斷集合中是否包含某一個元素 |
| 4 | isEmpty( ) ? ?判斷集合是否為空 |
| 5 | iterator( ) ? ?主要用于遞歸集合,返回一個Iterator()對象 |
| 6 | remove( ) ? ?從集合中去掉特定的對象 |
| 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));
?? ??? ??? ??? ? }
結果是:? ? ===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()+" ");
?? ??? ??? ? }?
結果:null 2 3 9? ——默認排序了
數組轉集合:https://blog.csdn.net/meixi_android/article/details/82221089
3、HashMap
存儲鍵值對我們首先想到HashMap,它的底層基于哈希表,采用數組存儲數據,使用鏈表來解決哈希碰撞,它是線程不安全的
HashMap允許空鍵值,并且它是非線程安全的,所以插入、刪除和定位元素會比較快。
? ? ? ? ? ?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)); ? ?
?? ? ? ? ? ? ? ?}? ?
結果:sss
map.get(key) is :ddd
map.get(key) is :6666
map.get(key) is :sss
map.get(key) is :null
TreeMap不允許空鍵值,TreeMap是基于紅黑樹實現的,適用于按自然順序火茲定于順序遍歷key。(鍵需同類型)。如果你需要得到一個有序的結果你就應該使用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)); ? ?
?? ? ? ? ? ? ? ?} ?
結果:
map.get(key) is :cccc
map.get(key) is :aaa
map.get(key) is :bbbbbb
map.get(key) is :ddd
?
HashTable是基于HashCode實現的,但它是線程安全的,所以會比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)); ? ?
?? ? ? ? ? ? ? ?} ?
結果:
tab.get(key) is :bbb
tab.get(key) is :aaa
tab.get(key) is :ddd
tab.get(key) is :ccc
?
總結
以上是生活随笔為你收集整理的java 数据结构详解,数组,集合,HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 以太坊智能合约开发环境配置
- 下一篇: AS中XML注释和取消注释快捷键,实际操