NSArray ----NSMutableArray
?
//NSArray
?Foundation中數(shù)組(NSArray)是有序的對(duì)象集合
?NSArray只能存儲(chǔ)Objective-C的對(duì)象,而不能存儲(chǔ)像 int、float這些基本數(shù)據(jù)類型,但是Objective-C對(duì)C 兼容,所以在Objective-C程序中,仍然可以使用C的 數(shù)組來存儲(chǔ)基本數(shù)據(jù)類型
?NSArray?一旦創(chuàng)建便不可以再對(duì)它就進(jìn)行更改,如果 要進(jìn)行對(duì)數(shù)組的增、刪、改等操作的話,需要使用 NSArray的子類NSMutableArray來創(chuàng)建對(duì)象
?
//NSArray常用方法
?+arrayWithObjects:使用?一組對(duì)象創(chuàng)建?一個(gè)數(shù) 組(注: 1,該方法可以接收可變數(shù)目的參數(shù)。2, 最后?一個(gè)值指定為nil,表示參數(shù)列表結(jié)束)
?-objectAtIndex:用數(shù)組索引檢索數(shù)組中的元素
?-count:返回?cái)?shù)組元素個(gè)數(shù)
?
//NSMutableArray
?NSMutableArray是NSArray的子類,繼承了
NSArray的所有方法,并添加了新的方法
?NSMutableArray用來處理可變數(shù)組
?
//+arrayWithCapacity:為可變數(shù)組指定初始容量
?-addObject:向可變數(shù)組的末尾添加?一個(gè)元素
?-addObjectsFromArray:將另外?一個(gè)數(shù)組的所有元素添加 到調(diào)用該方法的數(shù)組中
?-insertObject:atIndex:將?一個(gè)元素添加到數(shù)組指定的位 置上
?-removeObjectAtIndex:移除數(shù)組中指定位置上元素
?-removeObject:移除數(shù)組中指定元素
?
//快速遍歷
for(類名 *對(duì)象名 in 需要遍歷的對(duì)象)
{
?? //. . .
}
?
//
//? main.m
//? shiyueshihaoshuzu
//
//? Created by iphone on 11-10-10.
//? Copyright 2011年 __MyCompanyName__. Allrights reserved.
//
#import<Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
?? //@autoreleasepool {
?? ?? ?
??? ? NSAutoreleasePool* pool=[[NSAutoreleasePoolalloc] init];
? ?? ? // insert codehere...
? ?? ? NSArray *array=[NSArray arrayWithObjects:@"one",@"two",@"no",nil];//? 創(chuàng)建數(shù)組, nil表示空值,表示參數(shù)列表的結(jié)束
?? ?? ? NSLog(@"%@",array);
?? ?? ? NSArray * arr=[NSArray arrayWithObjects:@"hello", nil];
?? ?? ? NSLog(@"%@",arr);
?? ?? ? id rr=[array objectAtIndex:1];//用數(shù)組索引檢索數(shù)組中的元素
?? ?? ? NSLog(@"id%@",rr);
? ? ?? ?
? ? ?? longint i=[array count]; //輸出數(shù)組里的元素個(gè)數(shù)
? ? ?? ? NSLog(@"%ld",i); ?? ?
?? ?? ?
??? ? [NSMutableArrayarrayWithCapacity:20];//分配內(nèi)存空間
?? ?
??? ? NSMutableArray*nsarr=[NSMutableArrayarrayWithObjects:@"hi",@"hello", nil];
??? ? [nsarr addObject:@"no"];//-addObject:向可變數(shù)組的末尾添加?一個(gè)元素
?? ?? ? NSLog(@"%@",nsarr);
?? NSMutableArray *nsarr2=[NSMutableArray arrayWithObjects:@"a",@"b",nil];
? ?[nsarr addObjectsFromArray:nsarr2];//-addObjectsFromArray:將另外?一個(gè)數(shù)組的所有元素添加到調(diào)用該方法的數(shù)組中,將一個(gè)數(shù)組里的內(nèi)容添加到另一個(gè)數(shù)組中
?? NSLog(@"---------%@",nsarr);
? ?[nsarr2 insertObject:@"c"atIndex:1];
? ? NSLog(@"%@",nsarr2);
? ?[nsarr2 removeObjectAtIndex:0];//-removeObjectAtIndex:移除數(shù)組中指定位置上元素
? ? NSLog(@"%@",nsarr2);
? ?[nsarr2 removeObject:@"b"];//-removeObjectAtIndex:移除數(shù)組中指定位置上元素
? ? NSLog(@"%@",nsarr2);
?? ?
? ? for(NSString * tem in array)//遍歷
? ? {
? ? ?? NSLog(@"tem is:%@",tem);//輸出數(shù)組里面的元素
? ? }
?? ?
?? NSLog(@"--------------------");
?? ?
? ? for(inti=0;i<[arraycount];i++)//類似C里面的
? ? {
? ? ?? NSLog(@"%@",[array objectAtIndex:i]);
? ? }
?? ?
?? //}
? ? [pool drain];
? ? return 0;
}//
轉(zhuǎn)載于:https://www.cnblogs.com/allanliu/p/4246724.html
總結(jié)
以上是生活随笔為你收集整理的NSArray ----NSMutableArray的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CapsLock魔改大法——变废为宝实现
- 下一篇: Longest Substring wi