2D Array Creation and Access · Création et accès aux tableaux 2D
| English | Français |
|---|---|
| 2-D array/tuː diː əˈreɪ/ | tableau 2D |
A grid of values
- A 2-D array 二维数组 stores values in a grid of rows and columns.
- Declare with two brackets:
int[][] grid = new int[3][4];(3 rows, 4 columns). - Or an initializer:
int[][] g = {{1, 2}, {3, 4}, {5, 6}};(3 rows of 2). - Think spreadsheet, game board, or pixel image.
Une grille de valeurs
- Un tableau 2-D 二维数组 stocke des valeurs dans une grille de lignes et colonnes.
- Déclare avec deux crochets :
int[][] grid = new int[3][4];(3 lignes, 4 colonnes). - Ou avec un initialiseur :
int[][] g = {{1, 2}, {3, 4}, {5, 6}};(3 lignes de 2). - Pensez tableur, plateau de jeu ou image pixelisée.
Row then column
- Access an element with two indices:
grid[row][col]. grid[0][0]is the top-left;grid[1][2]is row1, column2.- Row comes first, then column — a common thing to get backwards.
- Both indices start at
0, like every Java array.
Ligne puis colonne
- Accédez à un élément avec deux indices :
grid[row][col]. grid[0][0]est le coin supérieur gauche ;grid[1][2]est la ligne1, colonne2.- La ligne vient en premier, puis la colonne — chose courante à inverser.
- Les deux indices commencent à
0, comme tout tableau Java.
Row and column counts
grid.lengthis the number of rows.grid[0].lengthis the number of columns (length of one row).- For a rectangular grid, every row has the same column count.
- Use these two lengths to bound loops over the grid.
Comptages de lignes et colonnes
grid.lengthest le nombre de lignes.grid[0].lengthest le nombre de colonnes (longueur d'une ligne).- Pour une grille rectangulaire, chaque ligne a le même nombre de colonnes.
- Utilisez ces deux longueurs pour délimiter les boucles sur la grille.
Two bounds to watch
- A valid element is
grid[r][c]with0 <= r < grid.lengthand0 <= c < grid[0].length. - Going out of range on either index throws ArrayIndexOutOfBoundsException.
- Mixing up row and column silently reads the wrong cell (or crashes).
- Double-check
[row][col]order every time.
Deux bornes à surveiller
- Un élément valide est
grid[r][c]avec0 <= r < grid.lengthet0 <= c < grid[0].length. - Sortir des limites sur l'un ou l'autre indice lance ArrayIndexOutOfBoundsException.
- Inverser ligne et colonne lit silencieusement la mauvaise cellule (ou plante).
- Vérifiez l'ordre
[row][col]à chaque fois.
It's grid[row][col] — row first, then column — and the two lengths differ. grid.length counts rows; grid[0].length counts columns. Swapping the indices reads the wrong cell or throws an exception. When you loop, the outer index goes to grid.length and the inner to grid[0].length.
C'est grid[row][col] — ligne d'abord, puis colonne — et les deux longueurs diffèrent. grid.length compte les lignes ; grid[0].length compte les colonnes. Inverser les indices lit la mauvaise cellule ou lance une exception. Quand vous bouclez, l'indice externe va jusqu'à grid.length et l'interne jusqu'à grid[0].length.
A 3×2 grid g = {{1, 2}, {3, 4}, {5, 6}}:
g[0][0]is1;g[2][1]is6(row 2, column 1).g.lengthis3(rows);g[0].lengthis2(columns).g[1][0]is3— row1, column0.
Une grille 3×2 g = {{1, 2}, {3, 4}, {5, 6}} :
g[0][0]est1;g[2][1]est6(ligne 2, colonne 1).g.lengthest3(lignes) ;g[0].lengthest2(colonnes).g[1][0]est3— ligne1, colonne0.
A 2-D array is a grid, created with new type[rows][cols] or a nested initializer. Access an element as grid[row][col] (row first). grid.length is the row count; grid[0].length is the column count. Both indices start at 0, and going out of range throws an exception.
Un tableau 2-D est une grille, créé avec new type[rows][cols] ou un initialiseur imbriqué. Accédez à un élément comme grid[row][col] (ligne d'abord). grid.length est le nombre de lignes ; grid[0].length est le nombre de colonnes. Les deux indices commencent à 0, et sortir des limites lance une exception.
grid[row][col] indexing · indexation grid[row][col]
g[2][1] = 6; g.length = 3 rows, g[0].length = 2 columns. · g[2][1] = 6 ; g.length = 3 lignes, g[0].length = 2 colonnes.
For int[][] g = {{1,2},{3,4},{5,6}}; what is g[2][1]? · Pour int[][] g = {{1,2},{3,4},{5,6}} ; que vaut g[2][1] ?
Row 2 is {5,6}; column 1 is 6. · La ligne 2 est {5,6} ; la colonne 1 est 6.
In grid[row][col], which index comes first? · Dans grid[row][col], quel index vient en premier ?
Row first, then column. · Ligne d'abord, puis colonne.
For a 3-row, 4-column grid, what does grid.length equal? · Pour une grille de 3 lignes et 4 colonnes, que vaut grid.length ?
grid.length is the number of rows = 3. · grid.length est le nombre de lignes = 3.
For a 3-row, 4-column grid, what does grid[0].length equal? · Pour une grille de 3 lignes et 4 colonnes, que vaut grid[0].length ?
grid[0].length is the number of columns = 4. · grid[0].length est le nombre de colonnes = 4.
grid.length gives the number of columns. · grid.length donne le nombre de colonnes.
grid.length is rows; grid[0].length is columns. · grid.length est les lignes ; grid[0].length est les colonnes.