This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Table data structure?

 
Jake Boxer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apache Commons Collections also has MultiKeyMap, you might be able to use that one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic