有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如果没有构造器,我怎么能让这段代码实现相同的功能?

我对编程非常陌生,我正在尝试修改我在网上找到的堆算法。根据前面的问题,我能够让代码与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()创建随机字符串,algorithm函数对随机字符串的所有可能迭代执行算法。我试图为每组随机字符串创建对象


共 (1) 个答案

  1. # 1 楼答案

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

    1. 私有变量声明
    private PrintWriter _pw;
    
    1. 构造函数本身
    public HeapAlgo(PrintWriter pw)
    
    1. Heapper变异函数
    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(); 
    }