5.字符串:
“aa:[email protected]!bb:[email protected]!cc:[email protected]”
将存入hashMap中
key:aa,bb,cc
value:[email protected],[email protected],[email protected];
然后循环遍历打印输出。(10分)
程序描述:
package exam2;
import java.util.HashMap;
import java.util.Map;
public class Test5 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
String str = "aa:[email protected]!bb:[email protected]!cc:[email protected]";
String[] strArr = str.split("!");//["aa:[email protected],bb:[email protected],cc:[email protected]"]
for(String ele:strArr){
String[] subArr = ele.split(":");
map.put(subArr[0],subArr[1]);
}
map.put("aa","[email protected]");
System.out.println(map);
}
}
这是结果的照片效果图
在这是第二种写法,
package com.qianfeng.ps.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="aa:[email protected]!bb:[email protected]!cc:[email protected]";
cunqu(str);
}
private static void cunqu(String str) {
Map<String,String> map = new HashMap<String,String>();
String strs[] = str.split("!"); //aa:[email protected]!bb:[email protected]!cc:[email protected]
System.out.println("打印key 值");
for(int i=0;i<strs.length;i++){
String[] s = strs[i].split(":");
map.put(s[0],s[1]);
// System.out.println(map.put(s[0], s[1]));
//System.out.println(s[1]);
}
// //第一种方式(增强for循环)遍历map集合
Set<String> keySet = map.keySet();
// System.out.println("打印vale值:");
for(String key : keySet){
// System.out.println(map.get(key));
System.out.print(key+" ,"+"\t"+map.get(key));
//System.out.println("key");
//System.out.print(key+",");
//System.out.println("\t");
//System.out.println("value");
//System.out.print(map.get(key)+",");
}
}
}