JAVA43. map简单应用
描述
现在有一个map集合如下:输入描述
String类型人名输出描述
先将题中给定的集合遍历输出,完成题中要求之后再遍历输出一次(输出格式为key+":"+value,第一次输出和第二次输出用空行隔开)示例1
输入:
David
输出:
1:Amy 2:Joe 3:Tom 4:Susan 1:Amy 2:Joe 3:Tommy 5:David
Go 解法, 执行用时: 4ms, 内存消耗: 860KB, 提交时间: 2022-05-23
package main import ( "fmt" "sort" ) func main() { map1 := make(map[int]string) map1[1] = "Amy" map1[2] = "Joe" map1[3] = "Tom" map1[4] = "Susan" //1.遍历集合,并将序号与对应人名打印。 var key []int for k, _ := range map1 { key = append(key, k) } sort.Ints(key) for _, k := range key { fmt.Printf("%v:%v\n", k, map1[k]) } fmt.Println() //2.向该map集合中插入一个编码为5姓名为控制台输入的人名的信息 var s string fmt.Scan(&s) map1[5] = s //3.移除该map中的编号为4的信息 delete(map1, 4) //4.将map集合中编号为3的姓名信息修改为"Tommy" map1[3] = "Tommy" //5.再次遍历经过上述操作后的集合,并将序号与对应人名打印。(注:第一次输出和第二次输出需用空行隔开) var key1 []int for k, _ := range map1 { key1 = append(key1, k) } sort.Ints(key1) for _, k := range key1 { fmt.Printf("%v:%v\n", k, map1[k]) } }
Java 解法, 执行用时: 24ms, 内存消耗: 10572KB, 提交时间: 2022-02-09
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name = scanner.next(); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "Amy"); map.put(2, "Joe"); map.put(3, "Tom"); map.put(4, "Susan"); //write your code here...... for(int i=1;i<=map.size();i++){ System.out.println(i+":"+map.get(i)); } System.out.println(); map.put(5,name); map.remove(4); map.replace(3,"Tommy"); for(Integer key: map.keySet()){ System.out.println(key+":"+map.get(key)); } } }
Java 解法, 执行用时: 26ms, 内存消耗: 10536KB, 提交时间: 2021-11-16
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name = scanner.next(); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "Amy"); map.put(2, "Joe"); map.put(3, "Tom"); map.put(4, "Susan"); //write your code here...... for(Integer key:map.keySet()){ System.out.println(key+":"+map.get(key)); } map.put(5,name); map.remove(4); map.replace(3,"Tommy"); System.out.println(); for(Integer key:map.keySet()){ System.out.println(key+":"+map.get(key)); } } }
Java 解法, 执行用时: 26ms, 内存消耗: 10604KB, 提交时间: 2021-12-17
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name = scanner.next(); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "Amy"); map.put(2, "Joe"); map.put(3, "Tom"); map.put(4, "Susan"); //write your code here...... for(Integer key:map.keySet()){ System.out.println(key+":"+map.get(key)); } //插入5号 map.put(5,name); //移除4号 map.remove(4); //替换3号 map.replace(3,"Tommy"); //打印空行 System.out.println(); //遍历map,打印序号和人名 for(Integer key:map.keySet()){ System.out.println(key+":"+map.get(key)); } } }
Java 解法, 执行用时: 26ms, 内存消耗: 10636KB, 提交时间: 2021-12-03
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name = scanner.next(); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "Amy"); map.put(2, "Joe"); map.put(3, "Tom"); map.put(4, "Susan"); //write your code here...... for(int key :map.keySet()){ System.out.println(key+":"+map.get(key)); } map.put(5,name); map.remove(4); map.replace(3,"Tommy"); System.out.println(); for(int key :map.keySet()){ System.out.println(key+":"+map.get(key)); } } }