gpt4 book ai didi

java - 我如何调用该程序中的方法?

转载 作者:行者123 更新时间:2023-11-30 05:39:03 25 4
gpt4 key购买 nike

我正在尝试测试我的程序以了解它是如何工作的,但我不确定如何在主方法中调用它。我尝试过使用Assignment5Solution.findOrder(),但它不起作用。任何有关此问题的帮助将不胜感激。该代码应该获取学生必须选修的类(class)数量以及每门类(class)的先决条件(如果有),并给出学生应该选修的类(class)的正确顺序。

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

最佳答案

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)类:

public class Assignment5Solution {

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

编辑2:

如果你想将它用作非静态方法,你需要这样做(将 null 和 0 更改为实际值):

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

关于java - 我如何调用该程序中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com