• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie: basic array questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie: basic array questions


  • Subject: Re: Newbie: basic array questions
  • From: zauhar <email@hidden>
  • Date: Mon, 28 Apr 2003 15:12:39 -0400

On Monday, April 28, 2003, at 02:47 PM, Philip D Riggs wrote:


1. I have a set of points, each with an ID, x-coord, y-coord, and a time
point. How can I set up a mutable array of this type and access each item
and any of their 4 attributes?

You create a class to define objects of this type:

myObject.h

@interface myObject : NSObject
{
double x, y, t ;
int ID ;
}

// Accessor methods

- (double) X ;
- (double) Y:
etc.
- (void) setX: (double)x Y:(double)y T:(double)t ID:(int)index ;
etc.



2. I have a 2-dimensional grid of cells, each with a relative location on a
grid and an idividual value. In C this was implemented as a two-dimensional
array (cell [x] [y]) with the value being stored at that x-y location. How
can I set up a 2-D array in Obj-C?

You would need an NSArray (or NSMutableArray), and each element of the array would be another NSArray. To make a 100 X 100 array of myObject instances:

NSMutableArray *myArray = [ [ NSMutableArray alloc ] initWithCapacity:100 ] ;

for( i = 0 ; i < 100 ; ++i )
{
[ myArray addObject: [ [ NSMutableArray alloc ] initWithCapacity:100 ] ] ;

for( j = 0 ; j < 100 ; ++j )
{
[ [ myArray objectAtIndex:i ] addObject:[ [ myObject alloc ] init ] ] ;
}
}

To initialize the values of element (i,j):

[ [ [ myArray objectAtIndex:i ] objectAtIndex:j ] setX:0.35 Y:-0.5 T:10.5 ID:10 ] ;

Or something like that...


Randy

P.S. None of the above has been checked, debugged, etc.


In each case, how would I implement this in Obj-C? What is the most
efficient way of accessing them if there are anywhere from 1 to 15,000 items
in each?

Thank you for any responses!
----------------------------------------------------------------------- -----
----
Philip D. Riggs
GIS Specialist / Laboratory Manager
Environmental Health Advanced Systems Laboratory
Colorado State University
Fort Collins, Colorado
http://ehasl.cvmbs.colostate.edu/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.



Randy J. Zauhar, PhD
Assoc. Prof. of Biochemistry
Director, Graduate Program in Bioinformatics
Dept. of Chemistry & Biochemistry
University of the Sciences in Philadelphia
600 S. 43rd Street
Philadelphia, PA 19104

Phone: (215)596-8691
FAX: (215)596-8543
E-mail: email@hidden
Web: http://tonga.usip.edu/zauhar


Discussion after watching Disney's "Lilo & Stitch":

DAD: "But why did the space aliens speak English, as opposed to French, or Swahili? And why did the one alien speak English with an Eastern European accent? I don't get it."

CATHERINE (age 7): "That's 'cause you don't have a good cartoon brain."
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Newbie: basic array questions (From: "Philip D Riggs" <email@hidden>)

  • Prev by Date: Newbie: basic array questions
  • Next by Date: RE: Newbie: basic array questions
  • Previous by thread: Newbie: basic array questions
  • Next by thread: RE: Newbie: basic array questions
  • Index(es):
    • Date
    • Thread