一:【數(shù)組】
【注】OC兼容C的數(shù)組,C的數(shù)組用于存儲基礎(chǔ)數(shù)據(jù)類型(int, char, float)數(shù)據(jù)和復(fù)合數(shù)據(jù)類型 (int *, int[10])數(shù)據(jù);使用OC的數(shù)組對象存儲類的對象。
【注】NSMutableArray : NSArray
1.NSArray的方法NSMutableArray都可以用
2.傳參需要傳入NSArray * 也可以傳入NSMutableArray *
一.不可變數(shù)組NSArray
【注】不可變數(shù)組,指數(shù)組對象一旦創(chuàng)建,其元素的個數(shù)和順序不可修改。
NSArray * array = @[@"One One", @"Two", @"Three", @"Four", @"dDD"];
//數(shù)組本身是一個對象
//數(shù)組的元素如@"One" @"Two" @"Three"等都是任意類的對象,不僅限于字符串。
//創(chuàng)建數(shù)組對象時傳參,傳入對象的地址
//數(shù)組中只是存儲了對象的地址,而非存儲了對象的本體。
//同一個對象可以存儲到兩個數(shù)組當中,仍然是一個對象
//在一個數(shù)組中修改了對象,在另一個數(shù)組中讀取對象,會發(fā)現(xiàn)對象也被修改了。
//遍歷打印一個數(shù)組
//打印一個數(shù)組,就是打印數(shù)組的元素
NSLog(@"%@", array);
//返回array數(shù)組的元素個數(shù)
NSUInteger count = [array count];
NSLog(@"%lu", count);
NSArray * array = @[@"One One", @"Two", @"Three", @"Four", dog];
【方法】
1.根據(jù)索引,返回數(shù)組的元素
Dog * dog2 = [array1 objectAtIndex:0];
Dog * dog3 = array2[0];
//這個種方法是等價的,返回第0個元素的地址,因為元素是Dog對象,所以用指向這種對象的指針來接收
2.返回數(shù)組的元素個數(shù)
NSUInteger count = [array count];
3.字符串的分割 strtok
NSArray * subStrings = [str componentsSeparatedByString:@" "];
NSArray * subStrings = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]];
4.數(shù)組中字符串的拼接
NSString * _str = [subStrings componentsJoinedByString:@"*"];
5.數(shù)組的遍歷 快速枚舉法
//通過索引遍歷
for (int i = 0; i < [array count]; i++) {
NSLog(@"%@", array[i]);
}
//快速枚舉法
for (NSString * str in array) {
//第一次循環(huán)str指向數(shù)組第一個元素,第二次循環(huán),str指向數(shù)組第二個元素。。。
NSLog(@"%@", str);
}
【注】在for in中break和continue都能使用
【注】如果遍歷的是可變數(shù)組,在for in結(jié)束前,不能修改可變數(shù)組的元素個數(shù)和順序。
二.可變數(shù)組NSMutableArray
1.重置可變數(shù)組
[array setArray:@[@"One", @"Two", @"Three", @"Four"]];
2.增加元素
追加:(添加元素)
[array addObject:@"Five"];
插入:
[array insertObject:@"Six" atIndex:2];
3.刪除元素
//刪除元素
//刪除指定元素,傳入需要刪除的元素的地址,刪除這個元素
//如果要刪的是字符串,只需傳入和所刪字符串相等的字符串就可以了,不用傳入同一個字符串。
//如果數(shù)組中有多個@"Four"會一起刪除
[array removeObject:@"Four"];
//刪除指定索引的元素
[array removeObjectAtIndex:2];
4.交換兩個元素的位置
[array exchangeObjectAtIndex:0 withObjectAtIndex:2];
二:【字典】
【注】NSMutableDictionary : NSDictionary
一.字典
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"One", @"1", @"Two", @"2", @"Three", @"3", nil];
NSDictionary * dict2 = @{@"1": @"One", @"2": @"Two", @"3": @"Three"};
1.返回鍵值對個數(shù)
NSUInteger count = [dict2 count];
2.通過鍵返回值
NSString * str = [dict2 objectForKey:@"2"];
3.返回所有的鍵或值
//返回所有的鍵
NSArray * keys = [dict2 allKeys];
//返回所有的值
NSArray * values = [dict2 allValues];
4.快速枚舉法遍歷
//快速枚舉法遍歷
for (NSString * key in dict2) {
//快速枚舉法只能遍歷字典的鍵
NSLog(@"%@", [dict2 objectForKey:key]);
//通過key再去查找值
}
二.可變字典
1.重置字典
[dict setDictionary:dict];
2.添加鍵值對
[dict setObject:@"Four" forKey:@"0"];
3.刪除鍵值對
[dict removeObjectForKey:@"0"];
字典實操
//1.創(chuàng)建學(xué)生類,成員變量有姓名,年齡和成績
//用學(xué)生類,創(chuàng)建三個學(xué)生對象,將三個學(xué)生添加到字典中,以學(xué)生的名字作為key。
//輸入一個學(xué)生的名字,打印出該學(xué)生的完整信息。
//學(xué)生對象是值,學(xué)生名字的字符串是key
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 1. 獲取對象
Student *studentOne = [[Student alloc] initWithName:@"EZ" age:21 score:23.1];
Student *studentTwo = [[Student alloc] initWithName:@"VN" age:10 score:34.5];
Student *studentThree = [[Student alloc] initWithName:@"TM" age:23 score:56.9];
// 2. 存儲對象
NSDictionary *dict = @{
@"EZ":studentOne,
@"VN":studentTwo,
@"TM":studentThree
};
//3. 獲取學(xué)生的姓名
char name[20];
scanf("%s",name);
NSString *nameOfStudent = [NSString stringWithFormat:@"%s",name];
//4.從字典中取出學(xué)生對象
Student *result = dict [nameOfStudent];
if (result) {
[result printfDetailOfStudent];
}
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *_name;
int _age;
float _score;
}
- (instancetype)initWithName:(NSString *)name age:(int)age score:(float )score;
- (void)printfDetailOfStudent;
@end
#import "Student.h"
@implementation Student
- (instancetype)initWithName:(NSString *)name age:(int)age score:(float)score
{
if (self = [super init]) {
_name = name;
_age = age;
_score = score;
}
return self;
}
- (void)printfDetailOfStudent
{
NSLog(@"學(xué)生的名字是:%@ 年齡是:%d 生物成績:%f",_name,_age,_score);
}
@end
’
練習(xí):
1.已知有數(shù)組@[@“One”, @“Two”, @“Three”, @“Four”];
編寫程序,遍歷打印數(shù)組的每個元素。
*2.創(chuàng)建一個可變字符串,將字符串中每個字符,逆序
- (NSMutableString *)reverseMutableString:(NSMutableString *)str;
//傳入@“hello”
//變成@“olleh”
字典初認識
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 成對出現(xiàn)(鍵值對)
// 數(shù)據(jù)沒有順序
// key value
// 實例化字典的方法一
NSDictionary *dict = @{@"1":@"呵呵",@"2":@"哈哈",@"3":@"嘿嘿"};
// 實例化字典的方法二
NSDictionary *dictTwo = [[NSDictionary alloc] initWithObjectsAndKeys:@"呵呵",@"1",@"哈哈",@"2",@"嘿嘿",@"3", nil];
// 從字典中取值
NSString *str = dict[@"1"];
NSString *strTwo = [dict objectForKey:@"2"];
// 1. 返回鍵值對的個數(shù)
NSInteger count = [dict count];
NSInteger countT = dict.count;
// 2. 返回所有的鍵
NSArray *arrayKeys = [dict allKeys];
NSLog(@"%@",arrayKeys);
// 3. 返回所有的值
// NSArray *arrayValues = [dict allValues];
// NSLog(@"%@",arrayValues);
// 遍歷字典
// 快速枚舉法 只能遍歷字典的鍵
/* for (NSString *key in dict) {
NSLog(@"----%@",key);
NSLog(@"%@",dict[key]);
}
*/
// 可變字典
/*
NSMutableDictionary *dictM =
[[NSMutableDictionary alloc] init];
// 重置
[dictM setDictionary:dict];
NSLog(@"%@",dictM);
// 2.添加鍵值對
[dictM setObject:@"20000000" forKey:@"you"];
NSLog(@"%@",dictM);
// 3.刪除鍵值對
// [dictM removeObjectForKey:@"3"];
*/
}
return 0;
}
作業(yè):
【數(shù)組】
1.創(chuàng)建一個數(shù)組,元素是@"One", @"Two", @"Three", @"Four",使用三種方法 遍歷打印數(shù)組的元素。
NSLog(@"%@",array);
2.創(chuàng)建一個數(shù)組,元素是 5 個 Dog 類的對象地址,遍歷數(shù)組,使每個元素執(zhí)行 bark 函數(shù)。
3.創(chuàng)建一個數(shù)組,元素是兩個字符串對象地址和三個Dog類的對象地址,遍歷數(shù)組,如果是字符串那么輸出"I am a string!"如果是 Dog 類的對象那么執(zhí)行 bark 方法。
4.設(shè)計?個學(xué)生類,成員有姓名,年齡,學(xué)號,成績。 要求,以上述每?成員,分別進行排序(學(xué)號按升序,成績按降序,年齡升序,名字降序)。
5.@“Yes I am a so bad man”,按照單詞逆序
6.有兩個字符串@"This is a boy";@"really fucking bad"; 將這兩個字符串單詞,交疊,形成系的字符串 @"This really is fucking a bad boy"。
【字典】
1.創(chuàng)建學(xué)生類,成員變量有姓名,年齡和成績
用學(xué)生類,創(chuàng)建三個學(xué)生對象,將三個學(xué)生添加到字典中,以學(xué)生的名字作為key。
輸入一個學(xué)生的名字,打印出該學(xué)生的完整信息。
//學(xué)生對象是值,學(xué)生名字的字符串是key
【自學(xué)】
NSArray:
- (NSArray *)arrayByAddingObject:(id)anObject;
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
- (NSUInteger)indexOfObject:(id)anObject;
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;
NSMutableArray:
- (void)addObjectsFromArray:(NSArray *)otherArray;
- (void)removeObjectsInArray:(NSArray *)otherArray;
- (void)removeObjectsInRange:(NSRange)range;
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
//- (void)sortUsingComparator:(NSComparator)cmptr;
五.數(shù)組的排序
【選擇法】
5 4 3 2 1
4 5 3 2 1
1 5 4 3 2
1 4 5 3 2
1 3 5 4 2
1 2 5 4 3
// 第一步
if (a[0]>a[1]) {
int tmp = a[0];
a[0] = a[1];
a[1] = tmp;
}
// 第二步
for (int i = 1; i < 5; i ++ ) {
if (a[0]>a[i]) {
int tmp = a[0];
a[0] = a[i];
a[i] = tmp;
}
}
// 第三步
int a[5] = {5,4,3,2,1};
for (int i = 0; i < 5 - 1; i ++) { // 用第幾個數(shù)和以后的對比
for (int j = i +1; j < 5; j ++) {
if (a[i] > a[j]) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
// 打印
for (int i = 0; i < 5 ;i ++) {
printf("%d",a[i]);
}
printf("\n");
if 1
NSLog(@"dd");
else
NSLog(@"ss");
endif
【冒泡法】
5 4 3 2 1
4 5 3 2 1
4 3 5 2 1
// 第一步
if (a[0] > a[1]) {
int tmp = a[0];
a[0] = a[1];
a[1] = tmp;
}
// 第二步
for (int i = 0; i < 4; i ++) {
if (a[i] > a[i +1]) {
int tmp = a[i];
a[i] = a[i +1];
a[i +1] = tmp;
}
}
// 第三步
int a[5] = {5,4,3,2,1};
for (int i = 0; i < 5 - 1; i ++) {
for (int j = 0; j < 5 - i - 1; j ++) {
if (a[j] > a[j + 1]) {
int tmp = a[j];
a[j] = a[j + 1 ];
a[j+1] = tmp;
}
}
}
for (int i = 0; i < 5 ;i ++) {
printf("%d",a[i]);
}
printf("\n");