Iphone数组一些基础操作NSArray/NSMutableArray
1 /*******************************************************************************************
2 NSArray
3 *******************************************************************************************/
4
5 /*---------------------------创建数组------------------------------*/
6 //NSArray *array = [NSArray alloc] initWithObjects:
7 @"One",@"Two",@"Three",@"Four",nil];
8
9 self.dataArray = array;
10 [array release];
11
12 //- (unsigned) Count;数组所包含对象个数;
13 NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
14
15 //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
16 NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
17
18
19 /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/
20
21 //arrayWithArray:
22 //NSArray *array1 = [NSArray alloc] init];
23 NSMutableArray *MutableArray = [NSMutableArray alloc] init];
24 NSArray *array = [NSArray arrayWithObjects:
25 @"a",@"b",@"c",nil];
26 NSLog(@"array:%@",array);
27 MutableArray = [NSMutableArray arrayWithArray:array];
28 NSLog(@"MutableArray:%@",MutableArray);
29
30 array1 = [NSArray arrayWithArray:array];
31 NSLog(@"array1:%@",array1);
32
33
34 //Copy
35
36 //id obj;
37 NSMutableArray *newArray = [NSMutableArray alloc] init];
38 NSArray *oldArray = [NSArray arrayWithObjects:
39 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
40
41 NSLog(@"oldArray:%@",oldArray);
42 for(int i = 0; i < [oldArray count]; i++)
43 {
44 obj = [oldArray objectAtIndex:i] copy];
45 [newArray addObject: obj];
46 }
47 //
48 NSLog(@"newArray:%@", newArray);
49 [newArray release];
50
51
52 //快速枚举
53
54 //NSMutableArray *newArray = [NSMutableArray alloc] init];
55 NSArray *oldArray = [NSArray arrayWithObjects:
56 @"a",@"b",@"c",@"d",@"e"
相关新闻>>
- 发表评论
-
- 最新评论 更多>>