Basic usage of Nsarray and Nsmutablearray in Objective-c

Source: Internet
Author: User




/*---------------------Nsarray---------------------------*/

//Create an array

Nsarray *array1 = [Nsarray arraywithobject:@"1"];

Nsarray *array2 = [Nsarray arraywithobjects:@"1", @"2", @"3", nil];

Nsarray *array3 = [Nsarray arraywitharray:array2];

NSLog (@"array1 =%@", array1);

NSLog (@"array2 =%@", array2);

NSLog (@"array3 =%@", array3);

//Get the number of data in the array

int count = [array2 count];

NSLog (@"array2 size is%d", count);

//access to data within an array

NSString *str1 = [Array2 objectatindex:0];

NSLog (@"array2 first content is%@", str1);

//array in which data is inserted to return a new array

Nsarray *array4 = [array3 arraybyaddingobject:@"4"];

NSLog (@"Array4 =%@", array4);

//data in array to make a character connection

NSString *STR2 = [Array4 componentsjoinedbystring:@","];

NSLog (@"str2 =%@", str2);

//Determine if an object is contained in an array

BOOL B1 = [Array4 containsobject:@"4"];

BOOL b2 = [Array4 containsobject:@"5"];

NSLog (@"B1 =%d,b2 =%d", b1,b2);

//Take an index of the object within the array

int index = [array4 indexofobject:@"4"];

NSLog (@"index =%d", index);

NSString *STR3 = [Array4 lastobject];

NSLog (@"Array4 last object is%@", STR3);

/ *-----------------------variable array nsmutablearray-----------------------------------------* /

//Initialize array specifies array length but variable

Nsmutablearray *marray1 = [Nsmutablearray arraywithcapacity:5];

//Add elements to an array

[MArray1 addobject:@"AAAA"];

[MArray1 addobject:@"CCCC"];

NSLog (@"mArray1 =%@", mArray1);

//Inserts an element into the specified position

[MArray1 insertobject:@"bbbb" atindex:1];

[MArray1 insertobject:@"dddd" atindex:[marray1 Count]];

[MArray1 insertobject:@"eeee" atindex:[marray1 Count]];

NSLog (@"mArray1 =%@", mArray1);

//removing elements

[MArray1 removeobject:@"Eeee"];

NSLog (@"mArray1 =%@", mArray1);

[MArray1 removeobjectatindex:[marray1 count]-1];

NSLog (@"mArray1 =%@", mArray1);

Nsarray *array5 = [Nsarray arraywithobjects:@"bbbb", @"CCCC", nil];

[MArray1 Removeobjectsinarray:array5];

NSLog (@"mArray1 =%@", mArray1);

//Insert an array into an array

Nsmutablearray *marray2 = [Nsmutablearray arraywithobjects:@"AAAA", @"AAAA", nil];

[MArray2 addobject:@"bbbb"];

NSLog (@"mArray2 =%@", mArray2);

[MArray2 Addobjectsfromarray:array4];

NSLog (@"mArray2 =%@", mArray2);

//Replace element

[MArray2 replaceobjectatindex:[marray2 count]-1 withobject:@"5"];

NSLog (@"mArray2 =%@", mArray2);

//Traversal Array General method: Low Performance

Nsarray *array6 = [Nsarray arraywithobjects:@"A", @"B", @"C", @"D", @"E", nil];

int len = [Array6 count];

For (int i=0; i<len; i++) {

NSString *value = [Array6 objectatindex:i];

NSLog (@"Array6%d content is%@", i,value);

}

//enumeration traversal equivalent to enhanced for loop in Java

For (NSString *string in Array6) {

NSLog (@"Array6 content is%@", string);

}

NSLog (@"-----------------------");

//When the array element type is not determined

For (ID string in array6) {

NSLog (@"Array6 content is%@", string);

}



Basic usage of Nsarray and Nsmutablearray in Objective-c




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.