如何在没有构造函数的情况下使此代码运行相同?

问题描述 投票:0回答:1

我是编程的新手,我正在尝试修改我在网上找到的堆算法。从前一个问题开始,我能够让代码与PrintWriter一起使用,但是当尝试将此函数用作另一个类中的方法时,由于构造函数而出现错误。如何在不使用构造函数的情况下修改此代码以使其工作相同?

我对编程不太熟悉,所以我尝试过查看以前的问题。不知何故,我想过使用嵌套类(不确定它们是如何工作的),但无济于事。该方法在其自己的类中有效。

// Should be within a class

private PrintWriter _pw;

// This is the part that needs to go.
public HeapAlgo(PrintWriter pw) {
   this._pw = pw;
}

public 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.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 void heap() throws IOException 
{
FileWriter fw = new FileWriter("note.txt");
PrintWriter pw = new PrintWriter(fw);
File temp = new File("code.txt");
Scanner file = new Scanner(temp);

String substring = "";

    String a[] = new String[4];
  a[0] = "" + file.nextLine(); 
  a[1] = "" + file.nextLine();
  a[2] = "" + file.nextLine();
  a[3] = "" + file.nextLine();



HeapAlgo obj = new HeapAlgo(pw); // Pass in a writer
obj.heapPermutation(a, a.length, a.length);
pw.close();
} 

当我在一个大类中运行方法时,我得到一个错误,说“错误:无效的方法声明;需要返回类型”。

任何帮助将不胜感激。谢谢。

编辑:我正在尝试编写此构造函数:

    public CodeRunner() 
    { 
    random(); 
    HeapAlgo.heap(//not sure if anything should go here); 
    algorithm(); 
    }

random()创建随机字符串,算法函数对随机字符串的所有可能迭代执行算法。我试图为每组随机字符串创建对象。

java algorithm constructor printwriter
1个回答
0
投票

看起来以下元素应该在名为HeapAlgo的类中:

  1. 私有变量声明
private PrintWriter _pw;
  1. 构造函数本身
public HeapAlgo(PrintWriter pw)
  1. heapPermutation函数
public void heapPermutation(String a[], int size, int n) throws IOException

最后剩下的方法heap()应该放在其他一些类中(可能是你的main()函数所在的位置)并从那里调用。

或者,您确实可以使用内部类。将您在类中提供的所有代码(可能称为Heap)包装起来,然后将上述三个元素包装在一个名为HeapAlgo的内部类中。像这样的东西(我很快打了这个,所以你可能需要修复错误):

public class HeapUtil {
    public class HeapAlgo {
        private PrintWriter _pw;

        // This is the part that needs to go.
        public HeapAlgo(PrintWriter pw) {
           this._pw = pw;
        }

        public PrintWriter getPrintWriter(){
            return _pw;
        }

        public 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.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 HeapAlgo heap() throws IOException 
    {
        FileWriter fw = new FileWriter("note.txt");
        PrintWriter pw = new PrintWriter(fw);
        File temp = new File("code.txt");
        Scanner file = new Scanner(temp);

        String substring = "";

        String a[] = new String[4];
        a[0] = "" + file.nextLine(); 
        a[1] = "" + file.nextLine();
        a[2] = "" + file.nextLine();
        a[3] = "" + file.nextLine();

        HeapAlgo obj = new HeapAlgo(pw); // Pass in a writer
        obj.heapPermutation(a, a.length, a.length);
        return obj;
    }
}

请注意,在这种情况下,如果您想在此类文件之外使用HeapAlgo,则需要使用Heap.HeapAlgo


编辑:试试上面的代码(我编辑过)。可能有一些错误,因为我没有实际运行它。

用法如下:

public CodeRunner(){ 
    random(); 
    // heapAlgo is the heap object
    HeapAlgo heapAlgo = HeapUtil.heap(); 
    // this gives you access to the PrintWriter inside the HeapAlgo
    PrintWriter printWriter = heapAlgo.getPrintWriter();
    // do your other stuff
    algorithm(); 
}
© www.soinside.com 2019 - 2024. All rights reserved.