【前言】
- OC中使用兩種數(shù)組,第一種是OC數(shù)組,第二種是C語(yǔ)言的數(shù)組,使用OC數(shù)組存放對(duì)象類型,使用C語(yǔ)言數(shù)組存放基礎(chǔ)數(shù)據(jù)類型
- OC數(shù)組也是一個(gè)類,我們使用的數(shù)組也是一個(gè)對(duì)象
- OC數(shù)組存放的是對(duì)象的指針,一個(gè)對(duì)象可以存放到多個(gè)數(shù)組中,通過(guò)一個(gè)數(shù)組中得指針改變了對(duì)象的內(nèi)容,其他數(shù)組中得指針讀取對(duì)象也會(huì)發(fā)生相應(yīng)的變化
關(guān)鍵詞:NSMutableArray : NSArray
-
NSArray
// 常見(jiàn)創(chuàng)建方法
- (id)initWithObjects:(id)firstObj, ... ;
+ (id)arrayWithObjects:(id)firstObj, ... ;
// 獲取數(shù)組元素個(gè)數(shù)
- (NSUInteger)count;
// 通過(guò)索引獲取相應(yīng)的元素
- (id)objectAtIndex:(NSUInteger)index;
// 通過(guò)對(duì)象地址獲取在數(shù)組中的索引
- (NSUInteger)indexOfObject:(id)anObject;
// 判斷數(shù)組中數(shù)組包含元素anObject
- (BOOL)containsObject:(id)anObject;
// 獲取數(shù)組的最后一個(gè)元素
- (id)lastObject;
// 把數(shù)組元素內(nèi)容按照字符串separator進(jìn)行拼接
- (NSString *)componentsJoinedByString:(NSString *)separator;
-
NSMutableArray
<1>增加數(shù)組元素
// 追加元素
- (void)addObject:(id)anObject;
// 指定索引插入元素
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
// 追加一個(gè)數(shù)組
- (void)addObjectsFromArray:(NSArray *)otherArray;
<2>刪除
// 刪除最后一個(gè)元素
- (void)removeLastObject;
// 刪除指定索引的元素
- (void)removeObjectAtIndex:(NSUInteger)index;
// 刪除所有元素
- (void)removeAllObjects;
//在一定范圍刪除指定的元素
- (void)removeObject:(id)anObject inRange:(NSRange)range;
// 刪除指定的元素
- (void)removeObject:(id)anObject;
// 根據(jù)一個(gè)數(shù)組刪除指定的元素
- (void)removeObjectsInArray:(NSArray *)otherArray;
// 修改數(shù)組
- (void)setArray:(NSArray *)otherArray;
// 替換指定索引的元素
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
// 交換數(shù)組元素
- (void)exchangeObjectAtIndex:(NSUInteger)index withObjectAtIndex:(NSUInteger)idx2;