一个可以变长,变维的数组(只可以变大),用来替代多维数组
基本做法与Array相似
set(Object value, int... pos):放入数值,第一个参数是要放入的内容,其余是下标
get(int... pos):取出数值,参数是下标,返回那个下标指定的数值
class Array {
private Object[] array = new Object[0];
private int length = 0;
public void set(Object value, int... pos) {
set(value,pos,0);
}
private void set(Object value,int[] pos, int begin) {
if(pos[begin] >= array.length) {
changeCapacity(pos[begin] + getNewCount(array.length));
length = pos[begin] + 1;
}
if(pos.length == begin+1) array[pos[begin]] = value;
else {
if(array[pos[begin]] == null) array[pos[begin]] = new Array();
((Array)array[pos[begin]]).set(value,pos,++begin);
}
}
public Object get(int... pos) {
return get(pos,0);
}
private Object get(int[] pos, int begin){
if(pos.length == begin+1) return array[pos[begin]];
else return ((Array)array[pos[begin]]).get(pos,++begin);
}
public int getLength(int... pos) {
return getLength(pos,0);
}
public int getLength(int[] pos, int begin) {
if(pos.length == begin) return length;
else return ((Array)array[pos[begin]]).getLength(pos,++begin);
}
private void changeCapacity(int length) {
Object[] temp = new Object[length];
for(int i=0; i<array.length; i++) {
temp[i] = array[i];
}
array = temp;
}
private int getNewCount(int lenght) {
return 10;
}
}