猿问

如何修改此代码以写入文件?

我正在尝试修改此堆算法,以显示字符串输入的所有可能排列。我正在尝试修改代码,以便能够与带有编写器文件的 Scanner 类一起使用。当我尝试写入一个新文件时,它不会添加所有 24 个字符串,而是添加前 4 个。因为它是一个 void 方法,我不能使用 pw.println(obj.heapPermutation(a, a.长度,a.长度))。有什么建议可以解决这个问题吗?


谢谢


PS我在网上找到了这段代码,我承认它不是我的。


import java.util.Scanner;

import java.io.*;

import static java.lang.System.*;


class HeapAlgo 





    void heapPermutation(String a[], int size, int n) throws IOException

    { 

       FileWriter fw = new FileWriter("note.txt");

       PrintWriter pw = new PrintWriter(fw);



 // if size becomes 1 then prints the obtained 

 // permutation 

       if (size == 1) 

          for (int i=0; i<n; i++)

          { 

              System.out.println(a[i] + "");




          } 



          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 


   HeapAlgo obj = new HeapAlgo(); 

   String a[] = new String["abcd","bbbb","cccc","dddd"];

   obj.heapPermutation(a, a.length, a.length);


有只小跳蛙
浏览 91回答 1
1回答

慕标琳琳

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

相关分类

Java
我要回答