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 }
相关文章
相关标签/搜索