【发布时间】:2016-05-03 18:58:28
【问题描述】:
我正在做以下事情
String s = caseInsensitiveMap.get("buyerCode");
我得到了错误
java.lang.ClassCastException: [Ljava.lang.String; incompatible with java.lang.String
我只是无法弄清楚我做错了什么。谷歌搜索的答案似乎指向需要在某处使用String[],但我不知道在哪里。
更多相关信息:
caseInsensitiveMap:映射 caseInsensitiveMap - com.msw.commerce.me.commands.MSWOrgCmdImpl.setRequestProperties(TypedProperty)
.get() : String java.util.Map.get(对象键)
我也尝试过
String s = caseInsensitiveMap.get((Object) "buyerCode");
将字符串 "buyerCode" 显式转换为所需的 Object 类型,但我得到了同样的错误。
谁能告诉我我做错了什么?从我所见,我匹配了这里的所有类型。 .get() 接受一个对象,我给它一个对象。它返回一个字符串,我将它分配给一个字符串。
编辑:更多代码
public void setRequestProperties(TypedProperty reqProperties)
throws ECException {
Map<String, String> reqMap = reqProperties.getMap();
Map<String, String> caseInsensitiveMap = new TreeMap<String, String>(
String.CASE_INSENSITIVE_ORDER);
caseInsensitiveMap.putAll(reqMap);
这是TypedProperty 的文档
【问题讨论】:
-
向我们展示相关代码。 caseInsensitiveMap 如何声明、初始化和填充?
-
[Ljava.lang.String 的意思是 String[],换句话说,你得到的是一个字符串数组,这与单个字符串不同。也可以在这里查看:stackoverflow.com/questions/3442090/…
-
您的地图包含
String[]作为值,而不是您期望的String。地图通常可以包含任何类型的对象,在您的情况下,您在没有意识到的情况下插入了错误的类型。 -
根据错误消息,
caseInsensitiveMap.get()返回的是字符串数组,而不是字符串。没有您的代码,就无法进一步分析。 -
您在
Map<String, String> reqMap = reqProperties.getMap();这一行收到任何警告吗?
标签: java string object dictionary casting