3.ios-nsarray

Source: Internet
Author: User

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

@autoreleasepool {

#pragma make immutable variable group

Creating an array of #pragma make

String reference

Nsstring*[email protected] "Zhangsan";

Nsstring*[email protected] "Lisi";

Nsstring*[email protected] "WANGWU";

The last nil equals the end of the mark.

Nsarray*array1=[[nsarray alloc]initwithobjects:string1, string2, string3, nil];

NSLog (@ "%@", array1);

Creating an array with a class method

Nsarray*array2=[nsarray arraywithobjects:@ (2), @ (3), nil];

NSLog (@ "%@", array2);

Create an array that takes the data from the original array and puts it in

Nsarray*array3=[nsarray Arraywitharray:array1];

NSLog (@ "%@", array3);

Literal syntax, created by a certain immutable type

Array literal syntax

Nsarray*[email protected][@ "haha", array1, Array2];

NSLog (@ "%@", Array4);

#pragma make to get the element values in an array by array subscripts

Objectatindex by index Atindex: Get array subscript element

Nsstring*obj1=[array1 objectatindex:0];

NSLog (@ "%@", obj1);

Firstobject getting the first element

Nsstring*obj2=[array1 Firstobject];

Lastobject gets the last element value

Nsstring*obj3=[array1 Lastobject];

Count gets the array length

Nsuinteger count=[array1 count];//returns an unsigned value

NSLog (@ "%d", count);

Indexofobject to find the subscript value of a component group element

Returns an object that has the subscript value in the array

Nsuinteger index=[array1 indexofobject:@ "Zhangsan"];

if (index==nsnotfound) {

NSLog (@ "not found");

}else{

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

}

Containsobject determines whether an array contains an object, the value of a pointer object, not the value in the object

Results 1he0;

BOOL Iscontains=[array1 containsobject:@ "Zhangsan"];

NSLog (@ "%d", iscontains);

Array 22 Insert String componentsjoinedbystring

If the string is stored in the array, you can use the connector to connect all of its elements

Note that the elements in the array must all be strings

Nsstring*string4=[array1 componentsjoinedbystring:@ ","];

NSLog (@ "%@", String4);

Adding an array Arraybyaddingobjectsfromarray arraybyaddingobject

Appends an element to the original array, returning a new array object because he is immutable

Nsarray*array5=[array1 Arraybyaddingobjectsfromarray:array2];

NSLog (@ "%@", array5);

Nsarray*array6=[array1 arraybyaddingobject:@ "Maliu"];

NSLog (@ "%@", Array6);

#pragma make traversal array

For Loop 1

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

Nsstring*str=[array1 OBJECTATINDEX:I];

NSLog (@ "[%d]=%@", I,STR);

}

For Loop Two

for (int i1=0; i1<array1.count; i1++) {

NSSTRING*STR1=ARRAY1[I1];

NSLog (@ "[%d]=%@", I1,STR1);

}

Iterating through an array using the enhanced for loop method

For (NSString * str2 in Array1)

{

NSLog (@ "%@", str2);

}

Reverseobjectenumerator the value of an array in reverse

for (NSSTRING*STR3 in [array1 Reverseobjectenumerator]) {

NSLog (@ "%@", STR3);

}

Enumerator

Nsenumerator * Enumerator = [array1 objectenumerator];

NSString * value;

while (value = [Enumerator Nextobject]) {

NSLog (@ "%@", value);

}

#pragma make mutable array

Creating a mutable array

NSString *str1 = @ "Zhangsan";

NSString *STR2 = @ "Lisi";

NSString *STR3 = @ "Wangwu";

Nsmutablearray *marray1 = [[Nsmutablearray alloc] initwithobjects:str1,str2,str3, nil];

The following is the wrong way to create an array

Here's how to create an immutable group, not a mutable array.

Nsmutablearray *array1 = @[str1,str2,str3];

Opens 5 spaces for storing elements, and when the stored element exceeds 5, the array automatically increases the space

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

Use the class method to create

Nsmutablearray *marray3 = [Nsmutablearray arraywithcapacity:5];

Methods for adding elements AddObject

[MArray1 ADDOBJECT:STR1];

[MArray1 ADDOBJECT:STR2];

Add an array and add all the elements in the mArray1 to the MArray2 Addobjectsfromarray

[MArray2 Addobjectsfromarray:marray1];

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

Two-dimensional arrays

This is to add the mArray1 to the MArray2 array so that the mArray2 becomes a two-dimensional array.

[MArray2 Addobject:marray1];

NSLog (@ "Two-dimensional array%@", mArray2);

InsertObject

Inserts a specific element at the specified location

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

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

Replaceobjectadidex

Replace element

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

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

Exchangeobjectatindex

Swap the position of two elements

[MArray2 exchangeobjectatindex:0 Withobjectatindex:3];

NSLog (@ "Interchange%@", MArray2);

Removeobjectadindex

Deletes the element at the specified position

[MArray2 removeobjectatindex:0];

NSLog (@ "MArray2 Delete the element at the specified position =%@", mArray2);

Delete last Element

[MArray2 Removelastobject];

NSLog (@ "MArray2 Delete last element =%@", mArray2);

Deletes the specified object

[MArray2 removeobject:@ "Lisi"];

NSLog (@ "MArray2 Delete the specified object =%@", mArray2);

Delete several objects

[MArray2 Removeobjectsinrange:nsmakerange (2, 1)];

NSLog (@ "%@", mArray2);

Delete all objects/empty list

[MArray2 removeallobjects];

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

}

return 0;

}

3.ios-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.