Java经典安全数组实现的代码

把做工程过程中比较常用的一些内容片段珍藏起来,如下内容内容是关于Java经典安全数组实现的内容,应该对大伙有好处。

final class DataSources {

private int size;
private DataSource[] data = new DataSource[4];

final int size(){
    return size;
}

final DataSource get(int idx){
    if (idx >= size)
        throw new IndexOutOfBoundsException("Index: "+idx+", Size: "+size);
    return data[idx];
}

final void add(DataSource table){
    if(size >= data.length ){
        DataSource[] dataNew = new DataSource[size << 1];
        System.arraycopy(data, 0, dataNew, 0, size);
        data = dataNew;
    }
    data[size++] = table;
}

}

猜你喜欢

转载自blog.51cto.com/14149868/2386135