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;
    }

    }

  • 相关阅读:
    iOS真机调试 for Xcode 5
    iOS/iphone开发如何为苹果开发者帐号APPID续费
    unity3d中布娃娃系统
    U3D实现与iOS交互
    两种方法连接MySql数据库
    Unity3D Gamecenter 得分上传失败的处理
    Unity3.5 GameCenter基础教程(转载)
    判断数字正则表达式
    (转)SQL server 2005查询数据库表的数量和表的数据量
    C#操作PowerDesigner代码
  • 原文地址:https://www.cnblogs.com/fengshaolingyun/p/6785128.html
Copyright © 2011-2022 走看看