大家都是行家,我就直接上代码了,我这个代码应该还是能看懂的,嘻嘻….
   1:  import java.util.ArrayList;
   2:  import java.util.List;
   3:   
   6:   
   7:  public final class DataTable {
   8:   
   9:      private DataRowCollection rows; //用于保存DataRow的集合对象
  10:      private DataColumnCollection columns; //用于保存DataColumn的对象
  11:      private String tableName; //表名
  12:      private boolean readOnly = false;
  13:      private int nextRowIndex = 0;
  14:      private DataExpression dataExpression;
  15:      private Object tag;
  16:   
  17:      public DataTable() {
  18:          this.columns = new DataColumnCollection();
  19:          this.rows = new DataRowCollection();
  20:          this.rows.setColumns(columns);
  21:          dataExpression = new DataExpression(this);
  22:      }
  23:   
  24:      public DataTable(String dataTableName) {
  25:          this();
  26:          this.tableName = dataTableName;
  27:      }
  28:   
  29:      public int getTotalCount() {
  30:          return rows.size();
  31:      }
  32:   
  33:      public boolean isReadOnly() {
  34:          return this.readOnly;
  35:      }
  36:   
  37:      public void setReadOnly(boolean readOnly) {
  38:          this.readOnly = readOnly;
  39:      }
  40:   
  41:      /**  
  42:       * 功能描述:  返回表名
  43:       * @param    
  44:       */
  45:      public String getTableName() {
  46:          return this.tableName;
  47:      }
  48:   
  49:      /**  
  50:       * 功能描述:  设置表名
  51:       * @param    
  52:       */
  53:      public void setTableName(String tableName) {
  54:          this.tableName = tableName;
  55:      }
  56:   
  57:      /**  
  58:       * 功能描述:  返回该表引用的封装类
  59:       * @param
  60:       * @return: DataRowCollection    
  61:       */
  62:      public DataRowCollection getRows() {
  63:          return this.rows;
  64:      }
  65:   
  66:      public DataColumnCollection getColumns() {
  67:          return this.columns;
  68:      }
  69:   
  70:      /**  
  71:       * 功能描述:  获取指定行指定列的数据
  72:       * @param
  73:       * @return: Object    
  74:       */
  75:   
  76:      public Object getValue(int row,
  77:              String colName) {
  78:          return this.rows.get(row).getValue(colName);
  79:      }
  80:   
  81:      public Object getValue(int row,
  82:              int col) {
  83:          return this.rows.get(row).getValue(col);
  84:      }
  85:   
  86:      /**  
  87:       * 功能描述:  为该表数据新建一行
  88:       * @param
  89:       * @return: DataRow     
  90:       */
  91:      public DataRow newRow() throws Exception {
  92:          DataRow tempRow = new DataRow(this);
  93:          nextRowIndex = nextRowIndex < this.rows.size() ? this.rows.size()
  94:                  : nextRowIndex;
  95:          tempRow.setColumns(this.columns);
  96:          tempRow.setRowIndex(nextRowIndex++);
  97:          return tempRow;
  98:      }
  99:   
 100:      public void setValue(int row,
 101:              int col,
 102:              Object value) {
 103:          this.rows.get(row).setValue(col, value);
 104:      }
 105:   
 106:      public void setValue(int row,
 107:              String colName,
 108:              Object value) {
 109:          this.rows.get(row).setValue(colName, value);
 110:      }
 111:   
 112:      /**  
 113:       * @param tag
 114:       */
 115:      public void setTag(Object tag) {
 116:          this.tag = tag;
 117:      }
 118:   
 119:      /**  
 120:       * @return  the tag   
 121:      */
 122:      public Object getTag() {
 123:          return tag;
 124:      }
 125:   
 126:      public DataColumn addColumn(String columnName,
 127:              int dataType) throws Exception {
 128:          return this.columns.addColumn(columnName, dataType);
 129:      }
 130:   
 131:      public boolean addRow(DataRow row) throws Exception {
 132:          if (row.getRowIndex() > this.rows.size())
 133:              row.setRowIndex(this.rows.size());
 134:          return this.rows.add(row);
 135:      }
 136:   
 137:      //以下为数据表扩展方法实现集合
 138:      /**  
 139:       * 功能描述:  返回符合过滤条件的数据行集合,并返回
 140:       * @param
 141:       * @return: DataTable    
 142:       */
 143:      public List<DataRow> select(String filterString) {
 144:          List<DataRow> rows = new ArrayList<DataRow>();
 145:          if (StringUtil.isNotEmpty(filterString)) {
 146:              for (Object row : this.rows) {
 147:                  DataRow currentRow = (DataRow) row;
 148:                  if ((Boolean) dataExpression.compute(filterString,
 149:                          currentRow.getItemMap())) {
 150:                      rows.add(currentRow);
 151:                  }
 152:              }
 153:              return rows;
 154:          } else {
 155:              return this.rows;
 156:          }
 157:      }
 158:   
 159:      /**  
 160:       * 功能描述:  对当前表进行查询 过滤,并返回指定列集合拼装的DataTable对象
 161:       * @param
 162:       * @return: DataTable    
 163:       */
 164:      public DataTable select(String filterString,
 165:              String[] columns,
 166:              boolean distinct) throws Exception {
 167:          DataTable result = new DataTable();
 168:          List<DataRow> rows = select(filterString);
 169:          //构造表结构
 170:          for (String c : columns) {
 171:              DataColumn dc = this.columns.get(c);
 172:              DataColumn newDc = new DataColumn(dc.getColumnName(),
 173:                      dc.getDataType());
 174:              newDc.setCaptionName(dc.getCaptionName());
 175:              result.columns.add(newDc);
 176:          }
 177:          //填充数据
 178:          for (DataRow r : rows) {
 179:              DataRow newRow = result.newRow();
 180:              newRow.copyFrom(r);
 181:              result.addRow(newRow);
 182:          }
 183:          return result;
 184:      }
 185:   
 186:      public DataTable select(String tableName,
 187:              String selectField,
 188:              String filterString,
 189:              String groupField) {
 190:          DataTable result = new DataTable();
 191:          //
 192:          return result;
 193:      }
 194:   
 195:      /**  
 196:       * 功能描述:  根据指定表达式对符合过滤条件的数据进行计算
 197:       * @param
 198:       * @return: Object
 199:       * @author: James Cheung
 200:       * @version: 2.0 
 201:       */
 202:      public Object compute(String expression,
 203:              String filter) {
 204:          return dataExpression.compute(expression, select(filter));
 205:      }
 206:   
 207:      public Object max(String columns,
 208:              String filter) {
 209:          return null;
 210:      }
 211:   
 212:      public Object min(String columns,
 213:              String filter) {
 214:          return null;
 215:      }
 216:   
 217:      public Object avg(String columns,
 218:              String filter) {
 219:          return null;
 220:      }
 221:   
 222:      public Object max(String columns,
 223:              String filter,
 224:              String groupBy) {
 225:          return null;
 226:      }
 227:   
 228:      public Object min(String columns,
 229:              String filter,
 230:              String groupBy) {
 231:          return null;
 232:      }
 233:   
 234:      public Object avg(String columns,
 235:              String filter,
 236:              String groupBy) {
 237:          return null;
 238:      }
 239:   
 240:      private List<DataColumn> getColumns(String colString) {
 241:          List<DataColumn> columns = new ArrayList<DataColumn>();
 242:   
 243:          return columns;
 244:      }
 245:  }

(转)在JAVA实现DataTable对象(三)——DataTable对象实现的更多相关文章

  1. ADO.NET对象之 DataTable

    ADO.NET可以在与数据库断开连接的方式下通过DataSet或DataTable对象进行数据处理,当需要更新数据时才重新与数据源进行连接,并更新数据源. DataTable对象表示保存在本机内存中的 ...

  2. [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  3. DataTable转换为Model实体对象

    记得在学校的时候,接触得最多的就是SqlHelper,每次在读取的时候不管是DataTable还是DataReader转换为实体对象的时候是最恼火的,因为要写很多代码,而且没有什么意义.后面接触到了反 ...

  4. 简单的反射 把datatable 转换成list对象

    /// <summary> /// 把datatable 转换成list对象 /// </summary> /// <typeparam name="T&quo ...

  5. JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码

    JAVA之旅(三十)--打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码 三十篇了,又是一个 ...

  6. JAVA写JSON的三种方法,java对象转json数据

    JAVA写JSON的三种方法,java对象转json数据 转自:http://www.xdx97.com/#/single?bid=5afe2ff9-8cd1-67cf-e7bc-437b74c07a ...

  7. 将DataTable转换为List<T>对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。

    可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm 但是该方法在DataTable里某个字段类型是 ...

  8. 【转】给DataTable和DataRow扩展方法,直接转换为对象集合或对象

    /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为对象集合或对象 /// 补充说明:此扩展类可以极大的简化操作,但是性能低下,大数据以 ...

  9. java并发编程实战:第三章----对象的共享

    我们不仅仅希望防止某个线程使用某个状态时,另一个线程在修改它:我们还希望某个线程修改了某个状态后,其他线程能够看到状态的变化. 一.可见性 重排序:在没有同步的情况下,编译器.处理器可能对代码的执行顺 ...

  10. C# 数组转换为DataTable 的三个方法

    C# 数组转换为DataTable 的三个方法   using System; using System.Data; namespace ArrayToDataTable { class ArrayT ...

随机推荐

  1. Hibernate学习笔记2.4(Hibernate的Id生成策略)

    通过设置告诉id该怎么设置. 1.通过xml方式 1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据 ...

  2. ArcGIS Python编程案例-电子资料链接

    ArcGIS Python编程案例(1)-Python语言基础 https://www.jianshu.com/p/dd90816d019b ArcGIS Python编程案例(2)-使用ArcPy编 ...

  3. windows中 git 命令使用记录

    建议国内开发安装淘宝npm镜像cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 或者每次执行安装时 npm ins ...

  4. JavaScript中的构造函数 renturn

    javascript中构造函数是不需要有返回值的,但是如果其中添加了 return 语句结果会如何呢: 看如下代码: 示例1. var Calculator = function () { retur ...

  5. 吴裕雄 python深度学习与实践(1)

    #coding = utf8 import threading,time count = 0 class MyThread(threading.Thread): def __init__(self,t ...

  6. 学JS的心路历程-Promise(一)

    今天在进入Promise代码之前,我们先来用个例子来解释Promise是什么. 未来值 假设我们今天来到快餐店,点了一个汉堡,付钱给店员. 点了餐点并付费,可以理解为我们发送了一个请求,希望得到一个回 ...

  7. java中几个happens-before规则

    1. 程序顺序规则:一个线程中的每个操作,happens-before 该线程中的任意后续操作. 2.监视器锁规则:对一个锁的解锁, happens-before 于随后对这个锁的加锁操作 3.vol ...

  8. js实现多级复选框的交互

    功能介绍   整个复选框是包含多级,可能有父级,可能有子级,在勾选复选框时,要做两种判断: 1要判断它下面有没有子级,有子级将子级的选中状态checked变得和自己一样. 2要判断它是否有父级,有父级 ...

  9. centos7 搭建keepalived+Nginx+tomcat

    准备1台 192.168.2.224  安装Nginx,2台安装tomcat   192.168.2.222   192.168.2.223 1.安装Nginx: 上传pcre-8.36.tar.gz ...

  10. Attention Please

    关于BJJ与Matlab的学习时间安排在五六日晚间: 其余一切重心在学术!