2-D arrays: working with a grid
A grid of values
- อาร์เรย์ธรรมดาคือแถวเดียวของข้อมูล
- 2-D array คือ ตาราง: มี แถว และ คอลัมน์ เหมือนตารางหรือสเปรดชีต
- เราใช้มันสำหรับกระดานเกม ภาพ ตารางคะแนน และอื่นๆ อีกมากมาย
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). - คุณยังสามารถเติมค่าลงใน
{...}ได้โดยตรง
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
- เซลล์ต้องการสอง indexes:
g[row][col]. Row ก่อน แล้วตามด้วย column - Both indexes start at 0.
g[1][2] = 9;stores9in row1, column2.
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
}
}
เกริดมีขนาดใหญ่เพียงใด?
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
}
}
การ traverses แบบ Row-major
- เพื่อเข้าถึง ทุก เซลล์ ให้ใช้ลูป ภายใน ลูปอีกชั้นหนึ่ง
- ลูปภายนอกเลือก แถว
rลูปภายในเลือก คอลัมน์c - วิธีนี้จะเข้าถึงแถว
0ทั้งหมดก่อน แล้วจึงถึงแถว1และ seterusnya ลำดับนี้เรียกว่า 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
}
}
ข้อผิดพลาดที่พบบ่อย
- Access เกริด
grid[row][col] grid.lengthคือจำนวน rows;grid[0].lengthคือจำนวน columns
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- สำหรับโปรแกรมที่สมบูรณ์ Run จะคอมไพล์และรันมัน สำหรับงาน method หรือ class Check answer จะคอมไพล์โค้ดของคุณด้วยการทดสอบที่จัดเตรียมไว้และแสดงผล
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
A 2-D array is a grid
A 2-D array is rows × columns; reach a cell with a[row][col].
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Complete sumGrid(int[][] g) so it returns the รวม of every cell in the grid. Use a nested loop with g.length rows and g[0].length columns.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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.)
คลิก Run เพื่อดูผลลัพธ์ที่นี่