我正在学习数组,基本上我有一个收集姓氏,名字和分数的数组.
我需要编写一个compareTo方法来比较姓氏和名字,以便列表可以按字母顺序从姓氏开始排序,然后如果两个人的姓氏相同,那么它将对第一个名称进行排序.
我很困惑,因为我书中的所有信息都是比较数字,而不是对象和字符串.
这是我到目前为止编码的内容.我知道这是错的,但它至少解释了我认为我在做什么:
public int compare(Object obj) // creating a method to compare
{
Student s = (Student) obj; // creating a student object
// I guess here I'm telling it to compare the last names?
int studentCompare = this.lastName.compareTo(s.getLastName());
if (studentCompare != 0)
return studentCompare;
else
{
if (this.getLastName() < s.getLastName())
return - 1;
if (this.getLastName() > s.getLastName())
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道<和>符号是错误的,但就像我说我的书只告诉你如何使用compareTo.
我想通过将一些字符转换为大写,其他一些字符转换为小写来修改Swift字符串.
在Obj-c我有以下内容:
- (NSString*) lowercaseDestination:(NSString*) string {
NSUInteger length = string.length;
unichar buf[length+1];
[string getCharacters:buf];
BOOL up = true;
for (int i=0; i< length ; i++) {
unichar chr = buf[i];
if( .... ) {
buf[i] = toupper(chr);
} else {
buf[i] = tolower(chr);
}
}
string = [NSString stringWithCharacters:buf length:length];
return string;
Run Code Online (Sandbox Code Playgroud)
你会如何在Swift 2中做到这一点?
我没有找到任何Character方法来提高或降低案例.
将String1个字符的数组作为选项吗?(然后使用String方法来上下各一个String