一维数组

数组是一组具有某种共同特性的原元素集合,它是应用非常广泛的数据存储结构,具有如下特点:

    1. 数组在定义时,不能分配存储空间,在定义完后,才给数组分配存储空间。

    2. 数组根据下标存取元素。

    3. 数组使用时,会进行边界检查。

    4. 数组既可以保存基本类型(基本类型数组),也可以保存对象引用(对象数组)。

下面为针对一维数组进行的插入、删除、查找的基本实现,为了方便说明,只在数组中存放了基本类型数据,如果想改为对象数组,原理相同。其中:

    1. 代码清单一:为固定长度的无序一维数组的Java实现。

    2. 代码清单二:为固定长度的有序一维数组的Java实现,查找时采用二分法进行查找。

    3. 代码清单三:为可变长度的无序一维数组的Java实现。构造时不必考虑数组的空间大小,亦可在构造后,释放掉增长的多余空间。

 

package com.zhangsx.array;

/**
 * 无序一维数组的Java实现。
 * 构造好以后,数组长度固定。
 * 
 * @author ZhangShixi
 */
public class Array {

    private long[] array;
    private int size;

    /**
     * 构造指定尺寸大小的数组。
     * @param size 数组尺寸大小
     */
    public Array(int size) {
        array = new long[size];
        size = 0;
    }

    /**
     * 获取数组的元素数目。
     * @return 元素数目
     */
    public int size() {
        return size;
    }

    /**
     * 向数组中插入一个元素。
     * @param value 要插入的元素
     */
    public void insert(long value) {
        array[size] = value;
        size++;
    }

    /**
     * 查找数组中的指定元素。
     * @param value 查找元素的关键字
     * @return 如果查找到,返回true;反之,返回false。
     */
    public boolean find(long value) {
        int count;
        for (count = 0; count < size; count++) {
            if (array[count] == value) {
                break;
            }
        }
        if (count == size) {
            return false;
        }
        return true;
    }

    /**
     * 查找数组中指定下下标的元素。
     * @param index 要查找的元素的下标
     * @return 要查找的元素
     */
    public long get(int index) {
        return array[index];
    }

    /**
     * 删除数组中的指定元素。删除元素后,后序元素将逐个前移。
     * @param value 要删除的元素的关键字
     * @return 如果删除成功,返回true;反之,返回false。
     */
    public boolean delete(long value) {
        int count;
        for (count = 0; count < size; count++) {
            if (array[count] == value) {
                break;
            }
        }
        if (count == size) {
            return false;
        } else {
            for (int next = count; next < size; next++) {
                array[next] = array[next + 1];
            }
            size--;
            return true;
        }
    }
}

 

package com.zhangsx.array;

/**
 * 有序一维数组的Java实现。
 * 构造好以后,数组长度固定。
 * 
 * @author ZhangShixi
 */
public class OrderedArray {

    private long[] array;
    private int size;

    /**
     * 获取数组的元素数目。
     * @return 元素数目
     */
    public int size() {
        return size;
    }

    /**
     * 向数组中插入一个元素。
     * 插入后,数组中的元素仍是按关键字有序排列的。
     * @param value 要插入的元素
     */
    public void insert(long value) {
        int index;
        for (index = 0; index < size; index++) {
            if (array[index] > value) {
                break;
            }
        }
        for (int count = index; count < size; count++) {
            array[count + 1] = array[count];
        }
        array[index] = value;
        size++;
    }

    /**
     * 查找数组中的指定元素。
     * 采用二分法查找实现。
     * @param value 要查找的元素的关键字
     * @return 要查找的元素在数组中的下标
     */
    public int find(long value) {
        int start = 0;
        int end = size - 1;
        int index;

        while (true) {
            index = (start + end) / 2;
            if (array[index] == value) {
                return index;
            } else if (end >= start) {
                if (array[index] < value) {
                    start = index + 1;
                } else {
                    end = index - 1;
                }
            } else {
                return size;
            }
        }
    }
    
    /**
     * 查找数组中指定下下标的元素。
     * @param index 要查找的元素的下标
     * @return 要查找的元素
     */
    public long get(int index) {
        return array[index];
    }

    /**
     * 删除数组中的指定元素。删除元素后,后序元素将逐个前移。
     * @param value 要删除的元素的关键字
     * @return 如果删除成功,返回true;反之,返回false。
     */
    public boolean delete(long value) {
        int index = find(value);
        if (index == size) {
            return false;
        } else {
            for (int count = index; count < size; count++) {
                array[count] = array[count + 1];
            }
            size--;
            return true;
        }
    }
}   

 

package com.zhangsx.array;

/**
 * 可变长度的无序一维数组的Java实现。
 * 可用其替代原始不可变长度的无序一维数组,使用时不必考虑空间大小。
 * 在向数组中插入数据时,如果数组已满,则会自动增长空间大小。
 * 当数据小于50时,长度默认为50;当数据大于50时,每次扩展的长度为原数组长度的10%。
 * cutOut()方法提供了修剪数组多余空间的实现,使用户在使用后,可释放掉增长的多余空间。
 * 
 * @author ZhangShixi
 */
public class VariableArray {

    private long[] array;
    private int size;
    // 增长因子
    private static final int GROWTH_FACTOR = 10;

    /**
     * 默认构造器
     */
    public VariableArray() {
        array = new long[0];
        size = 0;
    }

    /**
     * 获取数组的元素数目。
     * @return 元素数目
     */
    public int size() {
        return size;
    }

    /**
     * 获取数组的总长度。
     * @return 数组长度
     */
    public int length() {
        return array.length;
    }

    /**
     * 向数组中插入一个元素。
     * @param value 要插入的元素
     */
    public void insert(long value) {
        if (size == array.length) {
            changeCapacity();
        }
        array[size] = value;
        size++;
    }

    /**
     * 查找数组中的指定元素。
     * @param value 查找元素的关键字
     * @return 如果查找到,返回true;反之,返回false。
     */
    public boolean find(long value) {
        int count;
        for (count = 0; count < size; count++) {
            if (array[count] == value) {
                break;
            }
        }
        if (count == size) {
            return false;
        }
        return true;
    }

    /**
     * 查找数组中指定下下标的元素。
     * @param index 要查找的元素的下标
     * @return 要查找的元素
     */
    public long get(int index) {
        return array[index];
    }

    /**
     * 删除数组中的指定元素。删除元素后,后序元素将逐个前移。
     * @param value 要删除的元素的关键字
     * @return 如果删除成功,返回true;反之,返回false。
     */
    public boolean delete(long value) {
        int index;
        for (index = 0; index < size; index++) {
            if (array[index] == value) {
                break;
            }
        }
        if (index == size) {
            return false;
        } else {
            for (int count = index; count < size; count++) {
                array[count] = array[count + 1];
            }
            size--;
            return true;
        }
    }

    /**
     * 改变数组容量。
     */
    private void changeCapacity() {
        long[] temp = new long[getNewlength()];
        System.arraycopy(array, 0, temp, 0, size);
        array = temp;
    }

    /**
     * 裁剪多余空间。
     */
    public void cutOut() {
        if (array.length > size) {
            long[] temp = new long[size];
            System.arraycopy(array, 0, temp, 0, size);
            array = temp;
        }
    }

    /**
     * 计算增长后的数组的新长度。
     * @return 新长度
     */
    private int getNewlength() {
        return array.length < 50
                ? 50 : array.length + array.length / GROWTH_FACTOR;
    }
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值