数据结构:自定义线性数组
来源:互联网 发布:seo排名优化 编辑:程序博客网 时间:2024/06/06 17:59
/**
* 接口
* @author Administrator
*
* @param <T>
*/
public interface MyList<T> {
//增加元素
public void add(T t);
//插入元素
public void insert(int index, T t);
//删除数组
public void remove(T t);
//删除指定位置的元素
public void removeAt(int index);
//获取数组的大小
public int size();
//判断数组是否为空
public boolean isEmpty();
//获取元素
public T get(int index);
//设置元素
public void set(int index, T t);
//获取指定位置的元素
public int indexOf(T t);
//清除数组
public void clear();
}
package com.accp.list;
/**
* 实现List的增、删、改、查操作
* @author Administrator
*
* @param <T>
*/
public class MyArrayList<T> extends MyAbstractList<T> {
private T[] list;
/**存储空间的大小*/
private int capacity;
public MyArrayList() {
this(10);
}
public MyArrayList(int capacity) {
this.capacity = capacity;
list = (T[]) new Object[capacity];
}
@Override
/**
* 添加元素:在尾部
*/
public void add(T t) {
ensureCapacity();
list[size++] = t;
}
@Override
/**
* 插入元素
*/
public void insert(int index, T t) {
if(index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
ensureCapacity();
for(int i = size; i > index; i--) {
list[i] = list[i - 1];
}
list[index] = t;
size++;
}
@Override
/**
* 删除元素
*/
public void remove(T t) {
int index = indexOf(t);
if (index != -1) {
removeAt(index);
}
}
@Override
/**
* 删除指定位置的元素
*/
public void removeAt(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
for (int i = index; i < size - 1; i++) {
list[i] = list[i + 1];
}
size--;
}
@Override
/**
* 数组的大小
*/
public int size() {
return size;
}
@Override
/**
* 判断数组是否为空
*/
public boolean isEmpty() {
return size == 0;
}
@Override
/**
* 获取元素
*/
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return list[index];
}
@Override
/**
* 设置指定位置元素的值
*/
public void set(int index, T t) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
list[index] = t;
}
@Override
/**
* 清除数组:使数组的大小变为0
*/
public void clear() {
size = 0;
}
/**
* 追加存储的空间
*/
private void ensureCapacity() {
if (size == capacity) {
T[] temp = (T[]) new Object[capacity * 2];
capacity = capacity * 2;
for (int i = 0; i < size; i++) {
temp[i] = list[i];
}
list = temp;
}
}
}
package com.accp.list;
/**
* 抽象类
* @author Administrator
*
* @param <T>
*/
public abstract class MyAbstractList<T> implements MyList<T> {
protected int size;
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size==0;
}
@Override
public int indexOf(T t) {
for (int i = 0; i < size; i++) {
if (get(i).equals(t)) {
return i;
}
}
return -1;
}
}
- 数据结构:自定义线性数组
- 数据结构 线性结构中的数组
- 【数据结构】线性表(数组实现)
- 数据结构之线性结构--数组
- 数据结构--线性表(数组表述)
- java数据结构:线性表之数组实现
- 【数据结构】线性结构:栈&队列&数组
- 数据结构线性数组的基本操作
- 简单数据结构-数组实现线性表
- 1.数据结构--线性表之数组实现
- 数据结构之线性表(C++)---数组描述
- 数据结构-线性表的数组实现-Java
- 数据结构之线性表(数组实现)
- C语言数据结构-线性表-数组
- 线性数据结构
- (一)线性数组的数据结构和算法
- 数据结构之线性结构(二,联合数组等)
- 【数据结构】线性表之数组---C++语言描述
- 【COCOS2DX-ANDROID-游戏开发之二三】 界面中嵌入Android的WebView
- myeclipse中写struts.xml配置文件打开提示功能
- 系统消息
- 逃过一劫
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
- 数据结构:自定义线性数组
- OGG执行DDL复制报ORA-01435 user does not exist
- 致命错误:linux/videodev.h:没有那个文件或目录
- Inno Setup入门(二十二)——Inno Setup类参考(8)
- 指针--交换函数
- GG做extract时报错OGG-00053
- qnetworkaccessmanager访问IIS没反映
- VC 给控制台exe 增加版本信息,图标
- shell编程实用教程