[Objective-C] concepts and common methods of values in OC (NSArray and NSMutableArray)

Source: Internet
Author: User

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); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.