List a == null是指List位址是空,而List a == 0 是List 位址為0xFDJJLJDKLASJDLA,空間已經開出來了裡面卻沒放東西
Prefix-increment and post increment 前置遞增和後置遞增的差異
public class UnaryOperatorDemo {
public static void main(String[] args) {
int age = 10;
int var = age++;
System.out.println(age);
System.out.println(var);
}
}
class Person {
public void iam()
{System.out.println(“I am a person.”);}
}
class Superman extends Person {
public void iam()
{System.out.println(“I am a Superman.”);}
}
Random ran = new Random();
int r = ran.nextInt(n) // create number 0~(n-1)
String 經常變動使用 StringBuilder
public static String intToString(int x){
boolean isNegtive = false;
if (x<0)
isNegtive = true;
x = -x;
}
StringBuilder s = new StringBuilder();
while(x != 0){
s.append((char)('0'+ x%10))}
x /= 10;
if (Negtive){
s.append('-');
}
s.reverse();
return s.toString();
}
Comparator and Comparable
Comparable: 類別本身繼承comparable,改寫compareTo,實現在類別內部
public class Student implements Comparable {
String name;
int age
public int compareTo(Student another) {
int i = 0;
i = name.compareTo(another.name);
if(i == 0) {
return age - another.age;
} else {
return i;
}
}
}
// 可以使用Collections.sort(StudentList)排序
public class Student{
String name;
int age
}
class StudentComparator implements Comparator {
public int compare(Student one, Student another) {
int i = 0;
i = one.name.compareTo(another.name);
if(i == 0) {
return one.age - another.age;
} else {
return i; }
}
}
//Collections.sort(StudentList , new StudentComparator())
//指定comparator
使用binarySearch必須指定Comparator
Collection.binarySearch(Object, target, CompGPA)
private static Comparator<student> compGPA = new Comparator<student>(){
public int compare(student s1, student s2){
};