Choosing the correct structure
I'm a relative noob to Java, and am trying to store some attributes relating to table(s) i've loaded from the database.
I need to be able to store data about the table columns. ie. ColumnLabel, columnStyle, displayColumn, columnHref, displayColumn etc...
Initially I thought about a 2d Array of Hashmaps, where Array index one is the table number, and two is the column number. However I don't want to hardcode the max number of tables and/or columns.
So I have looked at an ArrayList of Hashmaps, which would work nicely for a single table, and set of columns.
My question is, because there are multiple tables being returned can I have an ArrayList of an ArrayList of Hashmaps? Anyone got any examples? Alternatively I am quite comfortable with having a max limit for the number of tables, as that will be fairly static in the Java, so would it be easier to have an Array of ArrayList of Hashmaps?
Or am going off at a tangent completely? Thanks in advance for any answers.
[1015 byte] By [
initssa] at [2007-10-2 12:51:05]

> I need to be able to store data about the table
> columns. ie. ColumnLabel, columnStyle, displayColumn,
> columnHref, displayColumn etc...
Just FYI You mentioned displayColumn twice here
> Initially I thought about a 2d Array of Hashmaps,
Personally I'd createa hashmap<String, Properties> where the string key is a combination of the table name and the column number.
> My question is, because there are multiple tables
> being returned can I have an ArrayList of an
> ArrayList of Hashmaps? Anyone got any examples?
I'm really not wild about this. Personally I don't like nesting collection classes but maybe that's just me.
> Alternatively I am quite comfortable with having a
> max limit for the number of tables, as that will be
> fairly static in the Java, so would it be easier to
> have an Array of ArrayList of Hashmaps?
Probably. Do you really need a second arraylist here?
This depends largely on how you obtain your table data.
If you have an existing DB whose table descriptions you need, you might be able to figure out the amount of tables and columns and create a 2D array to hold them.
If you have to add them in response to events while your executing, or getting the amounts is cumbersome (e.g., requires iterating over and counting tables and colums) you might be better of with an ArrayList.
Generally, it's not really robust to create "maximum sized" arrays that might not get filled completely.
DaanSa at 2007-7-13 10:03:10 >

The tables are static I have a prepared statement for each, and then call a generic routine to turn them into xHTML table statements.
The reason for this extra structure is to be able to apply further styling ie. Column Headers, URL's to the columns, decide whether to display columns or not.
So the structure I was thinking of and that is needed would be:
Table Fred (Table 1) // Array?
Column 1// ArrayList?
Label "Column Name" // hashmap?
URL"http:// ... "// hashmap
Display = "true" // hashmap
Column 2
Label "Column Name 2"
URL"http:// ... "
Display = "true"
Column 3
Label "Column Name 3"
URL"http:// ... "
Display = "true"
Table Bill (Table 2)
Column 1
Label "Column Name"
URL"http:// ... "
Display = "true"
Column 2
Label "Column Name 2"
URL"http:// ... "
Display = "true"
Column 3
Label "Column Name 3"
URL"http:// ... "
Display = "true"
There won't be massive volumes of data, say 5 tables with 8 columns in each, I don't really like the idea of concatenating table name with column number, because I still need a hashmap for each, combination.
Uhmmm, just a thought. But if columns always have the same set of properties, or very similar sets, why not create a Column class?
Then, depending on wether or not you can easily get the number of tables and the number of columns in each table, put them in 2d arrays or arraylists. A general rule-of-thumb for keeping lists that will not change at runtime: use arrays if you know the number of elements it will contain; use ArrayLists if you don't (or if determining the number of elements is expensive, e.g. requires iterating and counting)
DaanSa at 2007-7-13 10:03:10 >

That sounds like a plan! a Column class in a 2d array list. That way I can have a dynamic number of columns and tables.Do you have any example code? and I'll give you all my duke dollars (not sure what they do!)Cheers
Exactly what kind of examples are you looking for? Something like this?
public class ColumnInfo {
private String label;
private String url;
private boolean display;
String getLabel() {
return label;
}
void setLabel(String label) {
this.label = label;
}
//...repeat for other fields
}
public class MyColumnProcessor {
ArrayList<ArrayList><Column>> tables = new ArrayList<ArrayList><Column>>();
private void process() {
for (ArrayList<Column> table : tables) {
for (Column column : table) {
//process column, e.g.:
System.out.println("Processing column " + column.getLabel());
}
}
}
}
DaanSa at 2007-7-13 10:03:10 >

Thanks thats given me some good ideas + a starting point.
Sorry one last question - thinking about it I can extend and create a table class defined as:
class table
{
private String type;
private String caption;
private String insertURL;
private String style;
private String SQLStatement;
private String divStyle;
private column columns[];
}
class column
{
String label;
String column;
String url;
String style;
String width;
}
Would that be better structure than making use of ArrayLists for the columns, this way I define a table, with its columns + if I need more than one simply add the table class into an outer ArrayList.
Yes, this is definitely better. My nested ArrayLists solution assumed that tables had no extra properties you were interested in. You could even take it a step further and create a DataBase class to hold your tables :)
DaanSa at 2007-7-13 10:03:10 >

Not going to push it ! Working well thanks for the help.