• SuperArray


    package com.lovo.array;

    public class SuperIntArray {
    //属性
    public int[] array;

    private int index;//代表两层含义:1、下一个元素所在的下标;2、已经放了多少个元素。

    public SuperIntArray(){
    this.array = new int[20];
    }

    //行为
    //放入元素
    public void add(int num){
    if(this.index >= this.array.length){
    //扩容
    int[] newArray = new int[this.array.length + 10];
    System.arraycopy(this.array, 0, newArray, 0, this.array.length);
    this.array = newArray;
    }
    //把传入的num放入到array当中去
    this.array[index] = num;
    this.index++;
    }

    //得到某个元素
    public int get(int index){
    if(index < this.index && index >= 0){
    return this.array[index];
    }
    throw new ArrayIndexOutOfBoundsException(index);
    }

    //修改某个元素
    public void set(int index,int newNum){
    if(index < this.index && index >= 0){
    this.array[index] = newNum;
    }
    throw new ArrayIndexOutOfBoundsException(index);
    }

    //删除某个位置的元素
    public void remove(int index){
    if(index < this.index && index >= 0){
    System.arraycopy(this.array, index + 1, this.array, index , this.array.length - index - 1);
    this.index -- ;
    if(this.array.length - this.index >= 10 && this.array.length > 20){
    int[] newArray = new int[this.array.length - 10];
    System.arraycopy(this.array, 0, newArray, 0, newArray.length);
    this.array = newArray;
    }
    }
    throw new ArrayIndexOutOfBoundsException(index);
    }

    //获得元素的个数
    public int size(){
    return this.index;
    }

    public int getCapibility(){
    return this.array.length;
    }

    }

  • 相关阅读:
    液晶显示器分辨设置,显示器分辨率设置……
    如何显示语言栏
    查看一键Ghost的备份文件
    百度空间的变迁
    CentOS U盘安装
    Linux服务器系统选择
    博客一夜回到解放前
    spark常见的transformation和action算子
    二分查找
    9:两个栈实现一个队列
  • 原文地址:https://www.cnblogs.com/fengshaolingyun/p/6785128.html
Copyright © 2020-2023  润新知