目录
Java的原生数组是固定容量的,所以需要自己手动来写数组的扩容功能。
而JavaScript的原生数组是动态容量的,直接对其一顿操作就好了。。。
一、Java实现自定义数组
package array;
public class Array<T> {
private T[] a;
private int size;
public Array(int capacity) {
a = (T[]) new Object[capacity];
}
public Array() {
this(10);
}
// 获取数组中的元素个数
public int size() {
return size;
}
// 获取数组容量
public int getCapacity() {
return a.length;
}
// 判断数组是否为空
public boolean isEmpty() {
return size == 0;
}
// 往数组末尾添加一个元素
public void addLast(T e) {
add(size, e);
size++;
}
// 往数组头部插入一个元素
public void addFirst(T e) {
add(0, e);
size++;
}
// 在第index个位置插入一个元素
public void add(int index, T e) {
if(index < 0 || index > size) {
throw new RuntimeException("Add faild, Require index >= 0 and index <= size");
}
// 数组扩容
if(size == a.length) {
resize(2 * a.length);
}
for(int i = size - 1; i >= index; i--) {
a[i + 1] = a[i];
}
a[index] = e;
}
// 获取index索引位置的元素
public T get(int index) {
if(index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal.");
}
return a[index];
}
// 获取数组末尾的元素
public T getLast() {
return get(size - 1);
}
// 获取数组头部的元素
public T getFirst() {
return get(0);
}
// 修改index索引位置的元素为e
public void set(int index, T e) {
if(index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal.");
}
a[index] = e;
}
// 查找数组中是否有元素e
public boolean contains(T e) {
for(int i = 0; i < size; i++) {
if(a[i].equals(e)) {
return true;
}
}
return false;
}
// 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(T e) {
for(int i = 0; i < size; i++) {
if(a[i].equals(e)) {
return i;
}
}
return -1;
}
// 从数组中删除index位置的元素,返回删除的元素
public T remove(int index) {
if(index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal.");
}
T ret = a[index];
for(int i = index + 1; i < size; i++) {
a[i - 1] = a[i];
}
size--;
a[size] = null;
// 数组缩容
if(size == a.length / 4 && a.length / 2 != 0) {
resize(a.length / 2);
}
return ret;
}
// 从数组中删除第一个元素,返回删除的元素
public T removeFirst() {
return remove(0);
}
// 从数组中删除最后一个元素,返回删除的元素
public T removeLast() {
return remove(size - 1);
}
// 从数组中删除元素e
public void removeElement(T e) {
int index = find(e);
if(index != -1) {
remove(index);
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d , capacity = %d\n", size, a.length));
res.append("[");
for(int i = 0; i < size; i++) {
res.append(a[i]);
if(i != size -1) {
res.append(", ");
}
}
res.append("]");
return res.toString();
}
// 数组扩容
private void resize(int newCap) {
T[] newArr = (T[])new Object[newCap];
for(int i = 0; i < size; i++) {
newArr[i] = a[i];
}
a = newArr; // 让a重新指向newArr
}
}
二、JavaScript实现自定义数组
function MyArray(){
let a = [];
// 动态原型模式
if(typeof this.size !== "function"){
// 获取数组的元素个数
MyArray.prototype.size = function(){
return a.length;
};
// 判断数组是否为空
MyArray.prototype.isEmpty = function(){
return a.length == 0;
};
// 往数组末尾插入一个元素
MyArray.prototype.addLast = function(e){
a.push(e);
};
// 往数组头部插入一个元素
MyArray.prototype.addFirst = function(e){
a.unshift(e);
};
// 在数组的第index个位置插入一个元素
MyArray.prototype.add = function(index, e){
for(let i = index; i < a.length; i++){
a[i + 1] = a[i];
}
a[i] = e;
};
// 获取数组末尾的元素
MyArray.prototype.getLast = function(){
return a[length - 1];
};
// 获取数组头部的元素
MyArray.prototype.getFirst = function(){
return a[0];
};
// 获取index索引位置的元素
MyArray.prototype.get = function(index){
return a[index];
};
// 修改index索引位置的元素为e
MyArray.prototype.set = function(index, e){
a[index] = e;
};
// 查找数组中是否有元素e
MyArray.prototype.contains = function(e){
return a.indexOf(e) !== -1;
};
// 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
MyArray.prototype.find = function(e){
return a.indexOf(e);
};
// 从数组中删除最后一个元素,并返回删除的元素
MyArray.prototype.removeLast = function(){
return a.pop();
};
// 从数组中删除第一个元素,并返回删除的元素
MyArray.prototype.removeFirst = function(){
return a.shift();
};
// 从数组中删除index位置的元素,返回删除的元素
MyArray.prototype.remove = function(index){
if(index < 0 || index >= a.length){
throw new Error("Get failed. Index is illegal.");
}
let e = a[index];
a.splice(index, 1);
return e;
};
// 从数组中删除元素e
MyArray.prototype.removeElement = function(e){
let index = a.indexOf(e);
};
// 重写toString()方法
MyArray.prototype.toString = function(){
return "Array: size = " + a.length + "\n[" + a.toString() + "]";
};
}
};