tnaser
tnaser

Reputation: 181

How to store table or matrix in Java?

I used to use matrix in octave to store data from data set, in Java how can I do that? Assume I have 10-20 columns and large data, I don't think

int [][]data; 

would be the best option. Is nested map the only solution?

Upvotes: 5

Views: 10519

Answers (7)

Alba Mendez
Alba Mendez

Reputation: 4605

This answer merges some of gnomed's answer and SJuan76's answer contents.

  1. At a quick glance, I'd suggest you to use bidimentional arrays such as int[][].
    It's not a very huge amount of data (we're speaking of ≈500 ints) so it's not a bad idea.

    Advantages: It's the simpler, ideal (from the data-structuring side) way to go,
    especially if every “slot” of the matrix contains data.

    The inconvenient: You have to know the size of the matrix before constructing it.
    Anyway, you can resize it later using the Arrays utilities.

  2. If you want more effective handling of the data, you can use a single point-map.
    That is, the key of every entry is a java.awt.Point that defines where is the value located.

    Advantages: It's more effective than having a 2D array,
    especially if part of your matrix doesn't contain data.
    And it's adaptative; you don't need to know any sizes to construct/resize it.

    The inconvenient: If every “slot” of your matrix contains data,
    you'll loose (a lot of) space and performance. A 2D-array is more effective then.

  3. Want more? If your data is really huge you can use a sparse matrix.
    See this question for more details.

Upvotes: 2

pron
pron

Reputation: 1733

  • Well, if your indices are small integers, you can certainly use nested arrays.
  • In a matrix class, you may want to use a plain array, like so: (assuming n is the number of columns)
double get(int i, int j) { return data[i*n + j]; }

Upvotes: 0

gnomed
gnomed

Reputation: 5565

Depends on what you need to do. If you know the size of the lists, then an array is definitely ideal since it means you will have instant access (read/write time) to any position in the array, this is very useful for speed.

Maps are better if you dont know the size and it needs to be able to adapt.

And finally, as I discovered in a previous question, if you have a TON of data, and a lot of it will be "0" you might want to also consider using a Sparse Martrix

Upvotes: 5

Purushottam
Purushottam

Reputation: 624

I think multi-dimentional arrays are the best choice! They should serve your purpose. If your data set is only integers, int [] [] is an ideal choice.

Upvotes: 0

nidhin
nidhin

Reputation: 6920

You can use multidimensional arrays or you can try any pairs like HashMap

Upvotes: 0

Savino Sguera
Savino Sguera

Reputation: 3572

I would not discard multidimensional arrays so far: have you tried them? Are you finding specific limitations? IMHO as long as your data fits in memory, arrays can be good.

If your data is very sparse though, you may want to look at maps indeed.

Related question btw: Making a very large Java array

Upvotes: 0

SJuan76
SJuan76

Reputation: 24885

You could create a class Coordinate that takes an X and Y values and properly implement hashCode and equals.

Then create a HashMap<Coordinate, Data> and work with it.

Upvotes: 5

Related Questions