Map映射的main方法里面,到底该怎么样的new()一个对象

lu_script 2009-12-28 05:40:14
RT

public class MapTest2 {
public Map<String, Integer> putkey(String string, Integer integer) {
Map<String, Integer> tempMap = new HashMap<String, Integer>();
tempMap.put("a", 1);
tempMap.put("b", 2);
tempMap.put("c", 3);
return tempMap;

}

public static void main(String[] args) {
Map map = new HashMap();
MapTest2 mt = new MapTest2();

Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(" key " + key + " value " + value);
}
}
}
...全文
213 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lu_script 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ycnanevol 的回复:]
Map map = putkey("",0);
[/Quote]


谁帮忙解释下


lu_script 2009-12-28
  • 打赏
  • 举报
回复
我知道要new 一个实例,但是这个写法“Map map = putkey("",0);”

不知道
谁帮忙解释下
lu_script 2009-12-28
  • 打赏
  • 举报
回复
Map map = putkey("",0);
为啥也可以了?
nickycheng 2009-12-28
  • 打赏
  • 举报
回复
终于看明白了!

MapTest2 mt = new MapTest2();
Map map = mt.putkey(null, null);
Iterator it = map.entrySet().iterator();
...

lu_script 2009-12-28
  • 打赏
  • 举报
回复
能否解释一下代码啊?我不会Map
lu_script 2009-12-28
  • 打赏
  • 举报
回复
key a value 1
key c value 3
key b value 2
lu_script 2009-12-28
  • 打赏
  • 举报
回复
public class MapTest2 {
public static Map<String, Integer> putkey(String string, Integer integer) {
Map<String, Integer> tempMap = new HashMap<String, Integer>();
tempMap.put("a", 1);
tempMap.put("b", 2);
tempMap.put("c", 3);
return tempMap;

}

public static void main(String[] args) {
Map map = putkey("",0);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(" key " + key + " value " + value);
}
}
}
ycnanevol 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ycnanevol 的回复:]
Map map = putkey("",0);
[/Quote]

putkey方法 前加个static
lu_script 2009-12-28
  • 打赏
  • 举报
回复
是读出来,数据都写进去了
是new HashMap();
还是 new MapTest2();
ycnanevol 2009-12-28
  • 打赏
  • 举报
回复
Map map = putkey("",0);
nickycheng 2009-12-28
  • 打赏
  • 举报
回复
lu_script 2009-12-28
  • 打赏
  • 举报
回复
这段代码,我就是想把键值对读出来,但是main函数怎么写了?
jiayouxh 2009-12-28
  • 打赏
  • 举报
回复
没看明白。、

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧