自定义数组类,实现数组的增删改查
使用java
增加 :指定位置的,包括后面的元素,统一向后移动,并且size++,如果大小超过容量,就扩容 =>均摊复杂度O(1)
删除:指定位置后面的元素,统一向前移动一格,并且size--,如果大小小于原来容量的1/4.就缩容=>均摊复杂度是O(1)
修改:指定元素修改 O(1)
查询:查询,
package com.zggj.modify.common; public class Array { private int[] data;//存储数据的数组 private int size;//数组目前有多少个元素 //构造函数 :根据传入的容量量初始化 public Array(int capacity) { data = new int[capacity]; size = 0; } //无参构造函数.默认容量为10 public Array() { this(10); } //获取size public int getSize() { return size; } // 获取数组中的数据容量(最多可以容纳多少元素) public int getCapacity() { return data.length; } //判断数组中数据是否为0,直接判断数组的实时大小size即可 public boolean isEmpty() { return size == 0; } //获取指定位置的元素 public int get(int index) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } return data[index]; } public int getFirst() { return get(0); } public int getLast() { return get(size - 1); } //判断是否包含指定元素 public boolean contain(int element) { for (int i = 0; i < size; i++) { if (data[i] == element) { return true; } } return false; } // 查找指定元素 返回索引 public int find(int element) { for (int i = 0; i < size; i++) { if (data[i] == element) { return i; } } return -1; } // 向data中的index位置插入一个元素e public void insert(int index, int element) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } //如果容量已满,扩容 if (size == data.length) { resize(data.length * 2); } // index后面的所有元素后移 for (int i = size; i > index; i--) { data[i] = data[i - 1]; } data[index] = element; size++; } public void addFirst(int element) { insert(0, element); } public void addLast(int element) { insert(size, element); } // 从数组中删除index位置的元素,并返回删除的元素 public int remove(int index) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } int ret = data[index];//删除的要返回 if (size < data.length / 4 && data.length / 2 != 0) { resize(data.length / 2); } for (int i = index; i < size; i++) { data[i] = data[i + 1]; } size--; return ret; } public int removeFirst() { return remove(0); } public int removeLast() { return remove(size - 1); } //移除指定元素 public void removeElement(int element) { int index = find(element); if (index != -1) {//找到,就移除 remove(index); } } //扩容或者缩容 public void resize(int capacity) { int[] newArr = new int[capacity]; for (int i = 0; i < size; i++) { newArr[i] = data[i]; } data = newArr; } //更新 public void set(int index, int element) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } data[index] = element; } public void setFirst(int element) { set(0, element); } public void setLast(int element) { set(size - 1, element); } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(String.format("Array:size:%d,capacity:%d ", size, getCapacity())); builder.append("["); for (int i = 0; i < size; i++) { builder.append(data[i] + ","); } builder.append("]"); return builder.toString(); } //测试代码的main函数 public static void main(String[] args) { Array array = new Array(); for (int i = 0; i < array.getCapacity(); i++) { array.addLast(i); System.out.print(array.toString()); } array.insert(3, 99); System.out.print(array.toString()); array.remove(2); System.out.print(array.toString()); array.removeFirst(); System.out.print(array.toString()); array.removeLast(); System.out.print(array.toString()); } }
使用泛型改写数组
package com.zggj.modify.common; public class Array<E> { private E[] data;//存储数据的数组 private int size;//数组目前有多少个元素 //构造函数 :根据传入的容量量初始化 public Array(int capacity) { data = (E[])new Object[capacity]; size = 0; } //无参构造函数.默认容量为10 public Array() { this(10); } //获取size public int getSize() { return size; } // 获取数组中的数据容量(最多可以容纳多少元素) public int getCapacity() { return data.length; } //判断数组中数据是否为0,直接判断数组的实时大小size即可 public boolean isEmpty() { return size == 0; } //获取指定位置的元素 public E get(int index) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } return data[index]; } public E getFirst() { return get(0); } public E getLast() { return get(size - 1); } //判断是否包含指定元素 public boolean contain(E element) { for (int i = 0; i < size; i++) { if (data[i] == element) { return true; } } return false; } // 查找指定元素 返回索引 public int find(E element) { for (int i = 0; i < size; i++) { if (data[i] == element) { return i; } } return -1; } // 向data中的index位置插入一个元素e public void insert(int index, E element) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } //如果容量已满,扩容 if (size == data.length) { resize(data.length * 2); } // index后面的所有元素后移 for (int i = size; i > index; i--) { data[i] = data[i - 1]; } data[index] = element; size++; } public void addFirst(E element) { insert(0, element); } public void addLast(E element) { insert(size, element); } // 从数组中删除index位置的元素,并返回删除的元素 public E remove(int index) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } E ret = data[index];//删除的要返回 if (size < data.length / 4 && data.length / 2 != 0) { resize(data.length / 2); } for (int i = index; i < size; i++) { data[i] = data[i + 1]; } size--; return ret; } public E removeFirst() { return remove(0); } public E removeLast() { return remove(size - 1); } //移除指定元素 public void removeElement(E element) { int index = find(element); if (index != -1) {//找到,就移除 remove(index); } } //扩容或者缩容 public void resize(int capacity) { E[] newArr = (E[])new Object[capacity]; for (int i = 0; i < size; i++) { newArr[i] = data[i]; } data = newArr; } //更新 public void set(int index, E element) { if (index < 0 || index > size) { throw new IllegalArgumentException("插入元素失败!index的范围必须在[0, size)"); } data[index] = element; } public void setFirst(E element) { set(0, element); } public void setLast(E element) { set(size - 1, element); } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(String.format("Array:size:%d,capacity:%d ", size, getCapacity())); builder.append("["); for (int i = 0; i < size; i++) { builder.append(data[i] + ","); } builder.append("]"); return builder.toString(); } //测试代码的main函数 public static void main(String[] args) { Array<Integer> array = new Array<Integer>(); for (int i = 0; i < array.getCapacity(); i++) { array.addLast(i); System.out.print(array.toString()); } array.insert(3, 99); System.out.print(array.toString()); array.remove(2); System.out.print(array.toString()); array.removeFirst(); System.out.print(array.toString()); array.removeLast(); System.out.print(array.toString()); } }