迭代器报错
自己为一个类编写了一个迭代器如下:
package lc.test;
import java.util.Iterator;
public class Test {
/**
* @param args
*/
String[] str = { "asd", "zxc", "qwe", "rty" };
public Iterator<String> iterator() {
return new Iterator<String>() {
private int i = 0;
public boolean hasNext() {
return i < str.length;
}
public String next() {
System.out.println("hehe");
return str[i++];
}
public void remove() {
}
};
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test c1 = new Test();
Iterator<String> = c1.iterator();
}
}
红色一行会显示如下错误:
- Syntax error on token ">", VariableDeclaratorId expected after this token
百思不得其解,求解答
------解决方案--------------------Iterator<String> = c1.iterator();
你沒有聲明變量名
起碼也要這樣啊
Iterator<String> it = c1.iterator();
------解决方案--------------------是啊,就楼上说的,你的Itertor也是个泛型对象么,你怎么没有写对象名:
Iterator<String> it = c1.iterator();