2D Array Creation and Access · Criação e Acesso de Array 2D
| English | Português |
|---|---|
| 2-D array/tuː diː əˈreɪ/ | array 2-D |
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.
Uma grade de valores
- Um array 2-D 二维数组 armazena valores em uma grade de linhas e colunas.
- Declare com colchetes duplos:
int[][] grid = new int[3][4];(3 linhas, 4 colunas). - Ou um inicializador:
int[][] g = {{1, 2}, {3, 4}, {5, 6}};(3 linhas de 2). - Pense em planilha, tabuleiro de jogo ou imagem de pixels.
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.
Linha depois coluna
- Acesse um elemento com dois índices:
grid[row][col]. grid[0][0]é o canto superior esquerdo;grid[1][2]é linha1, coluna2.- Linha vem primeiro, depois coluna — algo comum de inverter.
- Ambos os índices começam em
0, como todo array 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.
Contagens de linha e coluna
grid.lengthé o número de linhas.grid[0].lengthé o número de colunas (comprimento de uma linha).- Para uma grade retangular, todas as linhas têm a mesma contagem de colunas.
- Use esses dois comprimentos para delimitar loops sobre a grade.
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.
Dois limites para vigiar
- Um elemento válido é
grid[r][c]com0 <= r < grid.lengthe0 <= c < grid[0].length. - Passar do limite em qualquer índice lança ArrayIndexOutOfBoundsException.
- Misturar linha e coluna lê silenciosamente a célula errada (ou causa crash).
- Verifique a ordem de
[row][col]sempre.
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.
É grid[row][col] — linha primeiro, depois coluna — e os dois comprimentos diferem. grid.length conta linhas; grid[0].length conta colunas. Inverter os índices lê a célula errada ou lança uma exceção. Quando iterar, o índice externo vai até grid.length e o interno até 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.
Uma grade 3×2 g = {{1, 2}, {3, 4}, {5, 6}}:
g[0][0]é1;g[2][1]é6(linha 2, coluna 1).g.lengthé3(linhas);g[0].lengthé2(colunas).g[1][0]é3— linha1, coluna0.
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.
Um array 2-D é uma grade, criado com new type[rows][cols] ou um inicializador aninhado. Acesse um elemento como grid[row][col] (linha primeiro). grid.length é a contagem de linhas; grid[0].length é a contagem de colunas. Ambos os índices começam em 0, e passar do limite lança uma exceção.
grid[row][col] indexing · índice grid[row][col]
g[2][1] = 6; g.length = 3 rows, g[0].length = 2 columns. · g[2][1] = 6; g.length = 3 linhas, g[0].length = 2 colunas.
For int[][] g = {{1,2},{3,4},{5,6}}; what is g[2][1]? · Para int[][] g = {{1,2},{3,4},{5,6}}; qual é g[2][1]?
Row 2 is {5,6}; column 1 is 6. · Linha 2 é {5,6}; coluna 1 é 6.
In grid[row][col], which index comes first? · Em grid[row][col], qual índice vem primeiro?
Row first, then column. · Linha primeiro, depois coluna.
For a 3-row, 4-column grid, what does grid.length equal? · Para uma grade de 3 linhas e 4 colunas, quanto vale grid.length?
grid.length is the number of rows = 3. · grid.length é o número de linhas = 3.
For a 3-row, 4-column grid, what does grid[0].length equal? · Para uma grade de 3 linhas e 4 colunas, quanto vale grid[0].length?
grid[0].length is the number of columns = 4. · grid[0].length é o número de colunas = 4.
grid.length gives the number of columns. · grid.length dá o número de colunas.
grid.length is rows; grid[0].length is columns. · grid.length são linhas; grid[0].length são colunas.