顺序表(ArrayList)java实现

http://blog.csdn.net/baoyiming1991/article/details/6265743

package com.important.data.struct.ArrayList;

/*
 * 顺序表: 线性表的顺序表是,指的是用一组地址连续的存储单元一次存储线性表的数据元素。
 *       以元素在计算机内“物理位置相邻”来表示线性表中数据元素之间的逻辑关系。
 *       只要确定了存储线性表的起始位置, 线性表中任何一数据元素都可以随机存取,
 *       所以线性表的存储结构是一种随机存取的存储结构。
 * 
 * 构建核心:顺序表用数组来表示,主要机能还是增删改查。
 */

/*
 * 优点: 1、无须为表示表中元素之间的逻辑关系而增加额外的存储空间。
 *      2、可以快速地存取表中任意位置的元素。
 * 缺点: 1、插入和删除操作需要移动大量的元素
 *      2、当线性表长度变化较大时,难以确定存储空间的容量。
 *      3、造成存储空间的“碎片化”
 */
public class MyArrayList
{

    public static void main(String[] args)
    {
        ArrayListType<String> arrayListType = new ArrayListType<>();
        //赋值化顺序表
        for(int i=1;i<12;i++){
            String temp = i+"";
            arrayListType.add(temp);
        }
        System.out.println("原始顺序表为:");
        arrayListType.print(arrayListType);

        //位置赋值
        arrayListType.add(3, "hh");
        System.out.println("在顺序表的第三个位置处插值后顺序表为:");
        arrayListType.print(arrayListType);

        //删除
        arrayListType.remove(5);
        System.out.println("删除顺序表的第五个位置处的值后顺序表为:");
        arrayListType.print(arrayListType);

        //修改
        arrayListType.set(2, "xixi");
        System.out.println("修改顺序表第二个位置处的值后顺序表为: ");
        arrayListType.print(arrayListType);

        //清空
        arrayListType.clear();
        System.out.println("清空顺序表后: ");
        arrayListType.print(arrayListType);



    }

}

//用数组构建基本数据类型,使用泛型
class ArrayListType<T>{

    private static final int DEFAULT_CAPACITTY = 10;    //数组默认初始容量

    private int theSize;    //顺序表大小
    private T[] theItems;   //顺序表数据

    public ArrayListType(){
        clear();    //初始化基本类型
    }

    //初始化 清空顺序表
    public void clear(){
        theSize = 0;
        ensureCapacity(DEFAULT_CAPACITTY);
    }

    //判断顺序表是否为空
    public boolean isEmpty(){
        return size() == 0;
    }

    //扩容
    public void trimToSize(){
        ensureCapacity(size());
    }

    //获取值
    public T get(int index){
        checkRange(index);
        return theItems[index];
    }

    //设值
    public T set(int index,T newVal){
        checkRange(index);
        T old = theItems[index];
        theItems[index] = newVal;
        return old;
    }

    //加值
    public boolean add(T x){
        add(size(), x);
        return true;
    }

    //加值 索引处
    public void add(int index,T x){     //?
        if(theItems.length == size()){
            ensureCapacity(size()*2+1);
        }
        for(int i=theSize;i>index;i--){
            theItems[i] = theItems[i-1];
        }
        theItems[index] = x;
        theSize++;
    }
    //判断索引是否越界
    public void checkRange(int index){
        if (index >= size())
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size();
    }

    //删除 index
    public T remove(int index){
        T removeT = theItems[index];
        for(int i = index;i<size()-1;i++){
            theItems[i] = theItems[i+1];
        }
        theSize--;
        return removeT;
    }


    //获取顺序表的大小
    public int size(){
        return theSize;
    }

    //容量扩充
    @SuppressWarnings("unchecked")
    public void ensureCapacity(int newCapacity){
        if(newCapacity<theSize){
            return;
        }
        T[] old = theItems;
        theItems = (T[])new Object[newCapacity];
        for(int i=0;i<size();i++){
            theItems[i] = old[i];
        }
    }

    //输出
    public void print(ArrayListType<T> arrayList){
        for(int j=0;j<arrayList.size();j++){
            System.out.print(arrayList.get(j)+" ");
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值