【发布时间】:2019-07-18 10:06:45
【问题描述】:
有人可以帮我用 Java 编写这段代码吗?我还是个初学者,在 Lynda.com 上看到了一门关于算法的课程。但是,该课程是基于python的。非常感谢任何帮助。
# using a hashtable to count individual items
# define a set of items that we want to count
items = ["apple", "pear", "orange", "banana", "apple",
"orange", "apple", "pear", "banana", "orange",
"apple", "kiwi", "pear", "apple", "orange"]
# create a hashtable object to hold the items and counts
counter = dict()
# iterate over each item and increment the count for each one
for item in items:
if item in counter.keys():
counter[item] += 1
else:
counter[item] = 1
# print the results
print(counter)
【问题讨论】:
-
你试过自己翻译成Java吗?
-
这不是代码转换网站。但这些可能会有所帮助:
items是List,counter是Map,我希望您已经了解for、if else和print语句。