java 数据结构--顺序表

来源:互联网 发布:叉叉助手脚本源码 编辑:程序博客网 时间:2024/05/21 05:43

DEMO地址:https://github.com/zhaopingfu/MDataStruct

在java中,最常用的顺序表就是ArrayList

  • 优点:查找,修改元素效率高
  • 缺点:增加,删除元素效率低

在ArrayList中,数据是存储一个Object类型的数组中的,而且有一个扩容因子,当达到这个扩容因子且不超过int最大值的时候就扩容,每次扩容后的大小都是之前数组长度的两倍

这里的扩容是新建一个数组,长度是之前数组长度的两倍,然后将之前数组中的数据全部拷贝到新的数组中,再将新创建的这个数组赋值给ArrayList中的数组对象

个人写了一个ArrayList,实现了增删改查的功能

package com.pf;public class MArrayList<T> {    private static final int DEFAULT_CAPACITY = 4;    private static final float DEFAULT_FACTOR = 0.75f;    private static final float MAX_CAPACITY = Integer.MAX_VALUE;    private Object[] datas;    private int size;    public MArrayList() {        datas = new Object[DEFAULT_CAPACITY];    }    public boolean add(T data) {        checkCapacity();        datas[size++] = data;        return true;    }    public boolean add(int index, T data) {        checkIndex(index);        checkCapacity();        System.arraycopy(datas, index, datas, index + 1, size - index);        datas[index] = data;        size++;        return true;    }    public T remove(int index) {        checkIndex(index);        T oldData = elementData(index);        int numMoved = size - index - 1;        if (numMoved > 0) {            System.arraycopy(datas, index + 1, datas, index, size - index - 1);        }        datas[--size] = null;        return oldData;    }    public boolean remove(Object o) {        if (o == null) {            for (int i = 0; i < size; i++) {                if (datas[i] == null) {                    fastRemove(i);                    return true;                }            }        } else {            for (int i = 0; i < size; i++) {                if (o.equals(datas[i])) {                    fastRemove(i);                    return true;                }            }        }        return false;    }    private void fastRemove(int index) {        int numMoved = size - index - 1;        if (numMoved > 0) {            System.arraycopy(datas, index + 1, datas, index, size - index - 1);        }        datas[--size] = null;    }    public T set(int index, T newData) {        checkIndex(index);        T oldData = elementData(index);        datas[index] = newData;        return oldData;    }    public T get(int index) {        checkIndex(index);        return elementData(index);    }    public int size() {        return size;    }    @SuppressWarnings("unchecked")    T elementData(int index) {        return (T) datas[index];    }    private void checkIndex(int index) {        if (index < 0 || index >= size) {            throw new ArrayIndexOutOfBoundsException("index is " + index + ", size is " + size);        }    }    private void checkCapacity() {        if (size / datas.length < DEFAULT_FACTOR) {            return;        }        if (datas.length * 2 >= MAX_CAPACITY) {            throw new RuntimeException("The array length is the largest");        }        Object[] newData = new Object[datas.length * 2];        System.arraycopy(datas, 0, newData, 0, size);        datas = newData;    }}