第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何修改此代碼以寫入文件?

如何修改此代碼以寫入文件?

有只小跳蛙 2022-06-23 20:03:05
我正在嘗試修改此堆算法,以顯示字符串輸入的所有可能排列。我正在嘗試修改代碼,以便能夠與帶有編寫器文件的 Scanner 類一起使用。當(dāng)我嘗試寫入一個(gè)新文件時(shí),它不會(huì)添加所有 24 個(gè)字符串,而是添加前 4 個(gè)。因?yàn)樗且粋€(gè) void 方法,我不能使用 pw.println(obj.heapPermutation(a, a.長(zhǎng)度,a.長(zhǎng)度))。有什么建議可以解決這個(gè)問題嗎?謝謝PS我在網(wǎng)上找到了這段代碼,我承認(rèn)它不是我的。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);
查看完整描述

1 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

你PrintWriter在 heapPermutation 方法中被初始化,因?yàn)樗沁f歸調(diào)用的,heapPermutation(a, size-1, n)每次都會(huì)被覆蓋。我相信默認(rèn)行為是替換文件,而不是附加到它。


您應(yīng)該創(chuàng)建一個(gè)構(gòu)造函數(shù)來初始化它,PrintWriter因此它不會(huì)每次都重新初始化。


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);



查看完整回答
反對(duì) 回復(fù) 2022-06-23
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)