Java实现数组


public class Array {
    private int data[];
    // 数组大小
    private int n;
    //数组实际元素个数
    private int count;
    public Array(int capacity) {
        this.data = new int[capacity];
        this.n = capacity;
        this.count = 0;
    }

    public int find(int index) {
        if (index < 0 || index >= count) {
            return -1;
        } else {
            return data[index];
        }
    }

    public boolean insert(int index, int a) {
        // 数组已满
        if (count == n) {
            return false;
        } else {
            // 数组未满
            //位置不合法
            if (index < 0 || index > count) {
                return false;
            } // 位置合法
            else {
                for (int i = count; i > index; i--) {
                    data[i] = data[i-1];
                }
                data[index] = a;
                count++;
                return true;
            }
        }
    }

    public boolean delete(int index) {
        if (index < 0 || index >= count) {
            return false;
        } else {
            // 从删除位置开始,所有元素向前移动一位
            for (int i = index + 1; i < count; i++) {
                data[i-1] = data[i];
            }
            count--;
            return true;
        }
    }

    public void printAll() {
        for (int i = 0; i < count; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Array array = new Array(5);
        array.printAll();

        array.insert(0, 3);
        array.insert(0, 4);
        array.insert(1, 5);
        array.insert(3, 9);
        array.insert(3, 10);
        array.printAll();

        array.delete(4);
        array.printAll();
        array.delete(0);
        array.printAll();
        array.delete(1);
        array.printAll();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值