Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 587
tnaser Avatar asked Jan 23 '12 17:01

tnaser


People also ask

How do you store a matrix?

In the computer memory, all elements are stored linearly using contiguous addresses. Therefore, in order to store a two-dimensional matrix a, two dimensional address space must be mapped to one-dimensional address space. In the computer's memory matrices are stored in either row-major or column-major order form.

How do you create a matrix in Java?

int[][] a = new int[3][1]; This will instantiate a 3x1 "matrix", meaning that valid indices for the first set of brackets are 0, 1 and 2; while the only valid index for the second set of brackets is 0. This looks like what your code requires.

How do you represent a matrix in Java?

Fig 1: A simple 4x4 matrix In order to represent this matrix in Java, we can use a 2 Dimensional Array. A 2D Array takes 2 dimensions, one for the row and one for the column. For example, if you specify an integer array int arr[4][4] then it means the matrix will have 4 rows and 4 columns.

Does Java have a matrix class?

The Java Matrix Class provides the fundamental operations of numerical linear algebra. Various constructors create Matrices from two dimensional arrays of double precision floating point numbers. Various "gets" and "sets" provide access to submatrices and matrix elements.


2 Answers

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.

like image 104
SJuan76 Avatar answered Sep 28 '22 19:09

SJuan76


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

like image 35
gnomed Avatar answered Sep 28 '22 18:09

gnomed