`
Chris_bing
  • 浏览: 6449 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
全排列 java, 设计模式 含有重复字符的字符数组的全排列
public class repeatAllArray {
 /*
  * allArray实现含有重复字符的数组的全排列
  * list是含重复字符的数组,i是指示当前位置的游标,length是数组长度
  * 注释:这个函数最核心的地方,我感觉是else块的代码
  */
 public void allArray(char list[], int i, int length) {
  if (i == length - 1) {
   for (int j = 0; j < length; j++) {
    System.out.print(list[j]);
   }
   System.out.println();
  } else {
   for (int j = i; j < length; j++) {
    if (!isExist(list, i, j)) {
     swap(list, i, j);// 交换当前值和当前位置之后的值
     allArray(list, i + 1, length);// 当前位置+1,递归
     swap(list, i, j);// 再交换
    }
   }
  }
 }
 
    /*
     * isExist判断j位置的字符是否已经在list[0]~list[j-1]中出现过了
     * list是含重复字符的数组,i是指示当前位置的游标,j是要判断的字符的位置
     */
 public boolean isExist(char list[], int i, int j) {
  for (int k = i; k < j; k++) {
   if (list[j] == list[k]) {
    return true;
   }
  }
  return false;
 }
 /*
  * swap实现了数组中两个位置的值的交换
  * list是含重复字符的数组,i,j表示要交换的位置
  */
 public void swap(char list[], int i, int j) {
  char temp = list[i];
  list[i] = list[j];
  list[j] = temp;
 }
 public static void main(String[] args) {
  char a[] = { 'a', 'a', 'b', 'b' };//以这个字符数组为例
  repeatAllArray array = new repeatAllArray();
  array.allArray(a, 0, a.length);
 }
}

线程安全的单例 java, 设计模式 单例模式之线程安全解析
public class ResourceFactory{
    private static class ResourceHolder{
        public static Resource resource = new Resource();      
    }     
    public static Resource getResource() {
        return ResourceFactory.ResourceHolder.resource;      
    }
}   
Global site tag (gtag.js) - Google Analytics