通用迭代器的声明
Declaration of generic Iterators
我正在编写一个名为 graph 的 class,我正在用 Hashmap 表示有向图。我想创建一个以下列方式打印出整个图形的方法:
key1: value13, valeue17, ..
key2: value21, ...
其中 value13 是 node1(key1) 指向的 node3 的值。
所以,对于类似 1->2->3,并且 2 也指向 4 的东西,我需要:
1: 2
2: 3,4
我的代码如下所示:
public class Graph<T>{
Map<Node<T>, List<Node<T>>> graph;
//constructors and methods
void printGraph(){
System.out.println(graph.keySet().iterator().next().value); // is printing 7
Iterator itKey = graph.keySet().iterator();
System.out.println(itKey.next()); // printing Graph$Node@15db9742
System.out.println(itKey.next().value); //error
while(itKey.hasNext()){
//code
}
public static void main(String[] args){
Graph<Integer> graph = new Graph<>();
Node<Integer> n1 = new Node<>(7);
Node<Integer> n2 = new Node<>(2);
graph.connect(n1, n2);
graph.printGraph();
}
}
我的问题出现在方法 printGraph()
中,我在其中定义了一个 Iterator
。我想做的是创建一个迭代器
在一组键上,然后为每个键创建一个迭代器,该迭代器将打印所有值。如您所见,如果我尝试打印 System.out.println(graph.keySet().iterator().next().value);
我得到 7,这是有道理的,因为它是我 keySet()
中第一个键的值。如果我用另一种方式来初始化迭代器,Iterator itKey = graph.keySet().iterator();
,这是一个指向 Node
:
的迭代器
System.out.println(itKey.next()); // printing Graph$Node@15db9742
虽然,如果我尝试打印它的值:
System.out.println(itKey.next().value); //error
我收到以下错误:
error: cannot find symbol
System.out.println(itKey.next().value);
^
symbol: variable value
location: class Object
1 error
这不应该是一回事吗?为什么会出现错误?
您应该提供通用迭代器而不是非通用迭代器。如果非通用它的 return Object 类型作为元素然后你需要将它类型转换为 Node 这不好,所以最好在获取 Iterator 实例时定义类型。
Iterator<Node<T>> itKey = graph.keySet().iterator();
while(itKey.hasNext()){
System.out.println(itKey.next().value);
}
你的
itKey.next().value
给出 错误 因为迭代器 不 知道 value
是什么。 可能 将 itKey.next()
转换为 Node
会起作用 但这不是打印图表的最理想方式。
您可以使用下面的方法。它使用条目集迭代 graph
映射。
printGraph
函数:
void printGraph() {
for (Map.Entry<Node<T>, List<Node<T>>> entry : graph.entrySet()) {
Node<T> fromNode = entry.getKey();
System.out.print(fromNode.value + " ->");
for (Node<T> toNode : entry.getValue())
System.out.print(" " + toNode.value);
System.out.println();
}
}
main
函数:
Node<Integer> n1 = new Node<>(1);
Node<Integer> n2 = new Node<>(2);
Node<Integer> n3 = new Node<>(3);
Node<Integer> n4 = new Node<>(4);
Node<Integer> n5 = new Node<>(5);
Graph<Integer> graph = new Graph<>();
graph.connect(n1, n2);
graph.connect(n1, n3);
graph.connect(n4, n5);
graph.printGraph();
打印:
4 -> 5
1 -> 2 3
那是一个编译错误,因为你的 Iterator itKey
有一个原始类型;所以调用 itKey.next()
将 return 和 Object
。您想要为迭代器指定正确的类型,以便 iterator.next()
具有 return 类型 Node
.
在您的代码中,只需更改 itKey
变量的类型
void printGraph() {
System.out.println(graph.keySet().iterator().next().value);
// use the non-raw type here
Iterator<Node<T>> itKey = graph.keySet().iterator();
System.out.println(itKey.next());
System.out.println(itKey.next().value);
while (itKey.hasNext()) {
// code
}
}
行System.out.println(graph.keySet().iterator().next().value);
编译,因为没有类型信息丢失。查看涉及的类型:
graph
变量的类型为 Map<Node<T>, List<Node<T>>>
graph.keySet()
类型为 Set<Node<T>>
graph.keySet().iterator()
的类型为 Iterator<Node<T>>
graph.keySet().iterator().next()
类型为 Node<T>
并且由于最后一个 next()
的类型是 Node
,我们可以得到它的 value
.
我正在编写一个名为 graph 的 class,我正在用 Hashmap 表示有向图。我想创建一个以下列方式打印出整个图形的方法:
key1: value13, valeue17, ..
key2: value21, ...
其中 value13 是 node1(key1) 指向的 node3 的值。 所以,对于类似 1->2->3,并且 2 也指向 4 的东西,我需要:
1: 2
2: 3,4
我的代码如下所示:
public class Graph<T>{
Map<Node<T>, List<Node<T>>> graph;
//constructors and methods
void printGraph(){
System.out.println(graph.keySet().iterator().next().value); // is printing 7
Iterator itKey = graph.keySet().iterator();
System.out.println(itKey.next()); // printing Graph$Node@15db9742
System.out.println(itKey.next().value); //error
while(itKey.hasNext()){
//code
}
public static void main(String[] args){
Graph<Integer> graph = new Graph<>();
Node<Integer> n1 = new Node<>(7);
Node<Integer> n2 = new Node<>(2);
graph.connect(n1, n2);
graph.printGraph();
}
}
我的问题出现在方法 printGraph()
中,我在其中定义了一个 Iterator
。我想做的是创建一个迭代器
在一组键上,然后为每个键创建一个迭代器,该迭代器将打印所有值。如您所见,如果我尝试打印 System.out.println(graph.keySet().iterator().next().value);
我得到 7,这是有道理的,因为它是我 keySet()
中第一个键的值。如果我用另一种方式来初始化迭代器,Iterator itKey = graph.keySet().iterator();
,这是一个指向 Node
:
System.out.println(itKey.next()); // printing Graph$Node@15db9742
虽然,如果我尝试打印它的值:
System.out.println(itKey.next().value); //error
我收到以下错误:
error: cannot find symbol
System.out.println(itKey.next().value);
^
symbol: variable value
location: class Object
1 error
这不应该是一回事吗?为什么会出现错误?
您应该提供通用迭代器而不是非通用迭代器。如果非通用它的 return Object 类型作为元素然后你需要将它类型转换为 Node 这不好,所以最好在获取 Iterator 实例时定义类型。
Iterator<Node<T>> itKey = graph.keySet().iterator();
while(itKey.hasNext()){
System.out.println(itKey.next().value);
}
你的
itKey.next().value
给出 错误 因为迭代器 不 知道 value
是什么。 可能 将 itKey.next()
转换为 Node
会起作用 但这不是打印图表的最理想方式。
您可以使用下面的方法。它使用条目集迭代 graph
映射。
printGraph
函数:
void printGraph() {
for (Map.Entry<Node<T>, List<Node<T>>> entry : graph.entrySet()) {
Node<T> fromNode = entry.getKey();
System.out.print(fromNode.value + " ->");
for (Node<T> toNode : entry.getValue())
System.out.print(" " + toNode.value);
System.out.println();
}
}
main
函数:
Node<Integer> n1 = new Node<>(1);
Node<Integer> n2 = new Node<>(2);
Node<Integer> n3 = new Node<>(3);
Node<Integer> n4 = new Node<>(4);
Node<Integer> n5 = new Node<>(5);
Graph<Integer> graph = new Graph<>();
graph.connect(n1, n2);
graph.connect(n1, n3);
graph.connect(n4, n5);
graph.printGraph();
打印:
4 -> 5
1 -> 2 3
那是一个编译错误,因为你的 Iterator itKey
有一个原始类型;所以调用 itKey.next()
将 return 和 Object
。您想要为迭代器指定正确的类型,以便 iterator.next()
具有 return 类型 Node
.
在您的代码中,只需更改 itKey
变量的类型
void printGraph() {
System.out.println(graph.keySet().iterator().next().value);
// use the non-raw type here
Iterator<Node<T>> itKey = graph.keySet().iterator();
System.out.println(itKey.next());
System.out.println(itKey.next().value);
while (itKey.hasNext()) {
// code
}
}
行System.out.println(graph.keySet().iterator().next().value);
编译,因为没有类型信息丢失。查看涉及的类型:
graph
变量的类型为Map<Node<T>, List<Node<T>>>
graph.keySet()
类型为Set<Node<T>>
graph.keySet().iterator()
的类型为Iterator<Node<T>>
graph.keySet().iterator().next()
类型为Node<T>
并且由于最后一个 next()
的类型是 Node
,我们可以得到它的 value
.