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

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

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

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

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

 package 线性表.顺序表.普通数组;

 /**
* ArrayList 顺序表
* JAVA 3.0
* 抛异常处理错误的下标
* 使用泛型,当然如果要替换成Object也是可以替换
*/
public class ArrayList<E> { private int capacity; //数组的总容量
private int current; //当前最后一个数组元素的下一个元素下标,从0开始,也是长度
private E[] data = null; //所有数据 public ArrayList(){ this(10);
} public ArrayList(int initialSize){ init(initialSize);
}
/**
* 初始化数组
*/
@SuppressWarnings("unchecked")
public void init(int initialSize){ current = 0;
data = (E[])new Object[initialSize];
capacity = initialSize;
}
/**
* 数组末尾插入元素*/
public void add(E e){ ensureCapacity();
data[current] = e;
current++; }
/**
* 在数组中插入元素*/
public void insert(int index, E e){ validateIndex(index);
ensureCapacity();
for(int i=current;i>=index;i--){ data[i+1] = data[i];
}
data[index] = e;
current++;
}
/**
* 判断是否需要扩展数组
* 如果需要将扩展为原来的两倍
*/
@SuppressWarnings("unchecked")
private void ensureCapacity(){ if(current == capacity){ capacity = capacity * 2;
E[] newData = (E[])new Object[capacity];
for(int index = 0; index < current; ++index) {
newData[index] = data[index];
}
data = newData;
}
} /**
* 删除某个已经存在的对象
* 如果T在数组中,则删除成功返回true,否则无这个元素返回false
*/
public boolean remove(E e){ for(int i=0;i<current;i++){ if(data[i].equals(e)){ remove(i);
return true;
}
}
return false;
}
/**
* 删除特定下标的数组
* @param index
*/
public void remove(int index){ validateIndex(index);
for(int i=index;i<current;i++){ data[i] = data[i+1];
}
data[current] = null;
current--;
}
/**
* 修改下标为index的值*/
public void set(int index, E e){ validateIndex(index);
data[index] = e;
}
/**
* 获取下标为index的值
*/
public E get(int index){ validateIndex(index);
return data[index];
}
/**
* 获取数组已使用的个数
*/
public int size(){ return current;
}
/**
* 销毁数组所有元素
*/
public void clear(){ // Arrays.fill(data, null); 可以替代下面的for循环
for(int i=0;i<current;i++){ data[i] = null;
}
capacity = 0;
current = 0;
}
/**
* 判断数组是否为空
*/
public boolean isEmpty(){ return current==0;
}
/**
* 打印结构
*/
public String toString() { String str = "[ ";
for (Object o : data) {
if (o != null) {
str += o + " ";
}
}
str += "]";
return str;
}
/**    * 判断index 是否越界   */
private void validateIndex(int index) { if (index < 0 || index >= current) {
throw new IndexOutOfBoundsException("无效的下标:" + index);
}
}

最新文章

  1. 采用dlopen、dlsym、dlclose加载动态链接库【总结】(转)
  2. 【python】编码
  3. Oracle事务之一:锁和隔离
  4. [JAVA设计模式]第一部分:接口、抽象类、设计原则
  5. Python练习题 026:求100以内的素数
  6. Unity3d读取.csv文件
  7. codevs 1107 等价表达式
  8. JavaScript中screen对象的两个属性
  9. Spring Bean 生命周期测试
  10. asp.net core mvc ajaxform submit files
  11. http uri唯一标识
  12. php中empty()、isset()、is_null()和变量本身的布尔判断区别
  13. 搞不懂为什么开发人员爱iOS恨Android?
  14. Android中五种常用的menu
  15. POJ_1050_To the Max
  16. C# 二进制图片串互转
  17. Oauth2.0 整合springCloud的Zuul 解决关键BUG 报错信息:Principal must not be null
  18. python中的内容编码
  19. jdbc如何锁定某一条数据或者表,不让别人操作?
  20. JQuery+ajax数据加载..........

热门文章

  1. 在C#主线程和子线程将数据传递给对方如何实现
  2. 流动python - 什么是魔术方法(magic method)
  3. CodeForces 484A Bits
  4. C# WinForm dataGridView 技巧小结
  5. ajax jsonp跨域
  6. object-c计划tips-添加到类对象属性
  7. Oracle解锁的相关操作(转)
  8. 汉高澳大利亚sinox2014电影播放flash最好的办法是安装游戏windows文本firefox
  9. 在 Windows 7 Professional、Enterprise 或 Ultimate 上安装 IIS 7.5
  10. adb这点小事——远程adb调试