📄 bdmapimpl.java
字号:
int xIndex = getIndex(xKeys, keyX),
yIndex = getIndex(yKeys, keyY);
if(xIndex!=-1 && yIndex!=-1) return contents[yIndex][xIndex];
else return null;
}
public Map get(Object key, int coordName) {
//creating returned map.
Map retval = null;
if(coordName == X){
retval = new HashMap(yKeys.length);
int index = getIndex(xKeys, key);
//key not found
if(index == -1) return null;
else{
//build returned map
for(int i=0; i<yKeys.length; i++){
retval.put(yKeys[i],contents[i][index]);
}
return retval;
}
}else if(coordName == Y){
retval = new HashMap(xKeys.length);
int index = getIndex(yKeys, key);
//key not found
if(index == -1) return null;
else{
//build returned map
for(int i=0; i<xKeys.length; i++){
retval.put(xKeys[i],contents[index][i]);
}
return retval;
}
}
return null;
}
/**returns size of this BDMap along specified coordinate. For example, if x coordinate
* is specified, where x is the coordinate for cells in a row of table represented by
* this BDMap, total number of columns is returned.
* @param coordName: name of the coordinate along which size must be given*/
public int size(int coordName){
if(coordName == X) return xKeys.length;
if(coordName == Y) return yKeys.length;
return 0;
}
//adds a row to bidimensional array
private void addRow(int index, Object[] row){
//index out of bounds, do nothing
if(index > contents.length || index <0) return;
if(contents.length == 0){
contents = new Object[][]{row};
return;
}
Object[][] newConts = new Object[contents.length+1][contents[0].length];
System.arraycopy(contents, 0, newConts, 0, index);
newConts[index] = row;
System.arraycopy(contents, index, newConts, index+1, contents.length-index);
contents = newConts;
}
//replaces a row into bidimensional array and returns former one
private Object[] replaceRow(int index, Object[] row){
//index out of bounds, do nothing
if(index >= contents.length || index <0) return null;
if(contents.length == 0){
return null;
}
//if row has not same size as it should, resize it
int correctRowSize = contents[0].length;
if(row.length != correctRowSize){
Object[] newRow = new Object[correctRowSize];
System.arraycopy(row, 0, newRow, 0, Math.min(correctRowSize, row.length));
row = newRow;
}
Object[] retval = contents[index];
contents[index] = row;
return retval;
}
//deletes a row from bidimensional array
private Object[] deleteRow(int index){
//index out of bounds, do nothing
if(index >= contents.length || index <0) return null;
if(contents.length == 0) return null;
Object[][] newConts = new Object[contents.length-1][contents[0].length];
Object[] retVal = contents[index];
System.arraycopy(contents, 0, newConts, 0, index);
System.arraycopy(contents, index+1, newConts, index, newConts.length-index);
contents = newConts;
return retVal;
}
//replaces a column into bidimensional array and returns former one
private Object[] replaceColumn(int index, Object[] column){
if(contents.length == 0){
return null;
}
//index out of bounds, do nothing
if(index >= contents[0].length || index <0) return null;
//if row has not same size as it should, resize it
int correctColSize = contents.length;
if(column.length != correctColSize){
Object[] newCol = new Object[correctColSize];
System.arraycopy(column, 0, newCol, 0, Math.min(correctColSize, column.length));
column = newCol;
}
Object[] retval = contents[index];
for(int i=0; i<contents.length; i++){
retval[i] = contents[i][index];
contents[i][index] = column[i];
}
return retval;
}
//adds a column to the bidimensional array
private void addColumn(int index, Object[] column){
if(contents.length == 0){
contents = new Object[column.length][1];
for(int i=0; i<contents.length; i++)contents[i][0]=column[i];
}
//index out of bounds, do nothing
if(index > contents[0].length || index <0) return;
for(int i=0; i<contents.length; i++){
contents[i] = add(contents[i], index, column[i]);
}
}
//deletes a column from bidimensional array
private Object[] deleteColumn(int index){
if(contents.length == 0) return null;
//index out of bounds, do nothing
if(index >= contents[0].length || index <0) return null;
Object[] retval = new Object[contents.length];
for(int i=0; i<contents.length; i++){
retval[i] = contents[i][index];
contents[i] = delete(contents[i], index);
}
return retval;
}
//inserts a null value to specified array at specified index resizing it
private static Object[] add(Object[] array, int index, Object toAdd){
if(index < 0 || index > array.length ) return array;
if(array.length == 0) return new Object[]{toAdd};
Object[] newArray = new Object[array.length+1];
System.arraycopy(array, 0, newArray, 0, index);
newArray[index] = toAdd;
System.arraycopy(array, index, newArray, index+1, array.length-index);
return newArray;
}
//deletes an object from specified array at specified index
private static Object[] delete(Object[] array, int index){
if(index < 0 || index >= array.length ) return null;
if(array.length == 0) return new Object[0];
Object[] newArray = new Object[array.length-1];
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index+1, newArray, index, newArray.length-index);
return newArray;
}
public static void main(String[] args){
BDMapImpl bdmi = new BDMapImpl();
int testSize = 3, tstSize2 = 2;
for(int i=0; i<testSize; i++){
for(int j=0; j<testSize; j++){
String str = "elemento ("+i+","+j+")";
System.out.println("adding "+str);
System.out.println("xKeys: "+bdmi.xKeys.length+", yKeys: "+bdmi.yKeys.length+" elements");
bdmi.put(new Integer(i), new Integer(j), str);
System.out.println("xKeys: "+bdmi.xKeys.length+", yKeys: "+bdmi.yKeys.length+" elements");
}
}
for(int i=0; i<tstSize2; i++){
for(int j=0; j<tstSize2; j++){
String str = "element ("+i+","+j+")";
System.out.println("adding "+str);
System.out.println("xKeys: "+bdmi.xKeys.length+", yKeys: "+bdmi.yKeys.length+" elements");
bdmi.put(new Integer(i), new Integer(j), str);
System.out.println("xKeys: "+bdmi.xKeys.length+", yKeys: "+bdmi.yKeys.length+" elements");
}
}
for(int i=0; i<testSize; i++){
for(int j=0; j<testSize; j++){
String str = "elemento ("+i+","+j+")";
System.out.println("requiring element "+str+": "+bdmi.get(new Integer(i), new Integer(j)));
}
}
for(int i=0; i<testSize; i++){
for(int j=0; j<testSize; j++){
String str = "elemento ("+i+","+j+")";
System.out.println("deleting "+str);
System.out.println("xKeys: "+bdmi.xKeys.length+", yKeys: "+bdmi.yKeys.length+" elements");
bdmi.remove(new Integer(i), new Integer(j));
System.out.println("xKeys: "+bdmi.xKeys[i]+", yKeys: "+bdmi.yKeys[j]);
}
}
for(int i=0; i<testSize; i++){
for(int j=0; j<testSize; j++){
String str = "elemento ("+i+","+j+")";
System.out.println("requiring element "+str+": "+bdmi.get(new Integer(i), new Integer(j)));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -