import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.AbstractSet;
public class Entry_1 implements Map.Entry<Integer,String>{
static int index;
public Entry_1(int i){ index=i;}
public Integer getKey(){return index;}
public String getValue(){return "加油!";}
public boolean equals(Object o){
System.out.println("Must invok here?");
throw new UnsupportedOperationException();
}
public String setValue(String s){
throw new UnsupportedOperationException();
}
//返回内部类EntrySets
public static EntrySets getf(int i) {
return new EntrySets(i);
}
//定义内部类EntrySets
static class EntrySets extends AbstractSet<Map.Entry<Integer, String>>{
int size;
public EntrySets(int i){
size=i;
}
public int size() {
return 0;
}
int count=0;//为什么这里改成小于等于0的数时有输出,把count改成大于0时输出都是空的啊?
//内部类迭代器方法
public Iterator<Map.Entry<Integer, String>> iterator() {
return new Iterator<Map.Entry<Integer, String>>(){
public boolean hasNext(){
return count<=size;
}
public Map.Entry<Integer, String> next(){
////////////////////////System.out.println(size);
count++;
return new Entry_1(count);
}
public void remove(){
}
};
}
}//////////////////////////////////////EntrySets
@SuppressWarnings("serial")
static Map<Integer,String> f(int in){
return new HashMap<Integer,String>(){
public Set<Map.Entry<Integer,String>> entrySet(){
return getf(index);
}
};
}
public static void main(String[]args){
for(int i:new Integer[]{5,10,2,3,4,5,6,7,8,}){
System.out.println(f(i));
}
}
}
/////想不通啊。
------解决方案--------------------------------------------------------
下面代码中的参数in没传给getf方法,是故意这么写的么, 根据这个debug一下吧;
@SuppressWarnings("serial")
static Map<Integer,String> f(int in){
return new HashMap<Integer,String>(){
public Set<Map.Entry<Integer,String>> entrySet(){
return getf(index);
}
};
------解决方案--------------------------------------------------------