如何在该程序中调用该方法?

时间:2019-05-08 18:25:40

标签: java

我正在尝试测试我的程序以查看其工作方式,但是我不确定如何在main方法中调用它。我试过做Assignment5Solution.findOrder(),但是它不起作用。任何与此问题的帮助将不胜感激。该代码应考虑到学生必须参加的课程数量以及每门课程的前提条件(如果有的话),并对学生应该参加的课程进行正确的排序。

package Assignment5;

import java.lang.reflect.Array;
import java.util.*;

/**
 *
 * @author harpe
 */
class Assignment5Solution {

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int E = prerequisites.length;
        Graph G = new Graph(numCourses);
        for (int i = 0; i < E; i++) {
            G.addEdge(prerequisites[i][1], prerequisites[i][0]);
        } // Graph is constructed

        DFS d = new DFS(G); // depth first search
        return d.reverseDFSorder();
    }

    public class DFS {

        private boolean[] marked;
        private int[] courseOrder; // i.e., reverse post order
        private boolean hasCycle;
        private int index; // index for the array courseOrder, index 0 is for the course taken first, …
        private HashSet<Integer> callStack; // used to detect if there are cycles on the graph

        DFS(Graph G) {
            marked = new boolean[G.V()];
            courseOrder = new int[G.V()];
            index = courseOrder.length - 1; // index 0 of courseOrder will be course taken first, lastIndex will be taken last

            callStack = new HashSet<Integer>(); // HashSet is a hash table, for O(1) search

            for (int v = 0; v < G.V(); v++) { // to visit each node, including those on islands or isolated
                if (!marked[v] && !hasCycle) {
                    dfs(G, v);
                }
            }
        }

        private void dfs(Graph G, int v) {
            marked[v] = true;
            callStack.add(v); // use HashSet to simulate callStack
            for (int w : G.adj(v)) {
                if (!marked[w]) {
                    dfs(G, w);
                } else if (callStack.contains(w)) // search in HashSet is O(1)
                {
                    hasCycle = true; // this is a cycle!
                    break;
                }
            }
            callStack.remove(v);
            courseOrder[index--] = v; // index starts from array length -1, decrease by 1 each time, and then ends at 0
        }

        public int[] reverseDFSorder() {
            if (hasCycle) {
                return new int[0]; // return an empty int array (with size 0)
            }
            return courseOrder;
        }
    } // end of class DFS

    public class Graph {

        private int V;
        private List[] adj;

        Graph(int V) // constructor
        {
            this.V = V;
            adj = new List[V];
            for (int i = 0; i < V; i++) {
                adj[i] = new ArrayList<Integer>();
            }
        }

        public void addEdge(int v, int w) {
            adj[v].add(w);
        }

        public Iterable<Integer> adj(int v) {
            return adj[v];
        }

        public int V() {
            return V;
        }
    } // end of class Graph
} // end of class Solution 

1 个答案:

答案 0 :(得分:1)

public int[] findOrder(int numCourses, int[][] prerequisites) {}

必须是:

public static int[] findOrder(int numCourses, int[][] prerequisites) {}

static关键字意味着您无需声明该类的对象即可使用它。因此,您可以通过以下方式使用它:

Assignment5Solution.findOrder(numCourses, prerequisites) 
//numCourses and prerequisites can be any int and int[][] respectively.

编辑:还要注意另一点,具体取决于您的主要方法在哪里,您可能需要使用以下方法将类Assignment5Solution设置为公共类:

public class Assignment5Solution {

当前受包装保护,因此只有在同一包装中才能使用。

EDIT2:

如果要将其用作非静态方法,则需要执行以下操作(将null和0更改为实际值):

    Assignment5Solution test = new Assignment5Solution() {};
    int numCourses = 0;
    int [][] prereqs = null;
    int[] reverseOrder = test.findOrder(numCourses, prereqs);