Tables

Table is an interface that defines the structure of the data types used to represent tabular data. The general idea of a Table is that it represents data in rows and columns, where each column represents one data attribute, and each row contains one record of data. For example, envision a database containing information about products shipped out of a doodad manufacturing facility. Doodads come in a variety of models, colors. Now, imagine one of the tables of our database has a daily record of the number of each of the doodad models and color shipped on each day. Our table would look something like this:

Model Number
Color
Number Shipped
2345 green
29
2344 white
12
2344 black
12

So the representation of this row might represent the Model Number as integers, the Color as strings, and the number shipped as an integer. The table to represent this data would then contain a column of integers, then a column of strings followed by a column of integers.

So tables are actually a collection of columns of data. Internally there is not requirement that the data actually be stored in columns. The default implementation stores the data this way for efficiency reasons, but there are obviously alternatives.

Methods of Table

I won't go into great detail on the methods in table as they are quite numerous, and that would be beyond the scope of this document. However, it is very important to know the methods that get the data in the table. They are enumerated here:

  public Object getObject(int row, int column);
  public int getInt(int row, int column);
  public short getShort(int row, int column);
  public float getFloat(int row, int column);
  public double getDouble(int row, int column);
  public long getLong(int row, int column);
  public String getString(int row, int column);
  public byte[] getBytes(int row, int column);
  public boolean getBoolean(int row, int column);
  public char[] getChars(int row, int column);
  public void setObject(Object element, int row, int column);

In the case of a MutableTable, there are also methods to set the values:

  public void setInt(int data, int row, int column);
  public void setShort(short data, int row, int column);
  public void setFloat(float data, int row, int column);
  public void setDouble(double data, int row, int column);
  public void setLong(long data, int row, int column);
  public void setString(String data, int row, int column);
  public void setBytes(byte[] data, int row, int column);
  public void setBoolean(boolean data, int row, int column);
  public void setChars(char[] data, int row, int column);

It is pretty obvious that you can set any row/column value to just about any data type you want. If the type of the column can not represent the data type passed, an exception will be thrown. Otherwise, the column object will convert the data appropriately and store it. The same is true of getting data, wherever possible, the data will be converted to the appropriate type and returned.

There are also some other useful methods of table that operate on columns. Here is the column data accessor method:

  public void getColumn(Object buffer, int pos);

This method will return the data from a column in the provided data array (the first input parameter).

If the table is mutable then you can change columns using the following methods:

  public void setColumn(<array of data type>, int position);
  public void addColumn(<array of data type>);
  public void insertColumn(<array of data type>, int position);

The first method will set the column at the given index. The next to last method will append a new column, the last method will insert a new column.

And if you want to trash columns of a MutableTable, we can do that to:

  public void removeColumn(int pos);
  public void removeColumns(int start, int len);


The first form removes the column at pos, the second form removes len columns starting at start.

Each column of a table can also be labeled. In the case of the example table above, we would name the columns "Model Number", "Color" and "Number Shipped". These methods allow this:

  public String getColumnLabel(int which);

And for MutableTable:

  public void setColumnLabel(String label, int which);

The first method returns the label for column indexed by which, the second sets the label for column which to label.

And if you want to change the number of columns or get the number of columns, there are the following:

  public int getNumColumns();

And for MutableTable:

  public void setNumColumns(int numColumns);

The first returns the number of columns, the second sets the number of columns.

The Table interface also provides a mechanism to mark values of the Table as "missing" or "empty". A missing value is one for which the true value is not available. An empty value is one in which the emptiness has meaning. Like leaving an entry in a shoes column empty to signify being barefoot.

The following methods tell us if a value is missing or empty:

  public boolean isValueMissing(int row, int col);
  public boolean isValueEmpty(int row, int col);

Mutable table provides methods to set these values:

  public void setValueToMissing(boolean b, int row, int col);
  public void setValueToEmpty(boolean b, int row, int col);