import java.util.*;
public class TestMap {
public static void main(String[] args) {
Map map=new TreeMap();
map.put(1,new Elent(1,2));
map.put(2,new Elent(2,0));
map.put(3,new Elent(5,1));
map.put(4,new Elent(7,1));
printKeyValuePairs(map);
}
public static void printValues(Map map){
Collection vs=map.values();
Iterator it=vs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void printKeyValuePairs(Map map){
Set ks=map.keySet();
Iterator it=ks.iterator();
while(it.hasNext()){
Object key=it.next();
Object value=map.get(key);
System.out.println(key+" "+value);
}
}
}
class Elent{
int id,index;
public Elent(int id,int index){
this.id=id;
this.index=index;
}
public int getId(){
return id;
}
public int getIndex(){
return index;
}
}
怎么取KEY值为I的对象ID的值是否为0?
[code="java"]
import java.util.*;
public class TestMap {
public static void main(String[] args) {
Map map = new TreeMap();
map.put(1, new Elent(1, 2));
map.put(2, new Elent(2, 0));
map.put(3, new Elent(5, 1));
map.put(4, new Elent(7, 1));
printKeyValuePairs(map);
}
public static void printValues(Map map) {
Collection vs = map.values();
Iterator it = vs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public static void printKeyValuePairs(Map map) {
Set ks = map.keySet();
Iterator it = ks.iterator();
while (it.hasNext()) {
Object key = it.next();
[color=red]//这里进行强制转换存在map里的value的类型啊
Elent value = (Elent)map.get(key);
System.out.println(key + " " + value.getId());[/color]
}
}
}
class Elent {
int id, index;
public Elent(int id, int index) {
this.id = id;
this.index = index;
}
public int getId() {
return id;
}
public int getIndex() {
return index;
}
}
[/code]
lovewhzlq 大哥,写代码写得好累!
这么长。楼主可以放分了!
[code="java"]Object obj = map.get(1);
if(obj == null)
return ;
Elent e = (Elent)obj;
System.out.println(e.getIDValues());[/code]