Implementing String Algorithms · Implémentation d'algorithmes de chaînes
| English | Français |
|---|---|
| StringIndexOutOfBoundsException/strɪŋ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ | StringIndexOutOfBoundsException |
Looping over characters
- To process a String, loop over its indices from
0tolength() - 1. for (int i = 0; i < s.length(); i++) { ... s.substring(i, i+1) ... }s.substring(i, i+1)gives the one-character String at indexi.- This visits every character in order — the basis of most String algorithms.
Itérer sur des caractères
- Pour traiter une chaîne, bouclez sur ses indices de
0àlength() - 1. for (int i = 0; i < s.length(); i++) { ... s.substring(i, i+1) ... }s.substring(i, i+1)donne la chaîne d'un caractère à l'indexi.- Cela visite chaque caractère dans l'ordre — c'est la base de la plupart des algorithmes sur chaînes.
Counting characters
- Combine the loop with an
ifto count matching characters. - "Count the vowels": loop, and
ifthe current character is a vowel,count++. - The accumulator pattern from 2.9 applies directly to Strings.
- Compare characters with
.equals(), not==.
Compter des caractères
- Combinez la boucle avec une
ifpour compter les caractères correspondants. - « Compter les voyelles » : boucle, et si
ifle caractère actuel est une voyelle,count++. - Le modèle accumulateur de 2.9 s'applique directement aux chaînes.
- Comparez les caractères avec
.equals(), pas avec==.
Building a new String
- To transform text, build up a new String with
+, since Strings are immutable. String result = "";thenresult = result + something;inside the loop.- "Reverse a String": append each character from the end toward the start.
- The original never changes — you accumulate a fresh String.
Construire une nouvelle chaîne
- Pour transformer du texte, construisez une nouvelle chaîne avec
+, car les chaînes sont immuables. String result = "";puisresult = result + something;à l'intérieur de la boucle.- « Inverser une chaîne » : ajoutez chaque caractère de la fin vers le début.
- L'originale ne change jamais — vous accumulez une chaîne fraîche.
Index bounds
- Valid indices run
0tolength() - 1; going outside throws a StringIndexOutOfBoundsException 越界异常. s.substring(i, i+1)is safe only whilei < s.length().- The loop condition
i < s.length()keepsiin range. - Off-by-one bounds are the most common String-loop bug.
Bornes d'index
- Les indices valides vont de
0àlength() - 1; aller au-delà lance une StringIndexOutOfBoundsException 越界异常. s.substring(i, i+1)est sûr uniquement tant quei < s.length().- La condition de boucle
i < s.length()gardeidans les limites. - Les erreurs de bornes d'un seul sont le bug de boucle sur chaîne le plus courant.
Loop String indices with i < s.length(), not i <= s.length(). The last valid index is length() - 1; touching index length() throws a StringIndexOutOfBoundsException. And build transformed text by accumulating a new String (result += ch) — you can't edit a String in place, because Strings are immutable.
Itérez sur les indices de chaîne avec i < s.length(), pas i <= s.length(). Le dernier index valide est length() - 1 ; toucher l'index length() lance une StringIndexOutOfBoundsException. Et transformez le texte en accumulant une nouvelle chaîne (result += ch) — vous ne pouvez pas modifier une chaîne in situ, car les chaînes sont immuables.
Counting vowels in s:
int count = 0;for (int i = 0; i < s.length(); i++) { String ch = s.substring(i, i+1); if ("aeiou".indexOf(ch) >= 0) count++; }- Visits each character; counts it if it appears in
"aeiou".
Compter les voyelles dans s :
int count = 0;for (int i = 0; i < s.length(); i++) { String ch = s.substring(i, i+1); if ("aeiou".indexOf(ch) >= 0) count++; }- Visite chaque caractère ; le compte s'il apparaît dans
"aeiou".
Process a String by looping its indices 0 to length()-1 (condition i < s.length()), reading each character with s.substring(i, i+1). Count with an if, or build a new String by accumulating with + (Strings are immutable). Staying in bounds avoids a StringIndexOutOfBoundsException.
Traitez une chaîne en bouclant sur ses indices 0 à length()-1 (condition i < s.length()), lisez chaque caractère avec s.substring(i, i+1). Comptez avec une if, ou construisez une nouvelle chaîne en accumulant avec + (les chaînes sont immuables). Rester dans les limites évite une StringIndexOutOfBoundsException.
Looping over the characters of HELLO · Parcourir les caractères de HELLO
A loop visits indices 0 to length()-1. · Une boucle visite les indices de 0 à length()-1.
The correct loop condition to visit every character of String s is... · La bonne condition de boucle pour visiter chaque caractère de String s est...
Valid indices are 0..length()-1, so i < s.length(). · Les indices valides sont 0..length()-1, donc i < s.length().
Accessing index s.length() of a String throws... · Accéder à l'indice s.length() d'une chaîne provoque...
The last valid index is length()-1. · Le dernier indice valide est length()-1.
To build a reversed String, since Strings are immutable, you... · Pour construire une chaîne inversée, étant donné que les chaînes sont immuables, vous...
Strings are immutable — build a fresh one. · Les chaînes sont immuables — construisez-en une neuve.
How many characters does a loop visit for the String "HELLO" (using i < length())? · Combien de caractères une boucle visite-t-elle pour la chaîne "HELLO" (en utilisant i < length()) ?
Indices 0..4 → 5 characters. · Indices 0..4 → 5 caractères.
The accumulator pattern from loops also applies to counting characters in a String. · Le motif de l'accumulateur des boucles s'applique également au comptage de caractères dans une chaîne.
count++ inside an index loop counts matching characters. · count++ à l'intérieur d'une boucle d'indices compte les caractères correspondants.