Java使用数组和容器实现全排列
数组方式实现
package com.xyes.lqb.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 全排列 数组实现
* @author Avenger
*
*/
public class Task4 {
private static final Runtime RUNTIME = Runtime.getRuntime();
public static void main(String[] args) {
long startMem = RUNTIME.freeMemory(); //获取当前可用内存
long startTime = System.currentTimeMillis(); //获取当前时间
List<String[]> result = null; //结果
try {
PrintStream out = System.out; //将输出定为到文件中
PrintStream newOut = getPrintStream();
System.setOut(newOut);
result = run();
newOut.flush();
newOut.close();
System.setOut(out);
} catch (Throwable t) {
t.printStackTrace();
}
long endTime = System.currentTimeMillis();
long endMem = RUNTIME.freeMemory();
System.out.println("用时:" + (endTime - startTime) + "ms");
System.out.println("排列总数:" + result.size());
System.out.println("内存占用" + (endMem - startMem) / 1024 + "KB"); //出现负数时是java启动了垃圾回收器
}
public static List<String[]> run() { //计算方法
String[] source = getData(); //获取要计算的列表(数组)
List<String[]> result = new ArrayList<String[]>(); //创建结果
permutation(source, 0, result); //算法主体
for (String[] strings : result) {
System.out.println(Arrays.toString(strings)); //输出结果
}
return result;
}
/**
* 获取数据源
* @return
*/
public static String[] getData() { //获取以A开头的序列 eg.[A,B,C]
int count = 9;
String[] data = new String[count];
for (int i = 0; i < count; i++) {
byte[] bs = new byte[1];
bs[0] = (byte) (65 + i);
data[i] = new String(bs);
}
return data;
}
/**
* 排序算法
* @param strings 数据源
* @param start 开始位置
* @param result 保存结果的容器
*/
public static void permutation(String[] strings, int start, //全排列算法
List<String[]> result) {
if (start >= strings.length) { //当交换已经到结尾时添加结果
result.add(strings);
} else { //没有到结尾时递归
for (int i = start; i < strings.length; i++) { //从当前位置开始 逐个交换
String[] tmpStrings = new String[strings.length]; //创建新数组
System.arraycopy(strings, 0, tmpStrings, 0, strings.length); //复制原数组内容
swap(tmpStrings, start, i); //执行交换(注意交换的是刚刚创建的数组)
permutation(tmpStrings, start + 1, result); //递归求排序 并且开始位置加1
}
}
}
/**
* 交换方法
* @param source 要交换的数组
* @param index1 下标1
* @param index2 下标2
*/
public static void swap(String[] source, int index1, int index2) {
String tmp = source[index1];
source[index1] = source[index2];
source[index2] = tmp;
}
public static PrintStream getPrintStream() {
File file = new File("D:/test.txt");
try {
return new PrintStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
容器方式实现
package com.xyes.lqb.test;
import java.util.ArrayList;
import java.util.List;
public class Task2 {
private static final List<List<String>> RESULT_LIST = new ArrayList<List<String>>();
private static final Runtime RUNTIME = Runtime.getRuntime();
public static void main(String[] args) {
long startMem = RUNTIME.freeMemory();
long startTime = System.currentTimeMillis();
List<String> source = getData();
RESULT_LIST.clear(); //清空结果
permutation(source, 0);
if (RESULT_LIST != null) {
for (List<String> list : RESULT_LIST) {
System.out.println(list);
}
} else {
System.out.println("null");
}
long endTime = System.currentTimeMillis();
long endMem = RUNTIME.freeMemory();
System.out.println("用时:" + (endTime - startTime) + "ms");
System.out.println("排列总数:" + RESULT_LIST.size());
System.out.println("内存占用" + (endMem - startMem) / 1024 + "KB");
}
public static void permutation(List<String> list, int start) {
if (start >= list.size()) {
RESULT_LIST.add(list);
} else {
for (int i = start; i < list.size(); i++) {
List<String> tmplist = new ArrayList<String>(list);
swap(tmplist, i, start);
permutation(tmplist, start + 1);
}
}
}
public static List<String> getData() {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10 ; i++) {
list.add(String.format("item[%s]", i));
}
return list;
}
public static <T> void swap(List<T> list, int index1, int index2) {
T element1 = list.get(index1);
T element2 = list.get(index2);
list.set(index2, element1);
list.set(index1, element2);
}
}