您的位置:首页 > 编程语言 > Java开发

JAVA采用数组结构实现一个线性表,可以增删改查,类似于ArrayList

2014-08-12 17:32 513 查看
      
     工作中我们经常使用到集合框架,其中使用最多应该是ArrayList这个类,ArrayList采用的数据结构是数组,存储的元素有序但不唯一,查找效率高,但是增删没有LinkedList的效率高。 


ArrayList:底层用数组实现的List 特点:查询效率高,增删效率低 轻量级 线程不安全 
LinkedList:底层用双向循环链表 实现的List 特点:查询效率低,增删效率高 
Vector: 底层用数组实现List接口的另一个类 特点:重量级,占据更多的系统开销 线程安全


下面写采用数组结构自定义一个类似于ArrayList的一个线性表,功能上实现增删改查。
package com.yes.list;

import java.util.Arrays;

/**
*
* @author yesong
* @since2014-8-6
*
* 采用数组结构实现一个线性表,可以增删改查
*
*/
public class SequenceList<T> {

// 数组的默认长度
private int DEFAULT_SIZE = 16;
// 记录数组的长度
private int capacity;
// 记录元素个数
private int size;
// 保存元素的数组
private Object[] elementData;

public SequenceList() {
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}

public SequenceList(int initSize, T element) {

capacity = 1;
while (capacity < initSize) {
capacity <<= 1;
}
elementData = new Object[capacity];
elementData[0] = element;
size++;
}

public SequenceList(T element) {
this();
elementData[0] = element;
size++;
}

/**
* 获取线性表元素个数
*
* @return
*/
public int length() {
return size;
}

/**
* 向指定位置插入元素
*
* @param position
* @param element
*/
public void insert(int position, T element) {

if (position < 0 || position > size) {
throw new IndexOutOfBoundsException(
"Index Out Of Bounds Exception...");
}
ensureCapacity(size + 1);
System.arraycopy(elementData, position, elementData, position + 1, size
- position);
elementData[position] = element;
size++;

}

/**
* 确保数组长度
*
* @param minCapacity
*/
private void ensureCapacity(int minCapacity) {

if (minCapacity > capacity) {

while (capacity < minCapacity) {
capacity <<= 1;
}
elementData = Arrays.copyOf(elementData, capacity);
}

}

/**
* 添加元素 在线性表的末尾
*
* @param element
*/
public void add(T element) {
insert(size, element);
}

/**
* 删除指定位置的元素。
*
* @param index
*/
public void deleteElement(int index) {

if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException(
"Index Out Of Bounds Exception...");
}
int removedCount = size - index - 1;
if (removedCount > 0) {
System.arraycopy(elementData, index + 1, elementData, index, size
- index);
}
elementData[--size] = null;
size--;

}

/**
* 删除第一个元素
*/
public void remove() {
deleteElement(size - 1);
}

/**
* 判断线性表是否为空
*
* @return
*/
public boolean isEmpty() {
return size == 0;
}

/**
* 清除线性表
*/
public void clear() {

Arrays.fill(elementData, null);
size = 0;
}

@Override
public String toString() {

if (size == 0) {
return "[ ]";
}
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i].toString() + ",");
}
sb.append("]");
sb.deleteCharAt(sb.length() - 2);

return sb.toString();
}

public T getValue(int index) {

if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index Out Of Bounds Exception");
}
return (T) elementData[index];
}

}


这个SequenceList类可以当成一个工具类使用。下面我们来测试这个工具类

<pre name="code" class="java">package com.yes.list;

public class TestSequenceList {

/**
* @param args
*/
public static void main(String[] args) {

SequenceList<String> sequenceList =  new SequenceList<String>();
sequenceList.add("test001");
sequenceList.add("test002");
sequenceList.add("test003");
sequenceList.add("test004");

System.out.println(sequenceList);

sequenceList.insert(2, "yesongsong007");
System.out.println(sequenceList);

//sequenceList.clear();
System.out.println(sequenceList.getValue(sequenceList.length() - 1));

}

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐