ios 數(shù)組處理記錄

//創(chuàng)建一個(gè)數(shù)組

NSArray*myArray;

NSValue*aValue = [NSNumbernumberWithInt:5];

NSString*aString =@"nihoa";

// NSString *aString = [NSString stringWithFormat:@"nihao"];

myArray = [NSArrayarrayWithObjects:aValue, aString,nil];//用便利構(gòu)造器

NSLog(@"%@",myArray);

NSArray*arr = [[NSArrayalloc]initWithObjects:@"one",@"two",nil];//用alloc+init

NSLog(@"%@",arr);

//查詢數(shù)組中是否有某個(gè)元素返回值是BOOL類型

if([arrcontainsObject:@"one"])

NSLog(@"YES");

else

NSLog(@"NO");

//元素個(gè)數(shù)

NSLog(@"%lu",[arrcount]);

[arrrelease];

//檢索元素 并存放在C語(yǔ)言的數(shù)組中

NSArray*arr1 = [[NSArrayalloc]initWithObjects:@"one",@"two",@"three",@"four",nil];

id*objects;

NSUIntegercount = [arr1count];

objects =malloc(sizeof(id) * count);//objects數(shù)組名

[arr1getObjects:objects];

for(NSUIntegeri =0; i < count; i++) {

NSLog(@"object at index %lu: %@", i, objects[i]);

}

free(objects);

//檢索某個(gè)下標(biāo)的元素

NSLog(@"%@",[arr1objectAtIndex:0]);

//檢索某個(gè)元素的下標(biāo)

NSLog(@"%lu",[arr1indexOfObject:@"two"]);

NSLog(@"%lu",[arr1indexOfObject:@"two"inRange:NSMakeRange(0,3)]);

//在數(shù)組中增加元素

NSArray*arr2 = [arr1arrayByAddingObject:@"tiandadida"];

NSLog(@"%@",arr2);

[arr1release];

//排序

NSArray*arrnumber= [[NSArrayalloc]initWithObjects:@"nihao",@"wo",@"helloword",@"zhonggu",nil];

NSArray*te = [arrnumbersortedArrayUsingComparator: ^(NSString*s,NSString*s2){

if(s.length< s2.length){

returnNSOrderedDescending;

}

if(s.length> s2.length){

returnNSOrderedAscending;

}

// NSLog(@"...........................");

returnNSOrderedSame;

}];

NSLog(@"te=%@.",te);

//firstObjectCommonWithArray的用法

NSArray*arrnum= [[NSArrayalloc]initWithObjects:@"haoma",@"wo",@"helloword",@"zhongguo",nil];

NSLog(@"%@",[arrnumberfirstObjectCommonWithArray:arrnum]);

//快速枚舉

NSArray*ar = [NSArrayarrayWithObjects:@"one",@"two",@"three",nil];

for(NSString*elementinar) {

NSLog(@"element: %@",element);

}

//lastobject

NSLog(@"%@",[arlastObject]);

//把數(shù)組元素(字符串),連接起來(lái).

NSString*string = [arcomponentsJoinedByString:@"->"];

NSLog(@"%@",string);

//某個(gè)范圍的子串

NSRangetheRange;

theRange.location=0;//range的起點(diǎn)

theRange.length= [arcount] /2;//range的長(zhǎng)度

NSArray*halfArray = [arsubarrayWithRange:theRange];

NSLog(@"%@",halfArray);

//description Returns a string that represents the contents of the array, formatted as a property list.

NSString*test = [ardescription];

NSLog(@"%@",test);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容