wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Table data structure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Table data structure?" Watch "Table data structure?" New topic
Author

Table data structure?

Jake Boxer
Greenhorn

Joined: Oct 15, 2007
Posts: 1
Is there a data structure in Java (built in) that works like a table? Something where you could dynamically add rows and columns, and have both indexed by name?

I know I can create an array of arrays, but I'm interested in something that would allow me to specify a row and column name, and find a value at that index.

For example, in row "Bob", at column "Age", I could find the value 18. This works a lot like a database table, except for my purposes, I only need to index by Strings (both for rows and columns), and I only need the values to be ints.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

Originally posted by Jake Boxer:
Is there a data structure in Java (built in) that works like a table? Something where you could dynamically add rows and columns, and have both indexed by name?

I know I can create an array of arrays, but I'm interested in something that would allow me to specify a row and column name, and find a value at that index.

For example, in row "Bob", at column "Age", I could find the value 18. This works a lot like a database table, except for my purposes, I only need to index by Strings (both for rows and columns), and I only need the values to be ints.


You can take a look at the DefaultTableModel which is the model for the Swing JTable. It provides almost all the functionality you require. Of course you cannot interract with it using SQL, but then, if I understand your question correctly, that is not what you really require. You want some java object whose structure as well as data you want to dynamically modify.

Hope I didnt misunderstand your question and shoot off on a tangent!


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There's nothing in the collections library like that, but you can build a little class that works that way. Have you worked with Maps? A Map of Maps might work like:

You could eliminate the casting with generics and you really should check for nulls here and there. Does that look useful?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Apache Commons Collections also has MultiKeyMap, you might be able to use that one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Table data structure?
 
Similar Threads
JTable: Get Row Index By Column(s) Value(s)
jtable
Trouble with model for calculating and adding table values dependent on multiple rows
Obtaining row id in JTable
Question about table model listener