Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping an array to int Java

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();
    }
}
like image 882
Kenadams Avatar asked Jul 15 '26 12:07

Kenadams


1 Answers

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);
}
like image 179
Eran Avatar answered Jul 18 '26 01:07

Eran