1: Basic concepts of Arrays In Foundation, an array (NSArray, NSMutableArray) is a set of ordered objects, which are obtained through index subscript. Each element. Similar to a string, an array is also a variable or an unchangeable array. In addition, an array cannot store basic array types, but can only store classes. Instance (object). If you need to put the basic data type and struct into an array, You need to encapsulate the data through NSNumber and NSValue" 2: immutable array-NSArray I. array Initialization // Immutable array Initialization NSArray * firstArray = [NSArray arrayWithObject: @ "abc"]; // when initializing multiple elements, note that nil is used as the end of the array NSArray * secondArray = [NSArray arrayWithObjects: @ "one", "two", nil]; NSArray * thirdArray = [NSArray arrayWithArray: secondArray]; NSArray * fourArray = [NSArray arrayWithContentsOfFile: @ "/apple/user/array.txt"]; 2. Obtain the number of elements in the array and access // Obtain the number of elements in the array and access int count = [secondArray count]; NSLog (@ "secondArray all element % ld", count); NSString * string1 = [secondArray objectAtIndex: 1]; NSLog (@ "secondArray of element 1 is % @", string1 ); 3. Based on the original array object, the append object returns a new array. // NSArray * array5 = [secondArray arrayByAddingObject: @ "end"]; NSLog (@ "array5 is % @", array5 ); 4. Use Specified characters to display data in string format // Array --> string NSString * string2 = [array5 componentsJoinedByString: @ ","]; NSLog (@ "% @", string2 ); 5. query whether the array contains the specified object// Determine whether the specified object exists in the array Boolean result = [array5 containsObject: @ "two"]; if (result) {NSLog (@ "specified two ");} else {NSLog (@ "nonexistent ");} 6. Return the index subscript Based on the specified object and return the last element in the array. // Returns the index subscript Based on the specified object and returns the last element of the array. NSInteger *index=[array5 indexOfObject:@"two"]; NSLog(@"index:%ld",index); NSString *string3 =[array5 lastObject]; NSLog(@"%@",string3); 3: common methods for variable arrays (NSMutableArray)I. initialize and set the number of elements to 5, but you can change it to append a value object. // Initialization, the number of specified arrays is 5, but NSMutableArray * mutableArrray1 = [NSMutableArray array]; // empty array NSMutableArray * mutableArrray2 = [NSMutableArray limit: 5]; NSMutableArray * mutableArrray3 = [NSMutableArray Syntax: @ "one", @ "two", nil]; NSLog (@ "% @", mutableArrray3); [mutableArrray3 addObject: @ "three"]; NSLog (@ "% @", mutableArrray3 ); 2. Insert an element to the specified subscript in the array // Insert the element [mutableArrray3 insertObject: @ "ccc" atIndex: 2] to the specified subscript in the array; NSLog (@ "% @", mutableArrray3 ); 3. Remove the specified element, the last element, and the element of the specified object. // Remove the last element [mutableArrray3 removeLastObject]; NSLog (@ "% @", mutableArrray3); // remove the specified Element [mutableArrray3 removeObject: @ "one"]; // remove the element [mutableArrray3 removeObjectAtIndex: 0]; NSLog (@ "% @", mutableArrray3 ); 4. Add an array to the array// Add arrays to several types NSMutableArray *mutableArray4=[NSMutableArray arrayWithObjects:@"four",@"five", nil]; [mutableArrray3 addObjectsFromArray:mutableArray4]; NSLog(@"%@",mutableArrray3); V. Replacing objects in the array// Replace objects in the array [mutableArrray3 replaceObjectAtIndex:1 withObject:@"4"]; NSLog(@"%@",mutableArrray3); 3: array Traversal I. Regular Traversal NSArray *array6=[NSMutableArray arrayWithObjects:@"x",@"y",@"z", nil]; NSInteger size = [array6 count]; for (NSInteger I = 0; I
Ii. Efficient Traversal
// Efficient Traversal for (NSString *str in array6) { NSLog(@"%@",str); } |