Java 更改类的数据结构以使用字符串
我有一个实用的int拓扑排序类。但是我需要改变我的方法,用字符串a,b代替整数,但问题是,计算节点数的方法也被用于arraylist,所以我几周来一直在尝试改变,但它总是说转换不好,或者不能转换为int,即使在我的类中某一点上没有int,所以我的问题是我只想让它说q.addEdgea,b; 这是我的代码,如果有任何帮助,也许可以把它变成节点或字符串,我将永远欠你的债Java 更改类的数据结构以使用字符串,java,Java,我有一个实用的int拓扑排序类。但是我需要改变我的方法,用字符串a,b代替整数,但问题是,计算节点数的方法也被用于arraylist,所以我几周来一直在尝试改变,但它总是说转换不好,或者不能转换为int,即使在我的类中某一点上没有int,所以我的问题是我只想让它说q.addEdgea,b; 这是我的代码,如果有任何帮助,也许可以把它变成节点或字符串,我将永远欠你的债 // A Java program to print topological sorting of a graph // usin
// A Java program to print topological sorting of a graph
// using indegrees
import java.util.*;
//Class to represent a graph
class Graph
{
int V;// No. of vertices
//An Array of List which contains
//references to the Adjacency List of
//each vertex
List <Integer> adj[];
public Graph(int V){//constructor
this.V = V;
adj = new ArrayList[V];
for(int i = 0; i < V; i++)
adj[i]=new ArrayList<Integer>();
}
// function to add an tasks to tree
public void addEdge(int u,int v){
adj[u].add(v);
}
// prints a Topological Sort of the complete graph
public void topologicalSort(){
// Create a array to store indegrees of all
// vertices. Initialize all indegrees as 0.
int indegree[] = new int[V];
// Traverse adjacency lists to fill indegrees of
// vertices. This step takes O(V+E) time
for(int i = 0; i < V; i++){
ArrayList<Integer> temp = (ArrayList<Integer>) adj[i];
for(int node : temp){
indegree[node]++;
}
}
// Create a queue and enqueue all vertices with
// indegree 0
Queue<Integer> q = new LinkedList<Integer>();
for(int i = 0;i < V; i++){
if(indegree[i]==0)
q.add(i);
}
// Initialize count of visited vertices
int cnt = 0;
// Create a vector to store result (A topological
// ordering of the vertices)
Vector <Integer> topOrder=new Vector<Integer>();
while(!q.isEmpty()){
// Extract front of queue (or perform dequeue)
// and add it to topological order
int u=q.poll();
topOrder.add(u);
// Iterate through all its neighbouring nodes
// of dequeued node u and decrease their in-degree
// by 1
for(int node : adj[u]){
// If in-degree becomes zero, add it to queue
if(--indegree[node] == 0)
q.add(node);
}
cnt++;
}
// Check if there was a cycle
if(cnt != V){
System.out.println("There exists a cycle in the graph");
return ;
}//else{System.out.println("no cycle in the graph");}
// Print topological order
for(int i : topOrder){
System.out.print(i+" ");
}
}
}
// Driver program to test above functions
class Main
{
public static void main(String args[])
{
Graph g=new Graph(8);//must be max number of letters+1
Node one=new Node("a");
Node two=new Node("b");
g.addEdge(one,two);
/*
g.addEdge(1, 4);
g.addEdge(2, 5);
g.addEdge(7, 5);
g.addEdge(5, 4);
g.addEdge(3, 4);
g.addEdge(4, 6);
g.addEdge(2, 4);*/
System.out.println("Following is a Topological Sort");
g.topologicalSort();
}
}
您可以向Graph添加一个类型,然后它将支持任何类型。例如:
class Graph<T> {
private final Map<T, List<T>> edgesByRoot = new HashMap<>();
public void addEdge(T a, T b){
getEdges(a).add(b);
}
private List<T> getEdges(T root) {
List<T> edges = edgesByRoot.get(root);
if (edges == null) {
edges = new ArrayList<>();
edgesByRoot.put(root, edges);
}
return edges;
}
}
class Main {
public static void main(String args[]) {
Graph<String> g=new Graph<>();
g.addEdge("a", "b");
System.out.println("Following is a Topological Sort");
g.topologicalSort();
}
}
在此基础上,实现拓扑排序,通过GetEdge使用映射而不是数组进行边查找,并构建一个列表。请确保在必要时也使用.equals而不是==,并且不要使用可变类型,因为t String是不可变类型,所以您在这方面做得很好
另一个注意事项是,不要使用向量。它与ArrayList完全相同,但在其所有方法上都有同步的关键字。它实际上没有那么有用,如果您确实需要它,建议使用Collections.synchronizedList,因为它不是多线程的,所以您的代码不需要它
工具书类
docs.oracle.com
docs.oracle.com
堆栈溢出
你能提供一个请删除不必要的注释吗?没有Node类的源代码作为我今天看到的最合适的答案。我越来越接近了,现在我只需要弄清楚如何正确使用映射,我打算使用hashtable,你会推荐哪一个?图g=新图;给出了一个错误:无法推断图形的类型参数,只能在新图形5中给它一个数字时接受;零件和仅显示0,1,2,3,4哈希表与向量属于同一类别。其中Vector是过时的ArrayList实现,Hashtable是过时的HashMap实现。HashMap与Hashtable完全相同。对于其他问题,请确保删除现有构造函数。