I am trying to map and array of ints to an int value. I know that int[] wont work as keys. I have tried List however that doesn't work as well. Is there anyway I can do this? Thanks.
Here is my failed attempt:
private void createMap(){
List<Integer> state_action_pair = new ArrayList<Integer>();
for(int i=0;i<this.stateActionTable.length;i++){
for(int j=0;j<this.stateActionTable[0].length;j++){
state_action_pair.add(this.stateActionTable[i][j]);
}
this.stateActionMap.put(state_action_pair, i);
state_action_pair.clear();
}
}
Your problem is that you use a single ArrayList instance for all the keys of your Map. You need an individual instance for each key :
private void createMap(){
for(int i=0;i<this.stateActionTable.length;i++){
List<Integer> state_action_pair = new ArrayList<Integer>();
for(int j=0;j<this.stateActionTable[0].length;j++){
state_action_pair.add(this.stateActionTable[i][j]);
}
this.stateActionMap.put(state_action_pair, i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With