码迷,mamicode.com
首页 > 编程语言 > 详细

使用JAVA数组实现顺序表

时间:2015-04-20 00:12:17      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

1,引入了JAVA泛型类,因此定义了一个Object[] 类型的数组,从而可以保存各种不同类型的对象。

2,默认构造方法创建了一个默认大小为16的Object数组;带参数的构造方法创建一个指定长度的Object数组

3,实现的顺序表的基本操作有:返回表的长度、获取指定索引处的元素(注意是索引,而不是位置。索引以下标0开始,位置以下标1开始)、按值查找数据元素的位置、直接插入元素(顺序表尾部)、向指定位置插入元素、直接删除元素(在顺序表尾部)、删除指定索引处元素、判断表是否为空、清空表。

  1 import java.util.Arrays;
  2 
  3 public class SequenceList<T> {
  4     private final int DEFAULT_SIZE = 16;//final实例变量显示指定初始值,且不再变化。
  5     
  6     private Object[] elementData;//该数组用来保存顺序表中的元素
  7     private int capacity;//保存数组的长度
  8     private int size;//保存顺序表中当前元素的个数
  9     
 10     //以默认的大小创建顺序表
 11     public SequenceList(){
 12         capacity = DEFAULT_SIZE;
 13         elementData = new Object[capacity];
 14     }
 15     
 16     //以指定的大小创建顺序表
 17     public SequenceList(int initSize){
 18         capacity = 1;
 19         while(capacity < initSize)
 20             capacity <<= 1;//将capacity设置成大于initSize的最小2次方
 21         elementData = new Object[capacity];
 22     }
 23     
 24     //获取顺序表中当前元素的个数
 25     public int length(){
 26         return size;
 27     }
 28     
 29     //获取顺序表中索引为  i 处的元素,i表示索引,即以 0 开始
 30     public T get(int i){
 31         if(i < 0 || i > size - 1)
 32             throw new IndexOutOfBoundsException("顺序表索引越界");
 33         return (T)elementData[i];
 34     }
 35     
 36     //查看顺序表中指定元素的索引,若未找到,返回-1
 37     public int locate(T element){
 38         for(int i = 0; i < size; i++)
 39             if(elementData[i].equals(element))
 40                 return i;
 41         return -1;
 42     }
 43     
 44     //在顺序表的指定索引处插入一个元素
 45     public void insert(T element, int index){
 46         if(index < 0 || index > size)
 47             throw new IndexOutOfBoundsException("顺序表索引越界");
 48         ensureCapacity(size + 1);//确保顺序表满时进行扩容,从而能插入元素
 49         //将指定索引后的所有元素向后移动一个位置
 50 //        System.arraycopy(elementData, index, elementData, index + 1, size - index);
 51         for(int i = size; i > index; i--)
 52             elementData[i] = elementData[i - 1];
 53         elementData[index] = element;
 54         size++;//顺序表中的元素个数增1
 55     }
 56     
 57     private void ensureCapacity(int minCapacity){
 58         //当数组容量已满时,对数组进行扩容。将容量扩展到大于minCapacity的最小2的次方
 59         if(minCapacity > capacity){
 60             while(capacity < minCapacity)
 61                 capacity <<= 1;
 62             elementData = Arrays.copyOf(elementData, capacity);
 63         }
 64     }
 65     
 66     //在顺序表的末尾添加一个元素
 67     public void add(T element){
 68         insert(element, size);
 69     }
 70     
 71     //删除顺序表中指定索引处的元素
 72     public T delete(int index){
 73         if(index < 0 || index > size - 1)
 74             throw new IndexOutOfBoundsException("顺序表索引越界");
 75         T oldValue = (T)elementData[index];
 76         int numMoved = size - index - 1;//计算需要移动的元素个数
 77         if(numMoved > 0){
 78             System.arraycopy(elementData, index + 1, elementData, index, numMoved);
 79         }
 80         elementData[--size] = null;//让垃圾回收器及时回收,避免内存泄露
 81         return oldValue;
 82     }
 83     
 84     //删除顺序表中的最后一个元素
 85     public T remove(){
 86         return delete(size - 1);
 87     }
 88     
 89     //判断顺序表是否为空表
 90     public boolean empty(){
 91         return size == 0;
 92     }
 93     
 94     //清空顺序表
 95     public void clear(){
 96         Arrays.fill(elementData, null);//将数组elementData中的每个元素都赋值null
 97         size = 0;
 98     }
 99     
100     public String toString(){
101         if(size == 0)
102             return "[]";
103         else{
104             StringBuilder sb = new StringBuilder("[");
105             for(int i = 0; i < size; i++)
106                 sb.append(elementData[i].toString() + ", ");
107             int len = sb.length();
108             //删除由于上面for循环中最后添加的多余的两个字符 (一个是逗号,一个是空格符号)
109             return sb.delete(len - 2, len).append("]").toString();
110         }
111     }
112 }

 

使用JAVA数组实现顺序表

标签:

原文地址:http://www.cnblogs.com/hapjin/p/4440243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!