以下程序的运行结果是()import java.util.*;public class TreeMapExample { public static void main(String[] args){ SortedMap map = new TreeMap();//创建TreeMap实例对象 map.put(3,"John"); map.put(2, "Tome"); map.put(1,"Todd"); map.put(5,"Jane"); map.put(4, "William");//新添加一个元素,sortedMap将会自动排序 for(Object k : map.keySet()){//以for-each语句对sortedMap集合进行遍历 System.out.println(k + ":" + map.get(k)); } }}
A: 编译错误
B: 运行时异常
C: 1:Todd
A: 编译错误
B: 运行时异常
C: 1:Todd
举一反三
- 以下程序的运行结果是()import java.util.*;public class TreeMapExample { public static void main(String[] args){ SortedMap map = new TreeMap();//创建TreeMap实例对象 map.put(3,"John"); map.put(2, "Tome"); map.put(1,"Todd"); map.put(5,"Jane"); map.put(4, "William");//新添加一个元素,sortedMap将会自动排序 for(Object k : map.keySet()){//以for-each语句对sortedMap集合进行遍历 System.out.println(k + ":" + map.get(k)); } }} A: 编译错误 B: 运行时异常 C: 1:Todd
- 编译运行以下程序的结果是:import java.util.TreeSet;public class Test02 {public static void main(String[] args) {Map map = new TreeMap(); // 创建TreeMap实例对象map.put(3, "John");map.put(2, "Tome");map.put(1, "Todd");map.put(5, "Jane");map.put(4, "William");// 新添加一个元素,sortedMap将会自动排序for (Object k : map.keySet()) {// 以for-each语句对sortedMap集合进行遍历System.out.println(k + ":" + map.get(k));}}} A: 编译错误 B: 运行时异常 C: 1:Todd2:Tome3:John4:William5:Jane
- 判断下列程序是否能正常运行,如果能,写出运行结果,如果不能,写出错误原因并进行纠正。import java.util.*;import java.util.Map.*;public class Demo {public static void main(String[] args) {Map map = new HashMap();map.put(1, "Tom");map.put(2, "Lucy");map.put(3,"Annie");Set keySet = map.keySet();Iterator it = keySet.iterator();while (it.hasNext()) {Object key = it.next();System.out.println(key);map.remove(key);}}}
- 阅读下面的Java代码,运行结果描述正确的是( )。import java.util.*;public class TestMap{public statci void main(String[] args){Map map=new HashMap( ); map.put(“first”,”football”); map.put(“first”,”basketball”); System.out.print(map.get(“first”));}} A: 编译时发生错误 B: 编译通过,运行时发生错误 C: 正确运行,显示basketball D: 正确运行,显示football
- 给定如下Java代码,编译运行的结果是( )。public class Test { public static void main(String[] args) { Map[String, String] map = new HashMap[String, String](); String s = "code"; map.put(s, "1"); map.put(s, "2"); System.out.println(map.size()); }} A: 编译时发生错误 B: 运行时引发异常 C: 正确运行,输出:1 D: 正确运行,输出:2