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

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章