main.m//oc04-task-08////Created by Keyzhang on 15-1-24.//Copyright (c) 2015 Keyzhang. All rights reserved.//#import <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char * argv[]) { @autoreleasepool {//Insert code here .../////////////nsmutablearray--variable array/////////// Constructs a mutable array using the constructor method of the parent class nsmutablearray *arr = [Nsmutablearray array]; Nsmutablearray its own construction method, indicating the capacity size of the initialized array, note: This is just for the readability of the code nsmutablearray *ARR0 = [Nsmutablearray arraywithcapacity:2]; add Element (object)/* Inserts a given object at the end of the array. */[Arr0 addobject:@ "1"]; [Arr0 addobject:@ "2"]; [Arr0 addobject:@ "3"]; NSLog (@ "Arr0 is%@", arr0); Constructs Nsmutablearray *arr1 = [Nsmutablearray arraywithobjects:@ "1", @ "3", nil] with the method of the parent class; NSLog (@ "arr1 is%@", arr1); Adds a new element to the end of the array [arr addobject:@ "X"]; NSLog (@ "arr is%@", arr); Insert element [arr1 insertobject:@ "2" atindex:1]; [arr1 insertobject:@ "0" atindex:0]; NSLog (@ "arr1 is%@", arr1); Delete element//delete last element [arr1 Removelastobject]; NSLog (@ "arr1 is%@", arr1); Delete the specified subscript element [arr1 removeobjectatindex:1]; NSLog (@ "arr1 is%@", arr1); Delete the specified element//If the element does not exist, do nothing [arr1 removeobject:@ "2"]; NSLog (@ "arr1 is%@", arr1); Remove all elements [arr1 removeallobjects]; NSLog (@ "arr1 is%@", arr1); Add multiple elements Nsmutablearray *arr2 = [Nsmutablearray arraywithobjects:@ "1", @ "2", nil]; Nsarray *lists = @[@ "3", @ "4", @ "5", @ "6"]; [Arr2 addobjectsfromarray:lists]; NSLog (@ "ARR2 is%@", arr2); Replace and swap elements//replace the target object of the subscript with the passed-in object [arr2 replaceobjectatindex:2 withobject:@ "7"]; NSLog (@ "ARR2 is%@", arr2); Exchange elements according to the subscript of the two elements [arr2 exchangeobjectatindex:2 withobjectatindex:5];NSLog (@ "ARR2 is%@", arr2); Extension: Person *ps = [[Person alloc] init]; Ps.name = @ "Tom"; Ps.age = 10; Array traversal Nsarray *ARR3 = @[@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", PS]; Traditional traversal method for (int i = 0; i < [ARR3 count]; i++) {NSLog (@ "arr3[%d] is%@", I, arr3[i]); }//Fast traversal method for (ID value in ARR3) {//loop block if ([value Ismemberofclass:[person cl "]) {person *ps = (person *) value; NSLog (@ "person name is%@", ps.name); Continue } NSLog (@ "value is%@", value); }//mutablecopy Nsarray *arr4 = @[@ "1", @ "2", @ "3"]; NSLog (@ "ARR4 is%@", ARR4); Nsmutablearray *ARR5 = [Arr4 mutablecopy]; [Arr5 addobject:@ "4"]; NSLog (@ "ARR5 is%@", ARR5); Nsmutablearray *ARR6 = [Nsmutablearray ARRAYWITHARRAY:ARR4]; } return 0;}
nsmutablearray--variable Array