2

Background

I'm looking for hints / pointers on how to approach this. When I search for table or grid, I get the UI stuff which is not what I'm looking for.

I don't know if this sounds like homework, but it is not. This is for a map of crops and I need to determine the crops planted around a particular plot of land.

Problem

I have a table of information, its 6 rows by 6 columns, but it can be any size.

I need to find a box in that table, and then get the information from all of the boxes around it.

|-1-|-2-|-3-|-4-|-5-|-6-|
|-7-|-8-|-9-|-A-|-B-|-C-|
|-D-|-E-|-F-|-G-|-H-|-I-|
|-J-|-K-|-L-|-M-|-N-|-O-|
|-P-|-Q-|-R-|-S-|-T-|-U-|
|-V-|-W-|-X-|-Y-|-Z-|-0-|

So I need to be able to pick box M and get the information for F,G,H,N,T,S,R,L

Note, if box J was needed, it is okay to return blanks or nulls for the boxes on the left that doen't exist. It does not wrap around and get the boxes from the other side of the table.

My Idea

I guess I could start with an array of arrays.

private static string[][] tableData;

So when looking for box M, I would need to get the row (i) and column (j) index for M, then the surrounding cells are as "simple" as:

| i-1, j-1 | i-1, j | i-1, j+1 |
| i, j-1   | i, j   | i, j+1   |
| i+1, j-1 | i+1, j | i+1, j+1 |

And when asked for box M, I could find box M's index, then get the prior "row" and then index -1, index, and index + 1. Do that for M's row -1, M's row and then M's row + 1.

Is that a fair and good way of doing this or are there better, perhaps already built classes that I should use?

So the question: is an array of arrays the best way to handle this problem, or is there a better way?

quip
  • 3,666
  • 3
  • 32
  • 45

3 Answers3

4

There is nothing built in that will simply let you get "surrounding" cells in the way you describe.

A two dimensional char (or string) array looks like a good match, as you have already posted.

You can write wrap this in a class that can return a 3*3 array containing the neighbors.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • +1 :i agree with Oded and i deleted my answer because i misintrepeted question. It would be better to use 2 D array as OP is already doing – TalentTuner Nov 23 '10 at 16:50
4

The data structure that you choose is going to depend highly on the exact problem characteristics. For example, if you only have data in a small fraction of the cells and you have a lot of cells (tens of thousands or millions), you would probably want to implement a sparse array. Otherwise you can use a multi-dimensional array or jagged array.

Jagged array (the one that you mentioned):

private static string[][] tableData;

Multi-dimensional array:

private static string[,] tableData;

If you're not dealing with square plots, you might consider a completely different data structure altogether.

As well although multi-dimensional arrays may be easier to work with, jagged arrays have better performance on the CLR.

Why are multi-dimensional arrays in .NET slower than normal arrays?

Your array manipulation algorithms might drive you to choose one data structure over another too. All this said, I would recommend encapsulating your data structure inside a class so that you can more easily switch representations if needed.

(And I wouldn't use a DataTable. That's a whole lot of overhead for features that you're not using.)

Community
  • 1
  • 1
James Kovacs
  • 11,549
  • 40
  • 44
  • Luckily for me, the tables will be fairly small, at most 30x30. – quip Nov 23 '10 at 16:54
  • Absolutely going to encapsulate this into a class. – quip Nov 23 '10 at 16:55
  • In that case, I would use string[,]. And definitely a good idea to encapsulate into a class. – James Kovacs Nov 23 '10 at 16:56
  • Nice catch about the performance. I certainly thought it would be the other way around but I guess the CLR has its suprises – Mikael Eliasson Nov 23 '10 at 16:59
  • 1
    The perf diff might not be significant (always measure!), but something to remember. It's a good reason to encapsulate the data structure inside a class so that you can easily swap impls if necessary. – James Kovacs Nov 23 '10 at 17:05
1

C# has:

private static string[,] tableData; 

Otherwise I think you approach is the correct one. Make sure you remember to consider the edges of the table though.

Mikael Eliasson
  • 5,157
  • 23
  • 27
  • I forgot about [,] arrays though, and I think it might be easier to work with since I don't need different sizes. The table should always be "rectangular" – quip Nov 23 '10 at 16:49