File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Two dimensional data structures? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Two dimensional data structures?" Watch "Two dimensional data structures?" New topic

Two dimensional data structures?

John McGahee

Joined: Jan 10, 2003
Posts: 5
Can you have a 2 dimensional Vector/ArrayList etc?
Is there a better(growable) substitute for say
String[][] strArray = new String[x][x];
Object[][] objArray = new Object[x][x]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi John,
i dont know if there is direct substitute but i guess if we use Hashtable, HashMap etc then we can consider it as "two dim" array, can't we?
each key in the hashtable is index of the row. and the value is the one dim array. right?
though i don't know how far this would be good to us.
more thoughts?
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Hi John,
I think if isolated, it might be meaningless to say this data structure is good and that less good. It depends on what do you want to do with the data structure.
Relationship of general purpose data structure:
1.Small amount of data ? 2 : 3
2.Amount of data predicatable ? 4 : 6
3.Searching and insertion must be very fast ? 9 : 5
4.Search speed more important than insertion speed? 7 : 8
5.Key distribution guaranteed random? 10 : 11
6.Linked list
7.Ordered Array
8.Unordered array
9.Hash Table
10.Binary Search Tree
11.Balanced Tree

Relationship of external storage choice:
1. Speed important ? 2 : 4
2. Speed critical ? 3 : 5
3. Extra storage available ? 6 : 7
4. Sequential search
5. B-tree
6. External hashing
7. Indexed files
Concerning hash table, a hash table is based on an array. The range of key value is usually greater than the size of the array. A key value is hashed to an array index by a hash function. It�s not exactly like a two dimensional array but not necessarily better than a two dimensional array. Depends on what you want
Once you make your decision of what data structure to use, you can almost always invoke some worked packages to deal with it( I think most probably they would be in the Collection package in Java SDK ) or if you have much time, you can do all the low level constructions by yourself, algorithms available via google
Hope that could be of help.

[ January 17, 2003: Message edited by: Ellen Fu ]
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
A List of Lists would be one way to do this. But if what you really want is a two-dimensional array, then just use that and manually resize your arrays as needed.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
If I am not mistaken, you have up to n-dimensinal array.
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Java does not support n-dimensional arrays in the strict sense, but rather arrays of arrays (of arrays, etc.). A "true" n-dimensional array would enforce a single scalar for each dimension. That is, a 3 x 5 array would be a three-element array, where each element has 5 elements in the sub-array. Java does not enforce this behavior.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
But I guess I can still use a notation like:
Can I?
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Yep, you can.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Thanks again!
I agree. Here's the link:
subject: Two dimensional data structures?
jQuery in Action, 3rd edition