Et bien voilà. Javapolis est terminé. Fini.
Un grand merci à Stephan Janssen, le fondateur de cet événement, et toute son équipe pour cet excellent événement.
Pour terminer en beauté, sachez que Joshua Bloch vous recommande des lubrifiants en polygène.
(j'espère que j'ai traduit correctement)
Bien évidemment ceci n'est pas sérieux. C'est l'un des Puzzles que Joshua Bloch co-présentait avec Neal Gafter jeudi en fin d'après midi. Mais celui-là m'a bien fait rire.
Voici un bout de son code qu'il nous proposait :
public class Histogram {
private static final String[] words =
{ "I", "recommend", "polygene", "lubricants"};
public static void main(String[] args) {
int[] histogram = new int[5];
for (String word1 : words) {
for (String word2 : words) {
String pair = word1+word2;
int bucket = Math.abs(pair.hashCode()) % histogram.length;
histogram[bucket]++;
}
}
//Suis plus trop sûr du code ci-dessous, mais le résultat est le même
int total = 0;
for (int i : histogram) {
total+=i;
}
System.out.println(total);
}
}
Et la question était de ce style :
Quel est le résultat ?
4, 16, change tout le temps, erreur à l'exécution
J'attends vos réponses, et les explications qui vont avec (que ceux qui connaissent la réponse parce qu'ils ont assisté à la session laisse la parole aux autres. Merci)
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.public class Histogram {
private static final String[] words = { "I", "recommend", "polygene", "lubricants"};
public static void main(String[] args) {
int[] histogram = new int[5];
for (String word1 : words) {
for (String word2 : words) {
String pair = word1+word2;
int hashcode = Math.abs(pair.hashCode());
if(hashcode == Integer.MIN_VALUE) {
hashcode = Integer.MAX_VALUE;
}
int bucket = hashcode % histogram.length;
System.out.println("bucket = " + bucket);
histogram[bucket]++;
}
}
int total = 0;
for (int i : histogram) {
total+=i;
}
System.out.println(total);
}
}
• Or (pair.hashCode() >>> 1) % histogram.length
• Or (pair.hashCode() & 0x7fffffff) % histogram.length
• Or use power-of-two length array
(pair.hashCode() & (histogram.length – 1))
La solution proposée par le yam's n'est pas possible car elle modifie le hascode de pair et eronne donc la valeur de bucket servant à stocker l'information dans l'histogram.
Vous devez être identifié pour poster un commentaire.

Vincent Brabant
| Lun | Mar | Mer | Jeu | Ven | Sam | Dim |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Copyright © 2000-2012 - www.developpez.com