zoukankan      html  css  js  c++  java
  • 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;
    }

    }

  • 相关阅读:
    导入sql错误
    导入sql错误
    导入sql错误
    导入sql错误
    【翻译】ASP.NET Web API是什么?
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    python中pip安装、升级、升级指定的包
  • 原文地址:https://www.cnblogs.com/fengshaolingyun/p/6785128.html
Copyright © 2011-2022 走看看