Array Creation and Access · Création et accès aux tableaux
| English | Français |
|---|---|
| initializer list/ɪˈnɪʃəlaɪzə lɪst/ | liste d'initialisation |
| ArrayIndexOutOfBoundsException/əˈreɪ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ | ArrayIndexOutOfBoundsException |
Making an array
- Declare an array with a type and
[]:int[] scores; - Create it with
newand a size:int[] scores = new int[5];(five slots). - Or list the values directly:
int[] scores = {85, 90, 78};(an initializer list 初始化列表). - Every slot starts at the type's default (
0forint,nullfor objects).
Créer un tableau
- Déclare un array avec un type et
[]:int[] scores; - Créez-le avec
newet une taille :int[] scores = new int[5];(cinq emplacements). - Ou listez les valeurs directement :
int[] scores = {85, 90, 78};(une liste d'initialisation 初始化列表). - Chaque case commence à la valeur par défaut du type (
0pourint,nullpour les objects).
Indexing from zero
- Array elements are numbered from
0; the last index islength - 1. - Access a slot with
scores[i]:scores[0]is the first,scores[2]the third. - Assign with
scores[0] = 100;and read withint x = scores[0];. - Off-by-one on the last index is the most common array bug.
Indexation à partir de zéro
- Les éléments du tableau sont numérotés à partir de
0; le dernier index estlength - 1. - Accédez à une case avec
scores[i]:scores[0]est le premier,scores[2]le troisième. - Affecter avec
scores[0] = 100;et lire avecint x = scores[0];. - L'erreur « off-by-one » sur le dernier indice est l'erreur de tableau la plus fréquente.
The length field
- An array's size is its
lengthfield — no parentheses:scores.length. - (Contrast
String'slength()method, which has parentheses.) lengthis fixed once the array is created — arrays don't resize.- Use
scores.lengthin loop conditions to stay in bounds.
Le champ length
- La taille d'un tableau est son champ
length— sans parenthèses :scores.length. - (Contrast
String'slength()méthode, qui comprend des parenthèses.) lengthest fixe une fois le tableau créé — les tableaux ne changent pas de taille.- Utilisez
scores.lengthdans les conditions de boucle pour rester dans les limites valides.
Out of bounds
- A valid index is
0tolength - 1; anything else throws an ArrayIndexOutOfBoundsException 数组下标越界异常. scores[5]on a length-5 array is out of bounds (indices are0–4).- The loop condition
i < scores.lengthkeepsiin range. - Never use
<=withlength— it steps one past the end.
Hors limites
- Un indice valide va de
0àlength - 1; tout autre valeur provoque une ArrayIndexOutOfBoundsException数组下标越界异常. scores[5]sur un tableau de longueur 5 est hors limites (les indices sont0–4).- La condition de boucle
i < scores.lengthgardeidans les limites. - N'utilisez jamais
<=aveclength— cela dépasse d'une position la fin du tableau.
Array size is length (a field, NO parentheses); String length is length() (a method, WITH parentheses). Mixing them up won't compile. And valid indices are 0 to length - 1 — scores[scores.length] is one past the end and throws an ArrayIndexOutOfBoundsException. Loop with i < scores.length, never i <= scores.length.
La taille du tableau est length (un champ, PAS de parenthèses) ; String length est length() (une méthode, AVEC parenthèses). Les confondre empêchera la compilation. Et les indices valides vont de 0 à length - 1 — scores[scores.length] est une position après la fin et provoque une ArrayIndexOutOfBoundsException. Bouclez avec i < scores.length, jamais i <= scores.length.
Creating and accessing an array:
int[] a = {10, 20, 30};— length3, indices0,1,2.a[0]is10;a[2]is30;a.lengthis3.a[3]throws ArrayIndexOutOfBoundsException (no index 3).
Création et accès à un tableau :
int[] a = {10, 20, 30};— longueur3, indices0,1,2.a[0]est10;a[2]est30;a.lengthest3.a[3]provoque ArrayIndexOutOfBoundsException (pas d'indice 3).
Create an array with new type[size] or an initializer list {...}. Elements are indexed from 0 to length - 1, accessed as a[i]. The size is the length field (no parentheses). An index outside 0..length-1 throws an ArrayIndexOutOfBoundsException — loop with i < a.length.
Créez un tableau avec new type[size] ou une liste d'initialisation {...}. Les éléments sont indexés de 0 à length - 1, accédés comme a[i]. La taille est le champ length (pas de parenthèses). Un indice hors 0..length-1 provoque une ArrayIndexOutOfBoundsException — bouclez avec i < a.length.
Indexing an array from 0 · Indexation d'un tableau à partir de 0
a[0]=10, a[1]=20, a[2]=30; a.length is 3. · a[0]=10, a[1]=20, a[2]=30 ; a.length vaut 3.
For int[] a = {10, 20, 30}; what is a[0]? · Pour int[] a = {10, 20, 30} ; quelle est la valeur de a[0] ?
Arrays are indexed from 0, so a[0] is the first element, 10. · Les tableaux sont indexés à partir de 0, donc a[0] est le premier élément, 10.
How do you get the size of an array a? · Comment obtenir la taille du tableau a ?
Array size is the length FIELD (no parentheses). · La taille du tableau correspond au champ length (sans parenthèses).
For int[] a = {10, 20, 30}; what is a.length? · Pour int[] a = {10, 20, 30} ; quelle est la valeur de a.length ?
Three elements → length 3. · Trois éléments → length vaut 3.
For a length-5 array, accessing index 5 (a[5])... · Pour un tableau de longueur 5, accéder à l'index 5 (a[5])...
Valid indices are 0..4; index 5 is out of bounds. · Les indices valides sont 0..4 ; l'index 5 est hors limites.
String uses length() (a method) while an array uses length (a field). · String utilise length() (une méthode) tandis qu'un tableau utilise length (un champ).
Different syntax — parentheses for String, none for arrays. · Syntaxe différente — parenthèses pour String, aucune pour les tableaux.