• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 am displeased. You are no longer allowed to read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic