TreeMap 键值到字符串数组
我正在尝试将 Treemap 中的键值添加到 String[],但我做错了一些事情,并且得到以下信息:“[Ljava.lang.String;@281ec58a”。有人可以帮忙吗? 提前致谢。
这是我正在使用的代码:
TreeMap t = new TreeMap(hm); //hm is a Hashmap
t = (TreeMap) sortByValues(t); // i sort the values with this method
String [] tempa = (String[]) t.keySet().toArray(new String[t.size()]);
I am trying to add the key values from a Treemap to a String[], but i am doing something wrong and i get this: "[Ljava.lang.String;@281ec58a". Can anyone help?
Thanks in advance.
This is the code i'm using:
TreeMap t = new TreeMap(hm); //hm is a Hashmap
t = (TreeMap) sortByValues(t); // i sort the values with this method
String [] tempa = (String[]) t.keySet().toArray(new String[t.size()]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“[Ljava.lang.String;@281ec58a””
就是默认情况下打印数组(即转换为字符串)的方式。如果您迭代数组的内容,并依次打印每个元素,您可能会发现一切都符合预期。"[Ljava.lang.String;@281ec58a"
is simply how arrays are printed (i.e. converted to a string) by default. If you iterate over the contents of the array, and print each element in turn, you'll likely find that everything is as expected.完全不清楚
HashMap
和TreeMap
中的值是什么,因为您使用的是原始类型。如果您使用泛型,您的代码对我们、您和编译器来说都会更清晰。然而,很可能一切都很好 - 只是您通过调用
toString
将字符串数组转换为字符串。 (您还没有告诉我们“我明白了”是什么意思 - 在哪里?)尝试使用:
来查看数组中的值是什么。
It's entirely unclear what the values within the
HashMap
andTreeMap
are, because you're using the raw types. Your code would be clearer to us, you and the compiler if you used generics.However, it could well be that everything is fine - it's just that you're converting the string array to a string somewhere by calling
toString
on it. (You haven't told us what you mean by "i get this" - where?)Try using:
to see what the values within the array are.