2-D arrays: working with a grid · 2次元配列:グリッドとの操作
A grid of values
- A normal array is a single row of values.
- A 2-D array is a grid: it has rows and columns, like a table or a spreadsheet.
- We use it for game boards, images, tables of marks, and more.
値のグリッド
- 通常の配列は、単一の 行 の値である。
- 2次元配列 は グリッド である:行 と 列 を持ち、表やスプレッドシートのような形をする。
- ゲーム盤、画像、得点表などに使用する。
Making a 2-D array
int[][] g = new int[3][4];makes a grid with 3 rows and 4 columns.- Every cell starts at
0(forint). - You can also fill it directly with values inside
{...}.
2次元配列の作成
int[][] g = new int[3][4];は 3行 と 4列 のグリッドを作成する。- すべてのセルは
0で初期化される(intのための初期値)。 {...}の中身で直接値を埋めることもできる。
public class Main {
public static void main(String[] args) {
int[][] g = new int[3][4]; // 3 rows, 4 columns, all 0
int[][] table = {
{1, 2, 3},
{4, 5, 6}
}; // 2 rows, 3 columns
System.out.println(table[0][2]); // 3
System.out.println(table[1][0]); // 4
}
}
Reading and writing one cell
- A cell needs two indexes:
g[row][col]. Row first, then column. - Both indexes start at 0.
g[1][2] = 9;stores9in row1, column2.
1つのセルの読み書き
- セルには 2つ のインデックスが必要である:
g[row][col]。行を先に、次に列。 - 両方のインデックスは 0 から始まる。
g[1][2] = 9;は9を行1、列2に格納する。
public class Main {
public static void main(String[] args) {
int[][] g = new int[2][2];
g[0][0] = 5;
g[0][1] = 7;
g[1][0] = 9;
g[1][1] = 11;
System.out.println(g[1][0]); // 9
g[1][0] = g[1][0] + 1;
System.out.println(g[1][0]); // 10
}
}
How big is the grid?
g.length— the number of rows.g[0].length— the number of columns in row0.- In AP CSA every row has the same length, so
g[0].lengthis the column count for the whole grid.
グリッドのサイズは?
g.length— 行の数。g[0].length— 行0の列の数。- AP CSAでは各行の長さが等しいため、
g[0].lengthはグリッド全体の列数を表す。
public class Main {
public static void main(String[] args) {
int[][] g = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(g.length); // 2 rows
System.out.println(g[0].length); // 3 columns
}
}
Row-major traversal
- To visit every cell, use a loop inside a loop.
- The outer loop picks the row
r. The inner loop picks the columnc. - This visits row
0fully, then row1, and so on. This order is called row-major.
行優先順巡回
- すべてのセルを訪問するには、ループの中にループを使う。
- 外側のループで行
rを選び、内側のループで列cを選ぶ。 - この順序ではまず行
0をすべて経由し、次に行1へと進む。この順序は 行優先 (row-major) と呼ばれる。
public class Main {
public static void main(String[] args) {
int[][] g = {
{1, 2, 3},
{4, 5, 6}
};
int total = 0;
for (int r = 0; r < g.length; r++) {
for (int c = 0; c < g[0].length; c++) {
total = total + g[r][c];
}
}
System.out.println(total); // 21
}
}
Common mistakes
- Index a grid
grid[row][col]. grid.lengthis the number of rows;grid[0].lengthis the columns.
よくあるミス
- グリッド
grid[row][col]にインデックスを付ける。 grid.lengthは行数;grid[0].lengthは列数。
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
あなたも試してみよう
- 各タスクはクラス骨組みを事前入力しています。main 内にコードを書くか、表示されたメソッドを完成させてください。
- 実行 を押してコンパイル・実行し、その後 回答を確認 を押してください。
- コードはサーバー上でコンパイル・実行されるため、初回実行でも高速です。
A 2-D array is a grid · 2次元配列はグリッドである
A 2-D array is rows × columns; reach a cell with a[row][col]. · 2次元配列は行数×列数であり、a[row][col] でセルにアクセスできます。
Complete sumGrid(int[][] g) so it returns the total of every cell in the grid. Use a nested loop with g.length rows and g[0].length columns. · sumGrid(int[][] g) を完成させ、グリッド内のすべてのセルの合計を返すようにしなさい。g.length 行と g[0].length 列の二重ループを使います。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete rowSum(int[][] g, int r) so it returns the total of the cells in row r only. Loop over the columns of that one row. · rowSum(int[][] g, int r) を完成させ、行 r だけのセルの合計を返すようにしなさい。その行の列をループで回します。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
In main, the grid is given. Print each row on its own line, with the numbers joined by a single space. For the grid shown, the output is 1 2 3 then 4 5 6. (Build each line in row-major order, then println it.) · main において、グリッドが与えられています。各 行 を独立した行として印刷し、数字は半角スペースで繋げてください。表示されるグリッドの場合、出力は 1 2 3 、次に 4 5 6 です。(各行を行優先順に構築後、println で出力します。)
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。