java 打印哈希表的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16203473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:12:32  来源:igfitidea点击:

Printing contents of a hashtable

javahashtableenumeration

提问by d199224

I'm having some trouble printing the contents of a hashtable, it seems to just be printing the reference out.

我在打印哈希表的内容时遇到了一些麻烦,它似乎只是将引用打印出来。

add to hashtable code:

添加到哈希表代码:

protected Hashtable items = new Hashtable();

public void addItem(String itemId, String category, String title, String imageurl, double price, int quantity) {
  //boolean test = false;
  String price1 = String.valueOf(price);
  String[] item = {itemId, imageurl, title, category, price1, Integer.toString(quantity)};

if (items.containsKey(itemId)) {

  String[] tmpItem = (String[])items.get(itemId);
  int tmpQuant = Integer.parseInt(tmpItem[5]);
  quantity += tmpQuant;
  tmpItem[5] = Integer.toString(quantity);
}
else {

  items.put(itemId, item);
}
}

view contents code:

查看内容代码:

public void getContents()
 {
  Enumeration e = items.elements();

  while (e.hasMoreElements())
  {
      System.out.println(e.nextElement());
  }
 }

results:

结果:

[Ljava.lang.String;@3736e10e
[Ljava.lang.String;@73f17a73
[Ljava.lang.String;@729e4f7c

Anyone got any ideas?

有人有任何想法吗?

Thanks

谢谢

回答by rgettman

You've printed the array objects themselves, not the contents of the arrays. Try Arrays.toString

您已经打印了数组对象本身,而不是数组的内容。尝试Arrays.toString

System.out.println(Arrays.toString((String[]) e.nextElement()));