IOS-Arrays (Nsarray)

Source: Internet
Author: User

1. Common ways to handle arrays

--------------------Non-variable groups

1. Creation of arrays

NSString *s1 = @ "Zhangsan";

NSString *s2 = @ "Lisi";

NSString *S3 = @ "Wangwu";

(1)

Nsarray *array1 = [[Nsarray alloc] initwithobjects:s1,s2,s3, nil];

NSLog (@ "%@", array1); Equivalent to Array1.descripton

(2) Use the class method to create

Nsarray *array2 = [Nsarray arraywithobjects:s1,s2,s3, Nil];

(3) Create an array object while depositing an element into it

Nsarray *array3 = [Nsarray arraywithobject:s1];

(4) Create an array in which the elements in this array come from array1

Nsarray *array4 = [Nsarray arraywitharray:array1];

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

2. Take the element by subscript

NSString *str1 = [Array4 objectatindex:0];

3. Number of array elements

Nsuinteger count = [Array4 count]; Equivalent to: Array4.count;

4. Determine if an element is contained in an array

BOOL iscontains = [Array4 containsobject:@ "Zhangsan"];

NSLog (@ "iscontains:%d", iscontains);

5. Find the subscript position of an object in an array

Nsuinteger index = [array4 indexofobject:@ "Wangwu"];

if (index = = nsnotfound) {

NSLog (@ "not find elemnts");

} else {

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

}

6. Link strings in an array (provided that the array is all strings)

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

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

7. Accessing the last element of an array

NSString *lastobj = [Array4 lastobject]; Array4.lastobject

NSLog (@ "lsatobj =%@", lastobj);

8. Add an element behind the array of the original path

Nsarray *array5 = [Array4 arraybyaddingobject:@ "Zhaolia"];

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

Take the corresponding subscript element

int idx=4;

if (idx <array5.count) {

NSString *s = [Array5 objectatindex:idx];

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

}

--------------the traversal of an array

1. Normal traversal

for (int i=0; i<array5.count; i++) {

NSString *str = [Array5 objectatindex:i];

NSLog (@ "%@", str);

}

Fast traversal

For (NSString *s in Array5) {

NSLog (@ "%@", s);

}

Optimization after 4.4

1. Create a vegetarian group

Nsarray *array7 = @[s1,s2,s3]; Equivalent to: Nsarray *array7 = [Nsarray arraywithobjects:s1,s2,s3,nil];

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

NSString *str = array7[0];

NSLog (@ "array[7] =%@", str);

------------------variable Array

Initialize, set the number of elements is 5, can be changed. (inherited from Nsarray)

Nsmutablearray *mutablearray = [Nsmutablearray arraywithcapacity:5];

Want to add an element to the array

[Mutablearray addobject:@ "AAA"];

Inserts an element into an array by specifying the subscript

[Mutablearray insertobject:@ "CCC" atindex:0];

NSLog (@ "%@", Mutablearray); The original position element is moved back

Remove the last element

[Mutablearray Removelastobject];

NSLog (@ "After removing the last element:%@", Mutablearray);

Removing the specified element

[Mutablearray removeobject:@ "AAA"];

Remove the specified subscript element

[Mutablearray removeobjectatindex:0];

To add an array to an array

[Mutablearray Addobjectsfromarray:array1];

1. Creating a mutable array

NSString *t1 = @ "Zhangsan";

NSString *t2 = @ "Lisi";

NSString *t3 = @ "Wangwu";

Nsmutablearray *marray1 = @[s1,s2,s3];//wrong. An immutable group was created here

Nsmutablearray *marray1 = [[Nsmutablearray alloc] initwithobjects:s1,s2,s3, nil];

When creating an array, 3 spaces are created to store elements, storage exceeds capacity, and arrays automatically increase space

Nsmutablearray *marray2 = [[Nsmutablearray alloc] initwithcapacity:3];

Nsmutablearray *marray3 = [Nsmutablearray arraywithcapacity:3];

2. Adding elements

[MArray2 ADDOBJECT:T1];

[MArray2 Addobject:t2];

[MArray2 ADDOBJECT:T3];

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

Adding elements from MArray2 to MArray3

[MArray3 Addobjectsfromarray:marray2];

Add MArray2 as a two-dimensional number

[MArray3 Addobject:marray2];

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

3. Inserting elements

[MArray2 insertobject:@ "Jack" atindex:0];

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

4. Replacing elements

[MArray2 replaceobjectatindex:0 withobject:@ "John"];

NSLog (@ "Replace:%@", mArray2);

5. Swap the position of two elements

[MArray2 Exchangeobjectatindex:3 withobjectatindex:0];

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

6. Deleting an element

6.1 Delete According to subscript

[MArray2 Removeobjectatindex:2];

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

6.2 Delete last Element

[MArray2 Removelastobject];

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

6.3 Delete the specified object

[MArray2 removeobject:@ "Zhangsan"];

6.4 Delete all elements

[MArray2 removeallobjects];

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

IOS-Arrays (Nsarray)

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.