2-D arrays: working with a grid · Mảng 2-D: thao tác với lưới ô vuông
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.
Một lưới các giá trị
- Mảng thông thường là một hàng đơn các giá trị.
- Mảng 2-D là một lưới: nó có hàng và cột, giống như bảng hoặc sheet tính toán.
- Chúng ta sử dụng nó cho bàn cờ trò chơi, hình ảnh, bảng điểm, và nhiều thứ khác.
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
{...}.
Tạo mảng 2-D
int[][] g = new int[3][4];tạo một lưới với 3 hàng và 4 cột.- Every cell starts at
0(forint). - Bạn cũng có thể điền trực tiếp bằng các giá trị bên trong
{...}.
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.
Đọc và ghi một ô
- Một ô cần hai chỉ số:
g[row][col]. Hàng trước, sau đó là cột. - Cả hai chỉ số đều bắt đầu từ 0.
g[1][2] = 9;lưu9vào hàng1, cột2.
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.
Grid có kích thước bao nhiêu?
g.length— số lượng hàng.g[0].length— số lượng cột trong hàng0.- Trong AP CSA, mỗi hàng đều có độ dài bằng nhau, nên
g[0].lengthlà số cột của toàn bộ grid.
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.
Duyệt theo hàng
- Để truy cập mọi ô, hãy dùng vòng lặp bên trong một vòng lặp khác.
- Vòng lặp ngoài chọn hàng
r. Vòng lặp bên trong chọn cộtc. - Cách này duyệt hết hàng
0, sau đó đến hàng1, và cứ thế tiếp diễn. Thứ tự này gọi là duyệt theo hàng (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.
Lỗi thường gặp
- Chỉ số cho grid
grid[row][col]. grid.lengthlà số hàng;grid[0].lengthlà số cột.
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.
Bây giờ bạn thử
- Mỗi nhiệm vụ điền sẵn khung lớp — viết mã của bạn bên trong main, hoặc hoàn thành phương thức được hiển thị.
- Nhấn Chạy để biên dịch và chạy, sau đó Kiểm tra câu trả lời.
- Mã của bạn được biên dịch và chạy trên máy chủ, vì vậy ngay cả lần chạy đầu tiên cũng rất nhanh.
A 2-D array is a grid · Một mảng 2-D là một lưới ô vuông
A 2-D array is rows × columns; reach a cell with a[row][col]. · Một mảng 2-D có cấu trúc hàng × cột; truy cập một ô bằng 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. · Hoàn thiện sumGrid(int[][] g) sao cho nó trả về tổng của mọi ô trong lưới. Sử dụng vòng lặp lồng nhau với g.length hàng và g[0].length cột.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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. · Hoàn thiện rowSum(int[][] g, int r) sao cho nó trả về tổng các ô trong hàng r duy nhất. Duyệt qua các cột của hàng đó.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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.) · Trong main, lưới được cung cấp. In mỗi hàng ra một dòng riêng biệt, với các số cách nhau bởi một dấu cách. Với lưới hiển thị ở trên, kết quả đầu ra sẽ là 1 2 3 rồi đến 4 5 6. (Xây dựng mỗi dòng theo thứ tự hàng trước cột, sau đó dùng println.)
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.