一个简单的矩阵存储模型类

  昨天一个同事问了一个问题,涉及到如何设计一个矩阵存储模型的问题,当时想了一些,但都不能很好的解决这个问题,也比较复杂,后来仔细想了一下,便写了下面这个类,比较简单,也能满足基本的应用。

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值