Java數據結構-01順序表

1、定義數組

  線性表是一種線性結構,它是具備相同類型的n(n≥0)個數據元素組成的有限序列。spa

2、存儲分類code

  1.順序存儲:對象

    ①簡述:是指將線性表中的各個元素依次存放在一組地址連續的存儲單元中,一般將這種方法存儲的線性表稱爲順序表;數組中稍微複雜一點的是多維數組和動態數組。對於Java而言,Collection集合中提供了ArrayList和Vector。blog

    ②特色:數據是連續的;隨機訪問速度快;ci

      ③實現方式:一維數組rem

     

    ④順序表相關操做:get

      A.順序表的插入it

        

       B.順序表的刪除io

        

    ⑤代碼實現(動態數組案例): 

  1 public class MyArrayList {
  2     //爲何使用Object,由於只是說這個容器是用來裝對象的,可是不知道用來裝什麼對象。
  3     private Object[] data;
  4     private int total;
  5     
  6     public MyArrayList(){
  7         data = new Object[5];
  8     }
  9     
 10     //添加一個元素
 11     public void add(Object obj){
 12         //檢查是否須要擴容
 13         checkCapacity();
 14         data[total++] = obj;
 15     }
 16 
 17     private void checkCapacity() {
 18         //若是data滿了,就擴容爲原來的2倍
 19         if(total >= data.length){
 20             data = Arrays.copyOf(data, data.length*2);
 21         }
 22     }
 23     
 24     //返回實際元素的個數
 25     public int size(){
 26         return total;
 27     }
 28     
 29     //返回數組的實際容量
 30     public int capacity(){
 31         return data.length;
 32     }
 33     
 34     //獲取[index]位置的元素
 35     public Object get(int index){
 36         //校驗index的合理性範圍
 37         checkIndex(index);
 38         return data[index];
 39     }
 40 
 41     private void checkIndex(int index) {
 42         if(index<0 || index>=total){
 43             throw new RuntimeException(index+"對應位置的元素不存在");
 44 //            throw new IndexOutOfBoundsException(index+"越界");
 45         }
 46     }
 47     
 48     //替換[index]位置的元素
 49     public void set(int index, Object value){
 50         //校驗index的合理性範圍
 51         checkIndex(index);
 52         
 53         data[index] = value;
 54     }
 55     
 56     //在[index]位置插入一個元素value
 57     public void insert(int index, Object value){
 58         /*
 59          * (1)考慮下標的合理性
 60          * (2)總長度是否夠
 61          * (3)[index]以及後面的元素日後移動,把[index]位置騰出來
 62          * (4)data[index]=value  放入新元素
 63          * (5)total++  有效元素的個數增長
 64          */
 65         
 66         //(1)考慮下標的合理性:校驗index的合理性範圍
 67         checkIndex(index);
 68         
 69         //(2)總長度是否夠:檢查是否須要擴容
 70         checkCapacity();
 71         
 72         //(3)[index]以及後面的元素日後移動,把[index]位置騰出來
 73         /*
 74          * 假設total = 5, data.length= 10, index= 1
 75          * 有效元素的下標[0,4]
 76          * 移動:[1]->[2],[2]->[3],[3]->[4],[4]->[5]
 77          * 移動元素的個數:total-index
 78          */
 79         System.arraycopy(data, index, data, index+1, total-index);
 80         
 81         //(4)data[index]=value  放入新元素
 82         data[index] = value;
 83         
 84         //(5)total++  有效元素的個數增長
 85         total++;
 86     }
 87     
 88     //返回全部實際存儲的元素
 89     public Object[] getAll(){
 90         //返回total個
 91         return Arrays.copyOf(data, total);
 92     }
 93     
 94     //刪除[index]位置的元素
 95     public void remove(int index){
 96         /*
 97          * (1)校驗index的合理性範圍
 98          * (2)移動元素,把[index+1]以及後面的元素往前移動
 99          * (3)把data[total-1]=null  讓垃圾回收器儘快回收
100          * (4)總元素個數減小 total--
101          */
102         
103         //(1)考慮下標的合理性:校驗index的合理性範圍
104         checkIndex(index);
105         
106         //(2)移動元素,把[index+1]以及後面的元素往前移動
107         /*
108          * 假設total=8, data.length=10, index = 3
109          * 有效元素的範圍[0,7]
110          * 移動:[4]->[3],[5]->[4],[6]->[5],[7]->[6]
111          * 移動了4個:total-index-1
112          */
113         System.arraycopy(data, index+1, data, index, total-index-1);
114         
115         //(3)把data[total-1]=null  讓垃圾回收器儘快回收
116         data[total-1] = null;
117         
118 //        (4)總元素個數減小 total--
119         total--;
120     }
121     
122     //查詢某個元素的下標
123 /*    public int indexOf(Object obj){
124         for (int i = 0; i < total; i++) {
125         //這兩種寫法都有風險
126             if(obj.equals(data[i])){
127                 //if(data[i].equals(obj)){
128                 return i;//找到,返回第一個找到的
129             }
130         }
131         return -1;//沒找到返回-1
132     }*/
133     
134     //查詢某個元素的下標
135     public int indexOf(Object obj){
136         if(obj == null){
137             for (int i = 0; i < total; i++) {
138                 if(data[i] == null){//等價於 if(data[i] == obj)
139                     return i;
140                 }
141             }
142         }else{
143             for (int i = 0; i < data.length; i++) {
144                 if(obj.equals(data[i])){
145                     return i;
146                 }
147             }
148         }
149         return -1;
150     }
151     
152     //刪除數組中的某個元素
153     //若是有重複的,只刪除第一個
154     public void remove(Object obj){
155         /*
156          * (1)先查詢obj的[index]
157          * (2)若是存在,就調用remove(index)刪除就能夠
158          */
159         
160         //(1)先查詢obj的[index]
161         int index = indexOf(obj);
162         
163         if(index != -1){
164             remove(index);
165         }
166         //不存在,能夠什麼也不作
167         //不存在,也能夠拋異常
168         //throw new RuntimeException(obj + "不存在");
169     }
170     
171     public void set(Object old, Object value){
172         /*
173          * (1)查詢old的[index]
174          * (2)若是存在,就調用set(index, value)
175          */
176         
177 //        (1)查詢old的[index]
178         int index = indexOf(old);
179         if(index!=-1){
180             set(index, value);
181         }
182         
183         //不存在,能夠什麼也不作
184         //不存在,也能夠拋異常
185         //throw new RuntimeException(old + "不存在");
186     }
187 }
相關文章
相關標籤/搜索