简体   繁体   中英

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();
    }
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM