我想打印一个嵌套的HashMap:
HashMap<Integer,HashMap<Character,Integer>> map;
我搜索了很多,但我找不到打印Integer的方法,因为当我在它上面使用getValues()时,它告诉我:“找不到符号”。 (因为它是一个整数值)
这就是我试图做的事情:
public void print(){
for(Map.Entry<Integer, HashMap<Character,Integer>> t :this.map.entrySet()){
Integer key = t.getKey();
for (Map.Entry<Character,Integer> e : this.map.getValue().entrySet())
System.out.println("OuterKey:" + key + " InnerKey: " + e.getKey()+ " VALUE:" +e.getValue());
}
}
我不能在我的第二个中使用getValue(),那么我还能使用什么呢?
提前致谢! 祝你今天愉快。 克里斯。
答案 0 :(得分:6)
getValue()
是Map.Entry
的方法,而不是Map
。
您应该在第二个循环中使用t.getValue().entrySet()
而不是this.map.getValue().entrySet()
。
那会给你内在的地图。
public void print(){
for(Map.Entry<Integer, HashMap<Character,Integer>> t :this.map.entrySet()){
Integer key = t.getKey();
for (Map.Entry<Character,Integer> e : t.getValue().entrySet())
System.out.println("OuterKey:" + key + " InnerKey: " + e.getKey()+ " VALUE:" +e.getValue());
}
}
答案 1 :(得分:0)
打印整件事的最简单方法:
System.out.println(map.toString());
是的,就是这样; toString()返回一个字符串...,其中包含您Map的所有内容;包括其内部地图!
如果你想自己做,那么你可以使用两个循环:
for(Map.Entry<Integer, HashMap<Character,Integer>> innerMap : map.entrySet()) {
for (Map.Entry<Character, Integer> aMap : innerMap.entrySet) {
... now you can call aMap.getKey() and .getValue()
答案 2 :(得分:0)
#import "ViewController.h"
@interface ViewController ()
// Create IBOutlet for the progressView's width constraint
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *progressViewWidthContraint;
// Declare a timer
@property (nonatomic, strong) NSTimer *progressTimer;
@property (nonatomic, assign) CGFloat progressPerSecond;
@end
@implementation ViewController
// Progress time can be changed on demand
NSInteger kProgressTime = 60;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Lets setup how much should the progress be increased by a second
self.progressPerSecond = self.view.frame.size.width / kProgressTime;
// Lets start showing the progress
[self startOrResetTimer];
}
- (void)startOrResetTimer {
// Just in case invalidate the timer before we would set it up
[self.progressTimer invalidate];
// Set the progressView's constant to its initial phase
self.progressViewWidthContraint.constant = 0;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
// Create the timer
self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgress) userInfo:nil repeats:true];
}
- (void)updateProgress {
// If the progress has reached the end of the screen, do not increase the constraint's constant
if (self.progressViewWidthContraint.constant == self.view.frame.size.width) {
return;
}
// Increase the constant by the previously calculated progress per second
self.progressViewWidthContraint.constant += self.progressPerSecond;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}
// Hook this method to your reset button on the interface builder, and any time you press the button, it will reset the timer and the progressbar
- (IBAction)didTapResetTimerButton:(UIButton *)sender {
[self startOrResetTimer];
}
@end
希望它有效。