単純なマトリックス記憶モデルクラス
昨日同僚が一つの問題について聞きましたが、どのようにマトリックスメモリーモデルを設計するかについての問題があります.その時はいくつか考えましたが、この問題をうまく解決できなくて、複雑でした.後でよく考えてみたら、次のような種類を書きました.比較的簡単で、基本的な応用にも満足できます.
次にアップグレードして、行全体の作業などを追加します.
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Dao
*/
public class RectStoreModel
{
private int maxRow = 0;
private int maxColumn = 0;
private Map objectMap = new HashMap();
public RectStoreModel()
{
}
public String getKey(int row, int column)
{
return row + "," + column;
}
public int getMaxRow()
{
return this.maxRow;
}
public void setMaxRow(int row)
{
if (row > this.maxRow)
{
this.maxRow = row;
}
}
public int getMaxColumn()
{
return this.maxColumn;
}
public void setMaxColumn(int column)
{
if (column > this.maxColumn)
{
this.maxColumn = column;
}
}
public Object retriveObject(int row, int column)
{
String key = getKey(row, column);
return this.objectMap.get(key);
}
public Object storeObject(int row, int column, Object object)
{
setMaxRow(row);
setMaxColumn(column);
String key = getKey(row, column);
return this.objectMap.put(key, object);
}
}
テストクラス/**
*
* @author Dao
*/
public class Main
{
public static void main(String[] args)
{
String str1 = "str1";
String str2 = "str2";
String str3 = "str3";
String str4 = "str4";
RectStoreModel rectStoreModel = new RectStoreModel();
rectStoreModel.storeObject(1, 10, str1);
rectStoreModel.storeObject(3, 33, str2);
rectStoreModel.storeObject(1, 39, str3);
rectStoreModel.storeObject(5, 20, str4);
int maxRow = rectStoreModel.getMaxRow();
int maxColumn = rectStoreModel.getMaxColumn();
for (int row = 0; row <= maxRow; row++)
{
for (int column = 0; column <= maxColumn; column++)
{
Object object = rectStoreModel.retriveObject(row, column);
System.out.print("[");
if (object != null)
{
System.out.print((String) object);
}
System.out.print("]");
}
System.out.println("");
}
}
}
テスト結果[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][str1][][][][][][][][][][][][][][][][][][][][][][][][][][][][][str3]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][str2][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][str4][][][][][][][][][][][][][][][][][][][]
次にアップグレードして、行全体の作業などを追加します.
import java.util.HashMap;
import java.util.Map;
/**
* , , , 、
* 、 , , ,
* @author Dao
*/
public class RectStoreModel
{
private int maxRow = 0;
private int maxColumn = 0;
private Map objectMap = new HashMap();
public RectStoreModel()
{
}
/**
*
*/
private String getKey(int row, int column)
{
return row + "," + column;
}
/**
* ,
*/
public void decreaseColumn()
{
removeColumn(this.maxColumn);
}
/**
* ,
*/
public void decreaseRow()
{
removeRow(this.maxRow);
}
/**
* , 0
*/
public int getMaxColumn()
{
return this.maxColumn;
}
/**
* , ,
* ,
*/
public void setMaxColumn(int column)
{
if (column > this.maxColumn)
{
this.maxColumn = column;
}
}
/**
* , 0
*/
public int getMaxRow()
{
return this.maxRow;
}
/**
* , ,
* ,
*/
public void setMaxRow(int row)
{
if (row > this.maxRow)
{
this.maxRow = row;
}
}
/**
* , getMaxColumn() ,getColumnCount() 1 ,
* + 1;
*/
public int getColumnCount()
{
return this.maxColumn + 1;
}
/**
* , getMaxRow() ,getRowCount() 1 ,
* + 1
*/
public int getRowCount()
{
return this.maxRow + 1;
}
/**
* ,
*/
public int getObjectCount()
{
int objectCount = 0;
for (int row = 0; row <= this.maxRow; row++)
{
for (int column = 0; column <= this.maxColumn; column++)
{
String key = getKey(row, column);
if (this.objectMap.containsKey(key))
{
objectCount++;
}
}
}
return objectCount;
}
/**
*
*/
public void increaseColumn()
{
this.maxColumn++;
}
/**
*
*/
public void increaseRow()
{
this.maxRow++;
}
/**
* ,
* @param insertColumn
*/
public void insertColumn(int insertColumn)
{
if (insertColumn > 0)
{
if (insertColumn <= this.maxColumn)
{
for (int row = 0; row <= this.maxRow; row++)
{
for (int column = this.maxColumn; column >= insertColumn; column--)
{
Object object = retriveObject(row, column);
storeObject(row, column + 1, object);
}
removeObject(row, insertColumn);
}
this.maxColumn++;
}
else
{
this.maxColumn = insertColumn;
}
}
}
/**
* ,
* @param inserRow
*/
public void insertRow(int inserRow)
{
if (inserRow > 0)
{
if (inserRow <= this.maxRow)
{
for (int row = this.maxRow; row >= inserRow; row--)
{
for (int column = 0; column <= this.maxColumn; column++)
{
Object object = retriveObject(row, column);
storeObject(row + 1, column, object);
if (row == inserRow)
{
removeObject(row, column);
}
}
}
}
else
{
this.maxRow = inserRow;
}
}
}
/**
*
*/
public Object removeObject(int row, int column)
{
String key = getKey(row, column);
return this.objectMap.remove(key);
}
/**
*
*/
public void removeColumn(int removeColumn)
{
if (removeColumn > 0 && removeColumn <= this.maxColumn)
{
for (int row = 0; row <= this.maxRow; row++)
{
for (int column = removeColumn; column < this.maxColumn; column++)
{
Object object = retriveObject(row, column + 1);
storeObject(row, column, object);
}
removeObject(row, this.maxColumn);
}
this.maxColumn--;
}
}
/**
*
*/
public void removeRow(int removeRow)
{
if (removeRow > 0 && removeRow <= this.maxRow)
{
for (int column = 0; column <= this.maxColumn; column++)
{
for (int row = removeRow; row < this.maxRow; row++)
{
Object object = retriveObject(row + 1, column);
storeObject(row, column, object);
}
removeObject(this.maxRow, column);
}
this.maxRow--;
}
}
/**
*
*/
public Object retriveObject(int row, int column)
{
String key = getKey(row, column);
return this.objectMap.get(key);
}
/**
*
*/
public Object storeObject(int row, int column, Object object)
{
setMaxRow(row);
setMaxColumn(column);
String key = getKey(row, column);
return this.objectMap.put(key, object);
}
}