简体   繁体   中英

Best data structure to store and manipulate my data?

I am writing a simple Java program that will input a text file which will have some numbers representing a (nxn) matrix where numbers are separated by spaces. for ex:

1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7

I then want to store these numbers in a data structure that I will then use to manipulate the data (which will include, comparing adjecent numbers and also deleting certain numbers based on specific rules. If a number is deleted, all the other numbers above it fall down the amount of spaces. For the example above, if say i delete 8 and 9, then the result would be:

() 2 3 ()
1  6 7 4
5  1 2 3
4  5 6 7

so the numbers fall down in their columns. And lastly, the matrix given will always be square (so always nxn, where n will be always given and will always be positive), therefore, the data structure has to be flexible to virtually accept any n-value.

I was originally implementing it in a 2-d array, but I was wandering if someone had an idea of a better data structure that I could use in order to improve efficiency (something that will allow me to more quickly access all the adjacent numbers in the matrix (rows and columns). Ultimately, mu program will automatically check adjacent numbers against the rules, I delete numbers, re-format the matrix, and keep going, and in the end i want to be able to create an AI that will remove as many numbers from the matrix as possible in the least amount of moves as possible, for any nxn matrix.

In my opinion, you yo know the length of your array when you start, you are better off using an array. A simple dataType will be easier to navigate (direct access). Then again, using LinkedLists, you will be able to remove a middle value without having to re-arrange the data inside you matrix. This will leave you "top" value as null. in your example :

null 2 3 null
1    6 7 4
5    1 2 3
4    5 6 7

Hope this helps.

Array access is pretty fast. Accessing adjacent elements is easy, as you just increment the relevant index(s) (being cognizant of boundaries). You could write methods to encapsulate those operations that are well tested. Having elements 'fall down' though might get complicated, but shouldn't be too bad if you modularize it out by writing well tested methods.

All that said, if you don't need the absolute best speed, there are other options.

You also might want to consider a modified circularly linked list. When implementing a sudoku solver, I used the structure outlined here . Looking at the image, you will see that this will allow you to modify your 2d array as you want, since all you need to do is move pointers around.

I'll post a screen shot of relevant picture describing the datastructure here, although I would appreciate it if someone will warn me if I am violating some sort of copy right or other rights of the author, in which case I'll take it down...

在此处输入图片说明

You could use one dimensional array with the size n*n .

int []myMatrix = new myMatrix[n * n];

To access element with coordinates (i,j) use myMatrix[i + j * n] . To fall elements use System.arraycopy to move lines.

Use special value (eg Integer.MIN_VALUE ) as a mark for the () hole.

I expect it would be fastest and most memory efficient solution.

尝试一个LinkedList数组。

如果您希望数字自动下降,建议您使用列的列表。

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