内部静态泛型 class <<Cannot be resolved to a type>>
Inner static generic class <<Cannot be resolved to a type>>
我正在尝试使用 HashMap 和 Generics.
创建相邻的 Graph 列表
但是,我不明白为什么我会收到错误消息“Edge cannot be resolved to a type”
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
Scanner sc=new Scanner(new File("input.txt"));
PrintWriter pw=new PrintWriter(new File("output.txt"));
int roads = sc.nextInt(); // How many Edges we have
int vertices = sc.nextInt(); // How many vertices Graph has
Graph<String> g = new Graph<String>(vertices); // Every Vertex is a String
for (int i=0;i<roads;i++) {
g.add(sc.next(), sc.next());
}
//Here i get an error
Edge<String> e; //Edge cannot be resolved to a type
}
static class Graph<E> {
HashMap<E,Edge<E>> m;
public Graph(int vertices) {
m = new HashMap<E,Edge<E>>(vertices);
}
public void add(E from,E to) {
if (m.get(from)==null) m.put(from,new Edge<E>(to,null));
else m.put(from, new Edge<E>(to,m.get(from)));
}
//Storing Graph as an adjacent list of edges
static class Edge<E> {
E to;
Edge<E> prev;
public Edge(E to, Edge<E> prev) {
this.to = to;
this.prev = prev;
}
}
//
}
}
请详细说明...
提前致谢
不要试图将 Edge
包含在 Graph
中。将其移动到并行 class,或外部 class。像
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("input.txt"));
PrintWriter pw = new PrintWriter(new File("output.txt"));
int roads = sc.nextInt(); // How many Edges we have
int vertices = sc.nextInt(); // How many vertices Graph has
Graph<String> g = new Graph<String>(vertices); // Every Vertex is a
// String
for (int i = 0; i < roads; i++) {
g.add(sc.next(), sc.next());
}
// Here i get an error
Edge<String> e; // Edge cannot be resolved to a type
}
static class Graph<E> {
HashMap<E, Edge<E>> m;
public Graph(int vertices) {
m = new HashMap<E, Edge<E>>(vertices);
}
public void add(E from, E to) {
if (m.get(from) == null)
m.put(from, new Edge<E>(to, null));
else
m.put(from, new Edge<E>(to, m.get(from)));
}
}
// Storing Graph as an adjacent list of edges
static class Edge<E> {
E to;
Edge<E> prev;
public Edge(E to, Edge<E> prev) {
this.to = to;
this.prev = prev;
}
}
}
Edge
是 Graph
中的内部 class,所以在 Graph
中你可以直接调用它 Edge
... 但是 main
不在 Graph
.
内
从 class Main
内部(包括方法 main
),您可以将其称为 Graph.Edge
(使用通用参数这将是 Graph.Edge<String>
).
在 class Main
之外,您可以将其称为 Main.Graph.Edge
(使用泛型,Main.Graph.Edge<String>
)。
我正在尝试使用 HashMap 和 Generics.
创建相邻的 Graph 列表
但是,我不明白为什么我会收到错误消息“Edge cannot be resolved to a type”
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
Scanner sc=new Scanner(new File("input.txt"));
PrintWriter pw=new PrintWriter(new File("output.txt"));
int roads = sc.nextInt(); // How many Edges we have
int vertices = sc.nextInt(); // How many vertices Graph has
Graph<String> g = new Graph<String>(vertices); // Every Vertex is a String
for (int i=0;i<roads;i++) {
g.add(sc.next(), sc.next());
}
//Here i get an error
Edge<String> e; //Edge cannot be resolved to a type
}
static class Graph<E> {
HashMap<E,Edge<E>> m;
public Graph(int vertices) {
m = new HashMap<E,Edge<E>>(vertices);
}
public void add(E from,E to) {
if (m.get(from)==null) m.put(from,new Edge<E>(to,null));
else m.put(from, new Edge<E>(to,m.get(from)));
}
//Storing Graph as an adjacent list of edges
static class Edge<E> {
E to;
Edge<E> prev;
public Edge(E to, Edge<E> prev) {
this.to = to;
this.prev = prev;
}
}
//
}
}
请详细说明...
提前致谢
不要试图将 Edge
包含在 Graph
中。将其移动到并行 class,或外部 class。像
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("input.txt"));
PrintWriter pw = new PrintWriter(new File("output.txt"));
int roads = sc.nextInt(); // How many Edges we have
int vertices = sc.nextInt(); // How many vertices Graph has
Graph<String> g = new Graph<String>(vertices); // Every Vertex is a
// String
for (int i = 0; i < roads; i++) {
g.add(sc.next(), sc.next());
}
// Here i get an error
Edge<String> e; // Edge cannot be resolved to a type
}
static class Graph<E> {
HashMap<E, Edge<E>> m;
public Graph(int vertices) {
m = new HashMap<E, Edge<E>>(vertices);
}
public void add(E from, E to) {
if (m.get(from) == null)
m.put(from, new Edge<E>(to, null));
else
m.put(from, new Edge<E>(to, m.get(from)));
}
}
// Storing Graph as an adjacent list of edges
static class Edge<E> {
E to;
Edge<E> prev;
public Edge(E to, Edge<E> prev) {
this.to = to;
this.prev = prev;
}
}
}
Edge
是 Graph
中的内部 class,所以在 Graph
中你可以直接调用它 Edge
... 但是 main
不在 Graph
.
从 class Main
内部(包括方法 main
),您可以将其称为 Graph.Edge
(使用通用参数这将是 Graph.Edge<String>
).
在 class Main
之外,您可以将其称为 Main.Graph.Edge
(使用泛型,Main.Graph.Edge<String>
)。