没有合适的资源?快使用搜索试试~ 我知道了~
首页java 遍历Map及Map转化为二维数组的实例
java 遍历Map及Map转化为二维数组的实例
8 下载量 176 浏览量
更新于2023-03-16
评论
收藏 33KB PDF 举报
主要介绍了java 遍历Map及Map转化为二维数组的实例的相关资料,希望通过本文能帮助到大家,实现这样的功能,需要的朋友可以参考下
资源详情
资源评论
资源推荐
java 遍历遍历Map及及Map转化为二维数组的实例转化为二维数组的实例
主要介绍了java 遍历Map及Map转化为二维数组的实例的相关资料,希望通过本文能帮助到大家,实现这样的功
能,需要的朋友可以参考下
java 遍历遍历Map及及Map转化为二维数组的实例转化为二维数组的实例
实例代码:实例代码:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) {
int a = 0, b = 0, c = 0;
// 第一种:通过Map.keySet()遍历Map及将Map转化为二维数组
Map<String, String> map1 = new HashMap<String, String>();
map1.put("012013012013", "张三");
map1.put("012013012014", "张四");
String[][] group1 = new String[map1.size()][2];
System.out.println("第一种:通过Map.keySet()遍历map1的key和value");
for (String key : map1.keySet()) {
System.out.println("key = " + key + " and value = " + map1.get(key));
group1[a][0] = key;
group1[a][1] = map1.get(key);
a++;
}
System.out.println("map1.size()为:" + map1.size() + ",a为:" + a + ",group1数组的长度为:" + group1.length);
System.out.println("----------------------------------------------------");
for(int n = 0; n < group1.length; n++) {
System.out.println("key = " + group1[n][0] + " and value = " + group1[n][1]);
}
// 第二种:通过Map.entrySet()使用iterator()遍历Map及将Map转化为二维数组
Map<String, String> map2 = new HashMap<String, String>();
map2.put("112013012013", "李三");
map2.put("112013012014", "李四");
System.out.println("" + "第二种:通过Map.entrySet()使用iterator()遍历map2的key和value");
Iterator<Map.Entry<String, String>> iterator = map2.entrySet().iterator();
String[][] group2 = new String[map2.size()][2];
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
group2[b][0] = entry.getKey();
group2[b][1] = entry.getValue();
b++;
}
System.out.println("map2.size()为:" + map2.size() + ",b为:" + b + ",group2数组的长度为:" + group2.length);
System.out.println("----------------------------------------------------");
for(int n = 0; n < group2.length; n++) {
System.out.println("key = " + group2[n][0] + " and value = " + group2[n][1]);
}
// 第三种:通过Map.entrySet()遍历遍历Map及将Map转化为二维数组
Map<String, String> map = new HashMap<String, String>();
map.putAll(map1);
map.putAll(map2);
String[][] group3 = new String[map.size()][2];
System.out.println("" + "第三种:通过Map.entrySet()遍历map的key和value ");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue());
group3[c][0] = entry.getKey();
group3[c][1] = entry.getValue();
c++;
}
System.out.println("map.size()为:" + map.size() + ",c为:" + c + ",group3数组的长度为:" + group3.length);
System.out.println("----------------------------------------------------");
for(int n = 0; n < group3.length; n++) {
System.out.println("key = " + group3[n][0] + " and value = " + group3[n][1]);
}
}
}
输出结果为:
第一种:通过Map.keySet()遍历map1的key和value
weixin_38663007
- 粉丝: 4
- 资源: 904
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0