简体   繁体   中英

Suitable data structure for Java 1000*1000 matrix.

I am practising java programming. I encountered a problem which requires 1000 x 1000 matrix which stores integers value less than 1500

I would want to navigate across all the elements I might need to fetch max element and its 4 adjacent elements.

What is the best data structure which doesn't affect performance?

1  2  3  4
5  6  7  8
9  10 11 12
12 14 15 16

for the element 11 --> 7, 10, 12, 15 are adjacent elements.

what is wrong with the 2d array data structure?

to get an adjecent of a number at i,j return [i-1][j],[i+1][j],[i][j+1],i[j-1] (you will have to deal with cases where i is zero etc)...

as performance goes, its O(1), doesn't get any better than that...

If you are talking about finding the location of the element. if the matrix is sorted you can simply do a binary search.

A 2dim Array of shorts:

-> short [][] matrix = new short[1000][1000];
|  Added variable matrix of type short[][] with initial value [[S@1794d431

Generate in a second, few MB in size. What could be better than that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM