0

I have the following code:

public class Interval {

    private HashMap intervals;

    public Interval() {
        this.intervals = new HashMap<String, List<Long>>();
    }

    public void addInterval(String name, long interval) {
        List<Long> foo = new ArrayList<Long>();
        foo.add(interval);
        foo.add(System.currentTimeMillis());
        this.intervals.put(name, foo);
    }

    public boolean isReady(String name) {
        if (System.currentTimeMillis() - (this.intervals.get(name)).get(0) > 
        (this.intervals.get(name)).get(1)) { //ERROR
            return true;
        }
        return false;
        this.intervals.get(name).set(1, System.currentTimeMillis()); //ERROR
    }
}

The code is supposed to store intervals (in milliseconds) by a key in a HashMap and have a method to see if the interval has passed yet (since the last call). It will be used as a cooldown for a player shooting in a game. Anyways, the code doesn't work because the value returned by intervals.get(String key) is an Object, and doesn't allow methods like "get" and "set" to be called on it like I would be able to on a ArrayList. Anyone know how to fix this?

P.S. The lines with the comment "ERROR" are the ones that give the error.

agillgilla
  • 859
  • 1
  • 7
  • 22

1 Answers1

1

Change your private field from

private HashMap intervals;

to

private HashMap<String, List<Long>> intervals;
DAXaholic
  • 33,312
  • 6
  • 76
  • 74