objective-c series-NSMutableArray
********************************************
// Variable array constructor
// The definitions of the following two sentences are immutable
// NSMutableArray * marray1 = [[NSArray alloc] init];
// NSMutableArray * marray1 = @ [@ "xx", @ "yy"];
// The default array has no elements
NSMutableArray * marray1 = [[NSMutableArray alloc] init];
NSMutableArray * marray = [NSMutableArray array];
NSMutableArray * marray2 = [[NSMutableArray alloc] initWithArray: @ [@ "1", @ "2"]];
NSMutableArray * marray3 = [[NSMutableArray alloc] initWithObjects: @ "one", @ "two", @ "three", nil];
id objs [5] = {@ "123", @ "456", @ "789", @ "abc", @ "def"};
// The type of objs is: id [5];
// Because objs is the name of the array, that is, the address of the first element, and because the type of the first element is id;
// So: the type of objs is: id *
NSMutableArray * marray4 = [[NSMutableArray alloc] initWithObjects: objs count: 3];
NSLog (@ "marray4:% @", marray4);
// open 80 bytes of dynamic memory for memory,
// That memory points to 80 bytes of legal memory
id * memory = (__bridge id *) malloc (80);
memory [0] = @ "mem1";
memory [1] = @ "mem2";
memory [2] = @ "mem3";
memory [3] = @ "mem4";
memory [4] = @ "mem5";
NSMutableArray * marray5 = [[NSMutableArray alloc] initWithObjects: memory count: 3];
NSLog (@ "marray5:% @", marray5);
// Construct a variable array, initially give it 10 elements of space, but no elements
NSMutableArray * marray6 = [[NSMutableArray alloc] initWithCapacity: 10];
// Note: marray6 is still an empty array, that is, no elements exist
NSLog (@ "marray6:% @", marray6);
********************************************
// Add elements to the array
// add insert
NSMutableArray * array = [[NSMutableArray alloc] init];
// Append an element to the tail
[array addObject: @ "theFirstObject"];
NSLog (@ "First call addObject: array:% @", array);
[array addObject: @ "theSecondObject"];
NSLog (@ "second call addObject: array:% @", array);
// Splice an array at the end
[array addObjectsFromArray: @ [@ "add1", @ "add2"]];
NSLog (@ "First call addObjectsFromArray: array:% @", array);
// Result description addObject is to add elements to the end of the array
[array insertObject: @ "theInsertObject" atIndex: 1];
NSLog (@ "First call insertObject: array:% @", array);
********************************************
// Delete elements from the array
NSMutableArray * array = [[NSMutableArray alloc] initWithArray: @ [@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "1", @ " 1" ]];
NSLog (@ "array:% @", array);
// remove
// Delete all occurrences of this object in the array
[array removeObject: @ "1"];
NSLog (@ "First call removeObject: array:% @", array);
// delete the last element
[array removeLastObject];
// Delete the element under the scale
[array removeObjectAtIndex: 0];
// delete all elements
[array removeAllObjects];
********************************************
// Reset the array and replace the element of a certain index
NSMutableArray * array = [[NSMutableArray alloc] initWithArray: @ [@ "abc", @ "def", @ "ghi"]];
NSLog (@ "raw array: array:% @", array);
NSArray * newArray = @ [@ "123", @ "456", @ "789"];
[array replaceObjectsInRange: NSMakeRange (0, [array count]) withObjectsFromArray: newArray];
NSLog (@ "Array with all elements replaced: array:% @", array);
[array replaceObjectAtIndex: 1 withObject: @ "[theFisrtReplaceObject]"];
NSLog (@ "replace a subscript element: array:% @", array);
NSLog (@ "The newly replaced element is:% @", [array objectAtIndex: 1]);
********************************************
// Other common methods of array objects
NSArray * array = @ [@ "one", @ "two", @ "three"];
// Get the first element of the array
id obj1 = [array firstObject];
NSLog (@ "obj1:% @", obj1);
// Get the tail element of the array
id obj2 = [array lastObject];
NSLog (@ "obj2:% @", obj2);
********************************************
********************************************