1. nsarray is a static array. The content and length of the created array cannot be modified.
Instance:
// Use arraywithobjects to initialize an unchangeable array object. // Separate initialized values with commas (,) and end with nil. Nsarray6 * city = [nsarray arraywithobjects: @ "Shanghai", @ "Guangzhou", @ "Chongqing", nil]; for (INT I = 0; I <[city count]; I ++) {nslog (@ "% @", [city objectatindex: I]);}
Running result:
Shanghai
Guangzhou
Chongqing
Common nsarray methods:
+ (ID) arraywithobjects: obj1, obj2,... nil // create a new array, which ends with nil. -(Bool) containsobject: OBJ // determine whether the array contains the object obj-(nsuinterger) Count // The number of elements in the array-(nsuinterger) indexofobject: OBJ // The first index that contains the OBJ element-(ID) objectatindex: I // The object stored in position I-(void) makeobjectsperformselector :( SEL) selector // send the message prompted by selector to every element in the array-(nsarray *) sortedarrayusingselector :( SEL) selector // sort the array according to the comparison method specified by selector-(bool) writetofile: path atomically :( bool) Flag // write the array to the specified file. If the flag is yes, you need to create a temporary file first.
2. nsmutablearray is a dynamic array that can dynamically add elements in the array. Similarly, nsmutablearray is a subclass of nsarray.
Instance:
// Use arraywithcapacity to create a dynamic array of 5 nsmutablearray * NSMA = [msmutablearray arraywithcapacity: 5]; for (INT I = 0; I <= 50; I ++) {if (I % 3 = 0) {[NSMA addobject: [nsnumber numberwithinteger: I]; // use addobject to add an object to the array NSMA} // output the element for (INT I = 0; I <[NSMA count]; I ++) in the array) {nslog (@ "% Li", (long) [[NSMA objectatindex] integervalue]);}
Array sorting example:
Student. h
#import <Foundation/Foundation.h>@interface Student: NSObject { NSString: *name; int age;}@property (copy,nonatomic) NSString* name;@property int age;-(void)print;-(NSComparisonResult)compareName:(id)element;@end
Studenttest. m
# Import <Foundation/Foundation. h> # import "student. H "int main (INT argc, const char * agrv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; Student * stu1 = [STUDENT alloc] init]; student * stu2 = [[STUDENT alloc] init]; Student * stu3 = [[STUDENT alloc] init]; [stu1 setname: @ "Sam"]; [stu1 setage: 30]; [stu2 setname: @ "Lee"]; [stu2 setage: 23]; [stu3 setname: @ "Alex"]; [stu3 setage: 26]; optional * students = [[nsmutablearray alloc] init]; [Students addobject: stu1]; [Students addobject: stu2]; [Students addobject: stu3]; nslog (@ "Before sorting: "); For (INT I = 0; I <[Students count]; I ++) {student * stu4 = [Students objectatindex: I]; nslog (@" Name: % @, age: % I ", [stu4 name], [stu4 age]);} [Students sortusingselector: @ selector (comparename :)]; nslog (@" after sorting: "); For (student * stu4 in students) {nslog (@" Name: % @, age: % I ", stu4.name, stu4.age);} [students release]; [stu1 release]; [sut2 release]; [stu3 release]; [pool drain]; return 0 ;}
Result:
Before sorting: Name: Sam, age: 30 name: Lee, age: 23 name: Alex, age: 26 after sorting: Name: Alex, age: 26 name: Lee, age: 23 name: Sam, age: 30
Common nsmutablearray methods:
+ (ID) array // create an empty array + (ID) arraywithcapacity: Size // create an array with the size-(ID) initwithcapacity: size // initialize a new quantity, and specify the capacity as size-(void) addobject: OBJ // Add OBJ to the array-(void) insertobject: OBJ atindex: I // insert OBJ into the data of the I element-(void) replaceobjectatindex: I withobject: OBJ // Replace the object of the I index with obj-(void) removeobject: OBJ // delete all objects whose names are OBJ from the array-(void) removeobjectatindex: I // Delete the object whose index is I from the array-(void) sortusingselector :( SEL) selector // sort by the comparison method indicated by Selector