Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修改此代码以写入文件?_Java_Java.util.scanner_Permutation_Filewriter_Printwriter - Fatal编程技术网

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

Java 如何修改此代码以写入文件?,java,java.util.scanner,permutation,filewriter,printwriter,Java,Java.util.scanner,Permutation,Filewriter,Printwriter,我试图修改这个堆的算法,该算法显示字符串输入的所有可能排列。我正在尝试修改代码,以便能够与带有writer文件的Scanner类一起使用。当我尝试写入一个新文件时,它并没有像应该的那样添加所有24个字符串,而是添加了前4个字符串。因为它是一个void方法,所以我不能使用pw.println(obj.heapermutation(a,a.length,a.length))。有没有解决这个问题的建议 谢谢 另外,我在网上找到了这个代码,我承认它不是我的 import java.util.Scanne

我试图修改这个堆的算法,该算法显示字符串输入的所有可能排列。我正在尝试修改代码,以便能够与带有writer文件的Scanner类一起使用。当我尝试写入一个新文件时,它并没有像应该的那样添加所有24个字符串,而是添加了前4个字符串。因为它是一个void方法,所以我不能使用pw.println(obj.heapermutation(a,a.length,a.length))。有没有解决这个问题的建议

谢谢

另外,我在网上找到了这个代码,我承认它不是我的

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);
import java.util.Scanner;
导入java.io.*;
导入静态java.lang.System.*;
希帕尔戈班
{ 
void-heaperBartation(字符串a[],int-size,int-n)引发IOException
{ 
FileWriter fw=新的FileWriter(“note.txt”);
PrintWriter pw=新的PrintWriter(fw);
//如果大小变为1,则打印获得的
//排列
如果(大小==1)

对于(int i=0;i您的
PrintWriter
是在heapermutation方法中初始化的,因为它是递归调用的
heapermutation(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);
类HeapAlgo{ //在类级别创建编写器 私人印刷作家; //创建一个构造函数来分配编写器 公共HeapAlgo(印刷撰稿人pw){ 这个._pw=pw; } void HeapperOtation(字符串a[],int size,int n)引发IOException{ //如果大小变为1,则打印获得的 //排列 如果(大小==1) 对于(int i=0;i