Précédent   Forum du club des développeurs et IT Pro > Java > Général Java > Langage
Langage Forum d'entraide sur le langage Java et autres langages pour la JVM : syntaxe, POO, conventions, API standard. Avant de poster -> FAQ Java
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 11/12/2012, 07h17   #1
grabriel
Membre chevronné
 
Avatar de grabriel
 
Inscription : septembre 2006
Messages : 936
Détails du profil
Informations forums :
Inscription : septembre 2006
Messages : 936
Points : 617
Points : 617
Par défaut Arrays.binarySearch ne trouve pas mon B

Bonjour,

J'ai un comportement qui me semble un peu étrange, mais peut être est-il normal, si vous pensez que oui, merci de m'éclairer sur ce phénomène.

J'ai le code suivant :

Code :
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
32
33
34
35
36
import java.util.Arrays;
 
public class Image {
 
	public static void main(String[] args) {
		String[] screen = new String[] {"DCBA"};
		String dithered = "ACBD";
 
		Image id = new Image();
		System.out.println(id.count(dithered, screen));
	}
 
	public int count(String dithered, String[] screen) {
		int result = 0;
		int dithSize = dithered.length();
		char[] ditheredChar = new char[dithSize];
		for (int i = 0; i < dithSize; i++) {
			ditheredChar[i] = dithered.charAt(i);
		}
 
		for (String line : screen) {
			int screenSize = line.length();
 
			for (int i = 0; i < screenSize; i++) {
				char letter = line.charAt(i);
				int toto = Arrays.binarySearch(ditheredChar, letter);
//Pour B toto affiche -2 alors que B est bien présent dans ma liste
				if (toto >= 0) {
					result++;
				}
			}
		}
 
		return result;
	}
}

Le programme devrai afficher en résultat 4 et il affiche 3 le problème se situe à la ligne commentée (~27).
En faisant le débug j'ai bien la lettre B qui apparaît dans mon tableau "ditheredChar" mais toto prends la valeur -2 au lieu de 2 et en regardant dans la javadoc ici http://docs.oracle.com/javase/6/docs...arySearch(char[],%20char)

Citation:
Note that this guarantees that the return value will be >= 0 if and only if the key is found.
Ce que je ne comprends pas c'est que la lettre B est bien présente et pour les trois autres le résultat de toto est correcte.

Merci pour votre aide.
grabriel est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/12/2012, 09h13   #2
tchize_
Expert Confirmé Sénior
 
Avatar de tchize_
 
Homme
Responsable de service informatique
Inscription : avril 2007
Messages : 18 278
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 33
Localisation : Belgique

Informations professionnelles :
Activité : Responsable de service informatique
Secteur : Service public

Informations forums :
Inscription : avril 2007
Messages : 18 278
Points : 32 754
Points : 32 754
Envoyer un message via MSN à tchize_ Envoyer un message via Skype™ à tchize_
Arrays.binarySearch cherche dans une liste triée. Hors la liste que tu lui passe n'est pas triée.

Voilà ton code corrrigé:

Code :
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
32
33
import java.util.Arrays;
 
public class Image {
 
	public static void main(String[] args) {
		String[] screen = new String[] {"DCBA"};
		String dithered = "ACBD";
 
		Image id = new Image();
		System.out.println(id.count(dithered, screen));
	}
 
	public int count(String dithered, String[] screen) {
		int result = 0;
		int dithSize = dithered.length();
		char[] ditheredChar = dithered.toCharArray();
                      Arrays.sort(ditheredChar);
 
		for (String line : screen) {
			int screenSize = line.length();
                                 char[] lineChars = line.toCharArray();
			for (char letter : lineChar) {
				int toto = Arrays.binarySearch(ditheredChar, letter);
//Pour B toto affiche -2 alors que B est bien présent dans ma liste
				if (toto >= 0) {
					result++;
				}
			}
		}
 
		return result;
	}
}
__________________
⥀⥁ Чиз faq java, cours java, javadoc. Pensez à et
Laisse entrer le jour après une nuit sombre. Si tu es toujours là, tu n'es pas faite pour mourir.
tchize_ est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 03h02.


 
 
 
 
Partenaires

Hébergement Web