0

What is the best way to represent this data structure? I'm using Objective-C for iPhone development but just generally is fine.

             Location1          Location2         Location3
Location1        0                  8                 23
Location2        8                  0                 16
Location3        23                 16                0

I could do nested arrays...

table = ['Location1' => ['Location 1' => '0',
                         'Location 2' => '8',
                         'Location 3' => '23'],
         'Location2' => ['Location 1' => '8',
                         'Location 2' => '0',
                         'Location 3' => '16'],
         'Location3' => ['Location 1' => '23',
                         'Location 2' => '16',
                         'Location 3' => '0']]

... but I'm not sure this is the most efficient way as there's a lot of data replication. Maybe some sort of object-oriented method?

Additionally, are there any special iPhone Objective-C data structures that would lend themselves to this kind of data?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user116170
  • 351
  • 1
  • 3
  • 11
  • this covers two dimensional array's :http://stackoverflow.com/questions/638129/how-to-declare-a-two-dimensional-array-of-string-type-in-objective-c – madmik3 Feb 27 '11 at 21:22

3 Answers3

1

You could use an array of arrays, that is NSArray objects that contain other NSArray objects that contain the data. The downside to this approach is that it is up to your program to enforce limits on the size of the array so that each row and column has a consistent size. If your data is static and plist-able (contains only strings, numbers, dates, data, arrays or dictionaries), consider storing it in a plist file, and loading it in one go using [NSArray arrayWithContentsOfFile:].

If you wish to access the data using keys such as Location1, then you'll need to use NSDictionary objects instead of NSArray objects; however doing this you lose the ability to access the data sequentially or by numerical index (it must always be accessed by key).

Also, if your data is mutable (that is, you want to be able to alter the data), you'll need to use NSMutableArray or NSMutableDictionary.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Data is immutable (probably should have mentioned that), so plists seem like a good way. I'll do some research into them (I've not been developing iPhone apps for long and have never used one before). Thanks! – user116170 Feb 27 '11 at 21:30
  • Quick question: Is there a lot of data or just a bit? If it's a lot, then plists might not be the best bet because it is loaded all in one swell foop. Fast, yes, but not for a lot of data. – BillEccles Feb 27 '11 at 22:18
1

I would recommend to make an NSobject class of location, with members location1, location2 and location3. and then you can use location object to get the values like:
[locationobj location1];
later On you can make nsarray or mutablearray of these objects too

Tobias
  • 579
  • 1
  • 5
  • 15
1

You could create a Mutable2DArray class which used an NSMutableArray internally, using a conversion between 2D indexes and a 1D index or tag:

index1D = numColumns * row2DIndex + column2DIndex;

You could even use plain C arrays internally, depending on the tabular data type and other requirements (reuse, extensibility, efficiency, memory usage, etc.)

hotpaw2
  • 70,107
  • 14
  • 90
  • 153