Ich habe den folgenden Code:Warum hat containsKey den Schlüssel nicht gefunden?
payoffs2exchanges.put(point, exchange);
if (!payoffs2exchanges.containsKey(point)) {
game.log.fine("yes");
} else {
game.log.fine("no");
}
Es gibt "Nein". Mit anderen Worten, ich füge das Schlüssel-Wert-Paar der Karte hinzu und überprüfe sofort danach, ob der Schlüssel existiert und finde heraus, dass er nicht existiert. Warum?
Ich habe immer noch das Problem mit dem Schlüssel. Der folgende Code besagt, dass jedes Mal, wenn ich einen Schlüssel hinzufüge, ein neuer Schlüssel hinzugefügt wird. Und ich weiß, dass das nicht der Fall ist.
Integer[] point = new Integer[2];
point[0] = proposerBestScore;
point[1] = responderBestScore;
game.log.fine("In the getCloudOfPayoffs: found payoffs:" + point[0] + "," + point[1] + ". Exchange: " + exchange[0]+","+exchange[1]+","+exchange[2]+","+exchange[3]+","+exchange[4]);
// With the following block we ensure that every options (pair of payoffs) is represented by exchange with minimal number of moves.
if (!payoffs2exchanges.containsKey(point)) {
payoffs2exchanges.put(point, exchange);
game.log.fine("In the getCloudOfPayoffs: this option is new. We add it to the map.");
} else {
game.log.fine("In the getCloudOfPayoffs: this option is old.");
Integer[] exchangeFromMap = payoffs2exchanges.get(point);
Integer newSum = 0;
Integer oldSum = 0;
for (int i=0;i<Design.nColors;i++) {
newSum = newSum + Math.abs(exchange[i]);
oldSum = oldSum + Math.abs(exchangeFromMap[i]);
}
if (newSum<oldSum) {
game.log.fine("In the getCloudOfPayoffs: the new exchange is better than the old one.");
payoffs2exchanges.put(point, exchange);
}
}
Welche Klasse ist 'Punkt'? Benutzerdefinierte Klasse? Welche Klasse ist "Auszahlungen2"? –
Eine Antwort zu Ihrem Update hinzugefügt. – aioobe
@aioobe, Entschuldigung für diese Manipulationen mit der Frage. Ich lege es zurück. Also, Sie antworten, ist nützlich. – Roman