1、找出如下代碼的錯誤
public class test {
public test() {
}
test a;
{
a = new test();
}
static test s;
static {
s = new test();
}
public static void main(String[] args) {
new test();
}
}
錯誤:執行個體變數初始化的時候出問題 他那個屬性是自己的類, 所以在建立的時候就會一直在那裡迴圈
錯誤原理:靜態變數執行個體了 一次就在記憶體中共用。 而執行個體變數每次都會分記憶體空間,每次都會建立執行個體
2、Java枚舉
package test.test2;
public enum Trice {
RED(1){
public Trice getNext(){
try {
Thread.sleep(super.a*1000);
System.out.println("the thread sleep" + super.a);
} catch (InterruptedException e) {
e.printStackTrace();
}
return GREEN.getNext();
}
},
GREEN(2)
{
public Trice getNext(){
try {
Thread.sleep(super.a*1000);
System.out.println("the thread sleep" + super.a);
} catch (InterruptedException e) {
e.printStackTrace();
}
return YELLOW.getNext();
}
},YELLOW(3)
{
public Trice getNext(){
try {
Thread.sleep(super.a*1000);
System.out.println("the thread sleep" + super.a);
} catch (InterruptedException e) {
e.printStackTrace();
}
return RED.getNext();
}
};
public int a;
private Trice(int a){
this.a=a;
}
public abstract Trice getNext();
}
Java枚舉變數預設繼承外層枚舉類,所以實現父類方法
3、最快實現清除List重複項的方法:使用HashSet