aspose file tools*
The moose likes Beginning Java and the fly likes Best option for two dimensional data - rectangular data - rows columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best option for two dimensional data - rectangular data - rows columns" Watch "Best option for two dimensional data - rectangular data - rows columns" New topic
Author

Best option for two dimensional data - rectangular data - rows columns

Bob Smith
Greenhorn

Joined: Sep 10, 2008
Posts: 12
What are my options for storing two dimensional data in Java?

I know I could use a two dimensional array, but arrays in java are rather limited since I have to initialize the size. What else can I use to store two dimensional data? What is the best practice?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
what are your data, show us sample. If your data is like, you want to get nth row and mth column directly, probably the only option is array. you have sth like all values can be grouped together based on one unique value, you can choose map.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

A List of Lists is also an option:
However, you need to create all intermediate Lists, something you can do with a shortcut by specifying all sizes in the array initialization.

If you're data structure will be sparse (i.e. contain a lot of gaps) a Map<Point,X> may work as well, as long as you encapsulate it well:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Rahul.p Kumar wrote: . . . sth . . .
For reasons explained here, please avoid that sort of abbreviation. People who didn't learn English as a first language won't understand that, least of all after you said "nth" and "mth".

Java doesn't support 2-Dimensional arrays. It supports arrays of arrays, which can be more versatile. For example your array of arrays can include differently sized arrays, or even an empty array, as its elements.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Campbell Ritchie wrote:For reasons explained here, please avoid that sort of abbreviation. People who didn't learn English as a first language won't understand that, least of all after you said "nth" and "mth".


if you want to write mth row, how you will write in this editor, is there any facility to write superscripts?
Bob Smith
Greenhorn

Joined: Sep 10, 2008
Posts: 12
Thanks for the input everyone.

Could I also use a map as follows?

Map<Integer, String[]> mydata = new HashMap<Integer, String[]>();

Integer would represent the row number, and the array of strings would hold the columns?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best option for two dimensional data - rectangular data - rows columns