gpt4 book ai didi

java - NavigableMap : java. lang.IllegalArgumentException:键超出范围

转载 作者:行者123 更新时间:2023-12-01 07:27:46 25 4
gpt4 key购买 nike

您能解释一下这段代码吗?

TreeMap<String, Integer> myMap = new TreeMap<String, Integer>();
myMap.put("a", 50);
myMap.put("b", 60);
myMap.put("c", 70);

NavigableMap<String, Integer> myMap2 = myMap.headMap("d", true);
myMap.put("e", 90);
myMap2.put("f", 100);//exception here
System.out.println(myMap.size() + " " + myMap2.size());

输出消息:

java.lang.IllegalArgumentException: key out of range
at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1407)
at SomeClass.main(SomeClass.java:13)

我不明白为什么我有它。

最佳答案

来自JavaDoc on TreeMap.headMap :

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

关于java - NavigableMap : java. lang.IllegalArgumentException:键超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21498112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com