• Java实现顺序表


    利用顺序存储结构表示的顺序表称为顺序表。

      它用一组连续的地址存储单元一次存放线性表中的数据元素。

    顺序表的实现是数据结构中最简单的一种。

    由于代码中已经有详细注释,代码外不再阐述。

    下次再陈上关于顺序表的循环队列和顺序栈的代码。

      1 package 线性表.顺序表.普通数组;
      2 
      3 /**
      4  * ArrayList 顺序表
      5  * JAVA 3.0
      6  * 抛异常处理错误的下标
      7  * 使用泛型,当然如果要替换成Object也是可以替换
      8  */
      9 public class ArrayList<E> {
     10 
     11     private int capacity; //数组的总容量
     12     private int current;  //当前最后一个数组元素的下一个元素下标,从0开始,也是长度
     13     private E[] data = null; //所有数据
     14     
     15     public ArrayList(){
     16         
     17         this(10);
     18     }
     19     
     20     public ArrayList(int initialSize){
     21         
     22         init(initialSize);
     23     }
     24     /**
     25      * 初始化数组
     26      */
     27     @SuppressWarnings("unchecked")
     28     public void init(int initialSize){
     29         
     30         current = 0;
     31         data = (E[])new Object[initialSize];  
     32         capacity = initialSize;
     33     }
     34     /**
     35      * 数组末尾插入元素*/
     36     public void add(E e){
     37         
     38         ensureCapacity();
     39         data[current] = e;
     40         current++;
     41         
     42     }
     43     /**
     44      * 在数组中插入元素*/
     45     public void insert(int index, E e){
     46         
     47         validateIndex(index);
     48         ensureCapacity();
     49         for(int i=current;i>=index;i--){
     50             
     51             data[i+1] = data[i];
     52         }
     53         data[index] = e;
     54         current++;
     55     }
     56     /**
     57      * 判断是否需要扩展数组
     58      * 如果需要将扩展为原来的两倍
     59      */
     60     @SuppressWarnings("unchecked")
     61     private void ensureCapacity(){
     62         
     63         if(current == capacity){
     64                     
     65             capacity = capacity * 2;
     66             E[] newData = (E[])new Object[capacity];  
     67             for(int index = 0; index < current; ++index) {  
     68                 newData[index] = data[index];  
     69             }  
     70             data = newData;
     71         }
     72     }
     73     
     74     /**
     75      * 删除某个已经存在的对象
     76      * 如果T在数组中,则删除成功返回true,否则无这个元素返回false
     77      */
     78     public boolean remove(E e){
     79         
     80         for(int i=0;i<current;i++){
     81             
     82             if(data[i].equals(e)){
     83                 
     84                 remove(i);
     85                 return true;
     86             }
     87         }
     88         return false;
     89     }
     90     /**
     91      * 删除特定下标的数组
     92      * @param index
     93      */
     94     public void remove(int index){
     95         
     96         validateIndex(index);
     97         for(int i=index;i<current;i++){
     98             
     99             data[i] = data[i+1];
    100         }
    101         data[current] = null;
    102         current--;
    103     }
    104     /**
    105      * 修改下标为index的值*/
    106     public void set(int index, E e){
    107         
    108         validateIndex(index);
    109         data[index] = e;
    110     }
    111     /**
    112      * 获取下标为index的值
    113      */
    114     public E get(int index){
    115         
    116         validateIndex(index);
    117         return data[index];
    118     }
    119     /**
    120      * 获取数组已使用的个数
    121      */
    122     public int size(){
    123         
    124         return current;
    125     }
    126     /**
    127      * 销毁数组所有元素
    128      */
    129     public void clear(){
    130         
    131 //      Arrays.fill(data, null);  可以替代下面的for循环
    132         for(int i=0;i<current;i++){
    133             
    134             data[i] = null;
    135         }
    136         capacity = 0;
    137         current = 0;
    138     }
    139     /**
    140      * 判断数组是否为空
    141      */
    142     public boolean isEmpty(){
    143         
    144         return current==0;
    145     }
    146     /**
    147      * 打印结构
    148      */
    149     public String toString() {  
    150         
    151         String str = "[ ";  
    152         for (Object o : data) {  
    153             if (o != null) {  
    154                 str += o + " ";  
    155             }  
    156         }  
    157         str += "]";  
    158         return str;  
    159     }  
    160     /**    * 判断index 是否越界   */
    161     private void validateIndex(int index) {  
    162         
    163         if (index < 0 || index >= current) {  
    164             throw new IndexOutOfBoundsException("无效的下标:" + index);  
    165         }  
    166     }  
    167     
  • 相关阅读:
    redis 源码阅读 数值转字符 longlong2str
    redis 源码阅读 内部数据结构--字符串
    redis 查看的版本
    redis 配置
    redis 基础
    redis 安装
    git 中关于LF 和 CRLF 的问题
    git 常用命令
    linux svn 服务端搭建
    测试开发之Django——No8.Django中的视图与URL配置
  • 原文地址:https://www.cnblogs.com/lzb1096101803/p/4526347.html
Copyright © 2020-2023  润新知