比较安全:
public class SingletonInstance {
//私有构造方法
private SingletonInstance (){
}
private static class Builder{
//声明成员变量
private static SingletonInstance singletonInstance = new SingletonInstance();
}
//对外提供接口获取该实例
public static SingletonInstance getSingletonInstance(){
return Builder.singletonInstance ;
}
}
懒汉式:
public class Singleton{
private static Singleton singleton = null;
public static synchronized getInstance(){
if(singleton==null){
singleton = new Singleton();
}
return singleton;
}
}
饿汉式:
public class Singleton{
private static Singleton singleton = new Singleton ();
private Singleton (){
}
public Singleton getInstance(){
return singletion;
}
}
冒泡排序:
public static void main(String[] args) {
int[] arr={6,3,8,2,9,1};
for(int i=0;i<arr.length-1;i++){//外层循环控制排序趟数
for(int j=0;j<arr.length-1-i;j++){//内层循环控制每一趟排序多少次
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int num:arr){
System.out.print(num+" ");
}
}