我在我的java程序中使用了treemaps。该程序的输出是一个排序后的映射,如下所示
[cat=1, dog=1, pin=3, ball=4, mice=4..... and so on]我想只显示地图的前3个值,而不是显示整个地图。我希望我的输出如下
cat=1
dog=1
pin=3我该怎么做呢?
发布于 2012-09-10 11:10:54
添加一个count变量,将其设置为0,并在达到3时中断循环:
int count = 0;
for (/* the loop that goes through all elements of the tree map...*/) {
// Do your printing...
count++;
if (count == 3) break;
}我特意发布了一个简单的解决方案,假设这是一个家庭作业。
https://stackoverflow.com/questions/12344926
复制相似问题