Java——数组,封装数组

数组介绍:

    ①用来存储一组相同类型的数据
    ②在内存中, 分配连续的空间用来存储数据, 创建时要指定数组的大小
    ③创建方式:int[] nums = new int[10];
    ④索引, 访问元素时通过索引进行访问, 速度快
    ⑤索引从0开始到length - 1
    ⑥使用数组时常见的错误:NullPointException(空指针异常) ArrayIndexOutOfBoundsException(索引越界)
    ⑦常见数组:字符串, 对象数组, 哈希表

优点:修改和查找速度快, 可以通过索引直接操作
缺点:增加和删除速度慢, 增加需要对后面的元素后移, 删除需要对后面的元素前移.
     数组一旦创建大小不可改变

数组的使用案例代码:

public class ArrayDemo {
    public static void main(String[] args) {
        // 1.创建数组
        int[] arr = {2, 8, 6, 4, 3, 7, 4};

        // 2.获取数组长度
        int length = arr.length;

        // 3.获取指定索引元素的值
        int num = arr[3];
        System.out.println("num = " + num);

        // 4.改变制定索引的值
        arr[3] = 10;
        num = arr[3];
        System.out.println("num = " + num);

        // 5.打印数组
        System.out.println(Arrays.toString(arr));

        // 6.数组越界
        System.out.println(arr[length]);// ArrayIndexOutOfBoundsException
    }
}
运行结果:

封装数组:

封装数组的作用:对于数组一旦创建大小不能改变, 有时候可能会导致空间不够或者空间浪费严重. 所以有了封装数组将各种方法写到一个类中对数组进行操作时简便

封装数组:代码

public class MyArray<V> {
    private V[] data;// 存放元素的数组
    private int size;// 数组中实际存放的元素个数
    private int capacity;// 数组的大小, 容积

    // 构造器
    public MyArray() {
        this.size = 0;
        this.capacity = 10;// 默认初始化大小为10
        this.data = (V[]) (new Object[this.capacity]);
    }

    public MyArray(int capacity) {
        if (capacity <= 0) {
            capacity = 10;
        } else {
            this.capacity = capacity;
        }
        this.size = 0;
        this.data = (V[]) (new Object[this.capacity]);
    }

    // 增
    public void add(V value) {
        if (this.size == this.capacity) {
            resize(this.capacity << 1);// 扩容
        }
        this.data[this.size++] = value;
    }

    // 增, 指定索引插入值
    public void insert(V value, int index) {
        if (index > this.size || index < 0) {
            throw new RuntimeException();
        }
        if (this.size == this.capacity) {
            resize(this.capacity << 1);// 扩容
        }
        for (int i = this.size; i > index; i--) {
            this.data[i] = this.data[i - 1];
        }
        this.data[index] = value;
        this.size++;
    }

    private void resize(int newCapacity) {
        this.data = Arrays.copyOf(this.data, newCapacity);
        this.capacity = newCapacity;
    }

    // 删, 根据索引删除
    public V removeByIndex(int index) {
        if (index >= 0 && index < size) {
            V delValue = data[index];
            for (int i = index + 1; i < size; i++) {
                this.data[i - 1] = this.data[i];
            }
            this.size--;
            // 判断是否需要进行缩绒
            if (this.size <= this.capacity / 4 && this.capacity >= 10) {
                resize(this.capacity >> 1);
            }
            return delValue;
        } else {
            return null;
        }
    }

    // 删, 根据值删除
    public boolean removeByVal(V value) {
        boolean flag = false;
        for (int i = 0; i < this.size; i++) {
            if (value.equals(this.data[i])) {
                for (int j = i + 1; j < size; j++) {
                    this.data[j - 1] = this.data[j];
                }
                this.size--;
                // 判断是否需要进行缩绒
                if (this.size <= this.capacity / 4 && this.capacity >= 10) {
                    resize(this.capacity >> 1);
                }
                flag = true;
            }
        }
        return flag;
    }

    // 改
    public void setValue(V value, int index) {
        if (index >= 0 && index < size) {
            this.data[index] = value;
        }
    }

    // 查, 根据索引查找值
    public V getValueByIndex(int index) {
        if (index >= 0 && index < size) {
            return this.data[index];
        }
        return null;
    }

    // 查, 根据值返回索引, 如果没有找到返回-1
    public int getIndex(V value) {
        for (int i = 0; i < this.size; i++) {
            if (value.equals(this.data[i])) {
                return i;
            }
        }
        return -1;
    }

    // 判空
    public boolean isEmpty() {
        return this.size == 0;
    }

    // 返回实际存储元素个数
    public int getSize() {
        return this.size;
    }

    @Override
    public String toString() {
//        return Arrays.toString(this.data); 这是java源码提供的一个toString方法

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < this.size; i++) {
            stringBuilder.append(this.data[i]);
            if (i == this.size - 1) {
                break;
            }
            stringBuilder.append(",");
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}

// MyArray测试类
public class MyArrayTest {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            myArray.add(random.nextInt(20));
        }
        myArray.add(random.nextInt(20));
        System.out.println("size = " + myArray.getSize());// 获取数组元素个数
        System.out.println(myArray);

        System.out.println(myArray.removeByVal(10));// 删除指定元素值, 如果删除成功返回true, 反之false
        System.out.println("size = " + myArray.getSize());
        System.out.println(myArray);

        System.out.println("data[5] = " + myArray.getValueByIndex(5));// 获取指定索引的值
        System.out.println("被删除的值:" + myArray.removeByIndex(5));// 删除制定索引元素并返回该值
        System.out.println(myArray);

        myArray.setValue(100, 5);// 将索引为5的位置元素值改为100
        System.out.println(myArray);

        myArray.insert(250,5);// 将250插入到索引为5的地方
        System.out.println(myArray);
    }
}
运行结果:

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Black—slience

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值