1 回答
TA贡献1830条经验 获得超9个赞
你PrintWriter在 heapPermutation 方法中被初始化,因为它是递归调用的,heapPermutation(a, size-1, n)每次都会被覆盖。我相信默认行为是替换文件,而不是附加到它。
您应该创建一个构造函数来初始化它,PrintWriter因此它不会每次都重新初始化。
class HeapAlgo {
// create a writer at the class level
private PrintWriter _pw;
// Create a constructor to assign the writer
public HeapAlgo(PrintWriter pw) {
this._pw = pw;
}
void heapPermutation(String a[], int size, int n) throws IOException {
// if size becomes 1 then prints the obtained
// permutation
if (size == 1)
for (int i=0; i<n; i++) {
System.out.println(a[i] + "");
this._pw.print(a[i] + ""); // print here I belive?
}
for (int i=0; i<size; i++) {
heapPermutation(a, size-1, n);
// if size is odd, swap first and last
// element
if (size % 2 == 1) {
String temp = a[0];
a[0] = a[size-1];
a[size-1] = temp;
}
// If size is even, swap ith and last
// element
else {
String temp = a[i];
a[i] = a[size-1];
a[size-1] = temp;
}
}
}
}
public static void main(String args[]) throws IOException {
FileWriter fw = new FileWriter("note.txt");
PrintWriter pw = new PrintWriter(fw);
HeapAlgo obj = new HeapAlgo(pw); // Pass in a writer
String a[] = new String["abcd","bbbb","cccc","dddd"];
obj.heapPermutation(a, a.length, a.length);
添加回答
举报