java - 如果没有构造函数,我怎样才能让这段代码发挥同样的作用?

标签 java algorithm constructor printwriter

我是编程新手,我正在尝试修改我在网上找到的堆算法。从上一个问题中,我能够获得与 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() 创建随机字符串,算法函数对随机字符串的所有可能迭代执行算法。我正在尝试为每组随机字符串创建对象。

最佳答案

似乎以下元素应该在名为 HeapAlgo 的类中:

  1. 私有(private)变量声明
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(); 
}

关于java - 如果没有构造函数,我怎样才能让这段代码发挥同样的作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670101/

相关文章:

java - 在Eclipse上创建两个Android项目

c++ - `std::list<>::sort()` - 为什么突然切换到自上而下的策略?

这个动画 Java 小程序背后的算法

c++ - 移动构造函数 C++

java - mysql.jdbc.driver类未找到异常

java - Android/Java JDBC MySQL - 获取行 - 有效算法

java - 序列化方法调用是什么意思?

algorithm - 了解 BFS 的运行时间复杂度

c++ - 构造函数重载造成困惑

javascript - 如何在 JavaScript 中迭代对象原型(prototype)的属性?