[关闭]
@fiy-fish 2015-07-15T20:08:37.000000Z 字数 5261 阅读 1372

day04-04-NSArray2

Objective-c


  1. // main.m
  2. // day04-04-NSArray2
  3. //
  4. // Created by Aaron on 15/7/6.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. /*
  9. NSArray *array = @[@1,@2,@3];
  10. [array count];
  11. [array objectAtIndex:0];
  12. id obj = array[0];
  13. [array indexOfObject:@1];
  14. [array containsObject:@1];
  15. [array firstObject];
  16. [array lastObject];
  17. [array indexOfObject:@1 inRange:NSMakeRange(0, 2)];
  18. [array subarrayWithRange:NSMakeRange(0, 2)];
  19. [array objectsAtIndexes:[NSIndexSet indexSetWithIndex:2]];
  20. */
  21. //数组的遍历
  22. #if 0
  23. int main(int argc, const char * argv[]) {
  24. @autoreleasepool {
  25. NSArray *array = @[@"hello",@"world",@"hello",@"nihao"];
  26. //第一种方式:循环
  27. #if 0
  28. for(int i = 0; i < array.count; i++)
  29. {
  30. id obj = array[i];
  31. NSLog(@"%@",obj);
  32. }
  33. for(NSInteger i = [array count]-1; i >= 0; i--)
  34. {
  35. id obj = array[i];
  36. NSLog(@"%@",obj);
  37. }
  38. #endif
  39. //第二种方式: for in 结构
  40. //快速遍历
  41. //不能方便的使用i做下标
  42. //在使用for in结构的时候,不要改变数组的容量
  43. //只能完整的正序遍历一次
  44. //for(id obj in array)
  45. for(NSString *obj in array)
  46. {
  47. // if([obj isKindOfClass:[NSNumber class]])
  48. // {
  49. //NSLog(@"%@ %ld",obj,[array indexOfObject:obj]);
  50. // }
  51. }
  52. //第三种方式:枚举器
  53. NSEnumerator *enumerator = [array objectEnumerator];
  54. enumerator = [array reverseObjectEnumerator];
  55. id obj = nil;
  56. while(obj = [enumerator nextObject])
  57. {
  58. NSLog(@"%@",obj);
  59. }
  60. }
  61. return 0;
  62. }
  63. #endif
  64. #if 0
  65. int main()
  66. {
  67. NSArray *array = @[@90,@88,@99,@50,@76,@52];
  68. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  69. for(int i = 0; i < [array count]; i++)
  70. {
  71. NSNumber *obj = [array objectAtIndex:i];
  72. if([obj integerValue] >= 60)
  73. {
  74. [indexSet addIndex:i];
  75. }
  76. }
  77. NSArray *sub = [array objectsAtIndexes:indexSet];
  78. NSLog(@"%@",sub);
  79. }
  80. #endif
  81. //字符串和数组
  82. #if 0
  83. int main()
  84. {
  85. NSString *path = @"/users/Aaron/desktop/file.txt";
  86. //以指定的字符串来切割字符串
  87. NSArray *array = [path componentsSeparatedByString:@"/d"];
  88. array = [path componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
  89. NSLog(@"%@",array);
  90. NSMutableString *str = [NSMutableString string];
  91. for(id obj in array)
  92. {
  93. if(![obj isEqualToString:@""])
  94. {
  95. [str appendFormat:@"/%@",obj];
  96. //NSLog(@"%@",obj);
  97. }
  98. }
  99. NSLog(@"%@",str);
  100. NSString *newStr = [array componentsJoinedByString:@"/"];
  101. NSLog(@"%@",newStr);
  102. //http://www.taobao.com/market/nvzhuang/qiang.php?spm=a21bo.7724922.8458.2.3nzdsq&acm=20140506001.1003.2.331846&uuid=AEDSuvcX_6UEJDkEXPAQCAXE5qpJkVjym&scm=1003.2.20140506001.OTHER_1436484331037_331846&pos=1
  103. NSArray *array1 = @[@"name=xiaobai",@"number=100",@"device=Iphone"];
  104. NSString *sss = [array1 componentsJoinedByString:@"&"];
  105. NSLog(@"%@",sss);
  106. }
  107. #endif
  108. //可变数组
  109. #if 0
  110. int main()
  111. {
  112. NSMutableArray *array = [NSMutableArray array];
  113. NSMutableArray *array1 = [NSMutableArray arrayWithArray:@[@1,@2]];
  114. //可变数组仅仅是在不可变的基础上添加了增删改
  115. //增
  116. //addObject: 往数组中增加一个元素,注意是增加到末尾
  117. [array1 addObject:@3];
  118. NSLog(@"%@",array1);
  119. //插入元素到指定的索引
  120. [array1 insertObject:@"..." atIndex:0];
  121. NSLog(@"%@",array1);
  122. //将其他数组的元素按顺序添加到当前数组
  123. [array addObjectsFromArray:array1];
  124. NSLog(@"-----%@",array);
  125. // NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  126. // [indexSet addIndex:0];
  127. // [indexSet addIndex:2];
  128. // [indexSet addIndex:1];
  129. // [indexSet addIndex:3];
  130. // [array insertObjects:array1 atIndexes:indexSet];
  131. // NSLog(@"%@",array);
  132. //删除数组中的某一个对象
  133. //只要出现的对象都删掉
  134. [array addObject:@"..."];
  135. //[array removeObject:@"..."];
  136. NSLog(@"%@",array);
  137. //删掉指定的索引对应的对象
  138. [array removeObjectAtIndex:0];
  139. NSLog(@"%@",array);
  140. //删除所有对象
  141. //[array removeAllObjects];
  142. //替换元素
  143. [array replaceObjectAtIndex:3 withObject:@"+++++"];
  144. NSLog(@"%@",array);
  145. //快速修改数组
  146. [array setArray:@[@0,@9,@8]];
  147. NSLog(@"%@",array);
  148. }
  149. #endif
  150. #if 0
  151. int main()
  152. {
  153. NSMutableArray *array = [NSMutableArray array];
  154. for(int i = 0; i < 5; i++)
  155. {
  156. int temp;
  157. scanf("%d",&temp);
  158. [array addObject:[NSNumber numberWithInt:temp]];
  159. }
  160. NSLog(@"%@",array);
  161. }
  162. #endif
  163. #if 0
  164. int main()
  165. {
  166. //数组排序
  167. NSMutableArray *array = [NSMutableArray array];
  168. for(int i = 0 ; i < 20; i++)
  169. {
  170. NSString *str = [NSString stringWithFormat:@"string%d",i];
  171. [array addObject:str];
  172. }
  173. //第一种方式: 自己用循环来实现
  174. for(int i = 0; i < [array count]-1; i++)
  175. {
  176. for(int j = 0; j < [array count]-i-1; j++)
  177. {
  178. if([(NSString *)[array objectAtIndex:j] compare:[array objectAtIndex:j+1]] == NSOrderedAscending)
  179. {
  180. [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
  181. }
  182. }
  183. }
  184. NSLog(@"%@",array);
  185. }
  186. #endif
  187. #import "Dog.h"
  188. //#if 0
  189. int main()
  190. {
  191. NSMutableArray *array = [NSMutableArray array];
  192. for(int i = 0; i < 10; i++)
  193. {
  194. Dog *dog = [[Dog alloc] initWithAge:arc4random_uniform(20) andWeight:arc4random_uniform(50)];
  195. [array addObject:dog];
  196. }
  197. NSLog(@"%@",array);
  198. //#endif
  199. //#if 0
  200. for(int i = 0; i < [array count]-1; i++)
  201. {
  202. for(int j = 0; j < [array count]-i-1; j++)
  203. {
  204. if([(Dog *)[array objectAtIndex:j] compareByAgeWithOtherDog:[array objectAtIndex:j+1]] == NSOrderedAscending)
  205. // if([[array objectAtIndex:j] age] < [[array objectAtIndex:j+1] age])
  206. {
  207. [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
  208. }
  209. }
  210. }
  211. //#endif
  212. //#if 0
  213. [array sortUsingSelector:@selector(sortByAgeWithOtherDog:)];
  214. NSLog(@"%@",array);
  215. }
  216. //#endif

  1. #import <Foundation/Foundation.h>
  2. @interface Dog : NSObject
  3. {
  4. NSInteger _age;
  5. NSInteger _weight;
  6. }
  7. -(instancetype)initWithAge:(NSInteger)age andWeight:(NSInteger)weight;
  8. -(NSComparisonResult)compareByAgeWithOtherDog:(Dog *)dog;
  9. -(BOOL)sortByAgeWithOtherDog:(Dog *)dog;
  10. -(NSInteger)age;
  11. //-(NSInteger)weight;
  12. @end

  1. #import "Dog.h"
  2. @implementation Dog
  3. -(instancetype)initWithAge:(NSInteger)age andWeight:(NSInteger)weight
  4. {
  5. if(self = [super init])
  6. {
  7. _age = age;
  8. _weight = weight;
  9. }
  10. return self;
  11. }
  12. -(NSInteger)age
  13. {
  14. return _age;
  15. }
  16. //-(NSInteger)weight{
  17. // return _weight;
  18. //}
  19. -(NSComparisonResult)compareByAgeWithOtherDog:(Dog *)dog
  20. {
  21. if(_age > [dog age])
  22. {
  23. return NSOrderedDescending;
  24. }
  25. else if(_age == [dog age])
  26. {
  27. return NSOrderedSame;
  28. }
  29. else
  30. {
  31. return NSOrderedAscending;
  32. }
  33. }
  34. -(BOOL)sortByAgeWithOtherDog:(Dog *)dog
  35. {
  36. return _age < [dog age];
  37. }
  38. -(NSString *)description
  39. {
  40. return [NSString stringWithFormat:@"dog age: %5ld weight: %5ld",_age,_weight];
  41. }
  42. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注