2

I have a matrix-like data structure that I want to preserve in its original form as much as maybe allowed in a language such as Java. It is as follows:

    a()  b() ... n()
a()  .1   .2 ...  .9
b()  .4   .3 ...  .6
..
.
n()  .1   .7 ...  .4

One could imagine a(), b(), ... n() as instances of an object. The only reason why I have the parenthesis here is because I'm using a domain-specific language to solve the problem. But, I could very well raise this question for mainstream languages such as Java. How would I design a data structure, (or use an offered type) and subsequently an easy function call, where I could pass in two object parameters (i.e. (a,b) ) and get back the numeric value. The constraint is to not to resort to define a complicated class defining a series of sub hashmap fields and so on and so forth to store the data one row or column a time. Does Java provide a built in type to offer this as a direct solution?

apil.tamang
  • 2,545
  • 7
  • 29
  • 40
  • Are the types homogeneous across rows and columns; that is to say, are all of the rows' types the same, and are all of the columns' types the same? – Makoto Mar 03 '15 at 17:45
  • yes, the values (as in elements of the matrix ) would be homogeneous: i.e. floats. It can be considered that the values in the topmost row and the left most column are also subclassed from a same parent. However, to note: this row and column does not actually exist in the matrix itself. They are simply there to index the actual data itself. – apil.tamang Mar 03 '15 at 17:49
  • I wouldn't have expected the elements to be non-homogeneous. What I was referring to was more the objects/types you'd use to index into a specific row-column pair. Is it fair to presume that the objects are the same across the rows, and are the same across the columns? This doesn't mean that the row and column types have to be the same. – Makoto Mar 03 '15 at 17:50
  • well, it wouldn't make much sense if all the objects used to index into a specific value were the same (i.e. equal to each other). I'm assuming that's not what you mean either. Now, the assumption that all the objects were instantiated from the same class maybe made. That's a reasonable assumption. – apil.tamang Mar 03 '15 at 17:55

3 Answers3

2

You can use a Map in Java with the key as a object combining the two objects (like a pair).

Java does not provide a implementation of tuples/pair. But you can use Apache Commons or implement your own. Take a look to this question also.

Luís Bianchin
  • 2,327
  • 1
  • 28
  • 36
2

A Map can only hold a key-value pair. A Table from Google Guava can hold row, column and value information.

You would instantiate one like so:

Table<Double, String, Double> table = HashBasedTable.create();

...and you can use whatever row, column, value triplet you like. The above is just a for-instance.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

You can use an adjacency matrix graph or even a two dimensional array, I believe both are valid solutions to your problem.

theCJCsoccer
  • 601
  • 8
  • 29