0
    for (Entry<String, Data> entry : list.entrySet()) {
        if(entry.getValue().getRoom() == 1){
            if(entry.getValue().getName().equalsIgnoreCase("RED")){
                 entry.getValue().getPosition() // need to get the lowest free number
                 // between the range of 1-6
            } 
        }
    }

在这种情况下如何获取 getPosition 的最低空闲位置。getPosition 值介于 1-6 之间,每个值 Room = 1 和 Name = RED 中只有一个。例如,如果 1,3,4,6 存在于 getPosition(with room=1 and name=red) 那么输出应该是 2。这是特定组合中 getPosition 中空闲的最小数字。希望你能帮助我。

4

1 回答 1

4

好吧,听起来最简单的方法是:

boolean[] taken = new boolean[7]; //(0-6 inclusive)
// You were never using the key as far as I could see...
for (Data data : list.values()) {
   if (data.getRoom() == 1 && data.getName().equalsIgnoreCase("RED")) {
       taken[data.getPosition()] = true;
   }
}

for (int i = 1; i <= 6; i++) {
    if (!taken[i]) {
        return i;
    }
}

// Do whatever you want if there are no free positions...
于 2012-09-21T22:36:57.957 回答