超级数组,list底层实现模仿


public class SuperArray {

 // 定义属性
 private int[] array = new int[20];
 //数组中已经存放的元素
 private int size;

 // 定义行为
 // 增加
 public void add(int values) {
  if (this.size < this.array.length) {
   this.array[this.size] = values;
   this.size++;
  } else {
   int[] newArray = new int[this.array.length + 10];
   System.arraycopy(array, 0, newArray, 0, this.array.length);
   this.array = newArray;
   this.add(values);

  }
 }

 // 删除
 public void delete(int index) {
  if (index < this.size && index >= 0) {
   for (int i = index; i < this.size - 1; i++) {
    this.array[i] = this.array[i + 1];
   }
   this.size--;
  }
  if (this.array.length > 20 && (this.array.length - this.size) > 10) {
   int[] newArray = new int[this.array.length - 10];
   System.arraycopy(array, 0, newArray, 0, this.array.length - 10);
   this.array = newArray;
  }
 }

 // 查询
 public int get(int index) {
  if (index >= 0 && index < this.size) {
   return this.array[index];
  }
  throw new ArrayIndexOutOfBoundsException();
 }

 // 修改
 public void set(int index, int values) {
  if (index >= 0 && index < this.size) {
   this.array[index] = values;
  }
 }

 // 输出元素个数
 public int size() {
  return this.size;
 }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值