简体   繁体   English

将数组映射到int Java

[英]Mapping an array to int Java

I am trying to map and array of ints to an int value. 我试图将int和int数组映射到int值。 I know that int[] wont work as keys. 我知道int []不会作为键。 I have tried List however that doesn't work as well. 我已经尝试过List,但是效果并不好。 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 . 您的问题是您对Map所有键使用单个ArrayList实例。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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