2-D arrays: working with a grid · מערכים בעלי ממד 2-D: עבודה עם רשת
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-D הוא גריד: יש לו שורות ו-עמודות, כמו טבלה או גיליון עבודה.
- אנו משתמשים בו ללוחות משחק, תמונות, טבלאות ציונים ועוד.
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.
קריאה וכתיבה בתא יחיד
- תא זקוק ל-שני אינדקסים:
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.
עברת שורה-על-שורה (Row-major traversal)
- כדי לבקר בכל תא, השתמש במחזוריות בתוך מחזוריות.
- המחזוריות החיצונה בוחרת את השורה
r. המחזוריות הפנימית בוחרת את העמודהc. - זה מבקר בשורה
0במלואה, ואז בשורה1, וכו'. סדר זה נקרא שורה-על-שורה.
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, או השלם את השיטה המוצגת.
- לחץ על Run כדי להרכיב ולהריץ, ולאחר מכן לחץ על Check answer.
- הקוד שלך נרכב ונרץ על השרת, ולכן גם ההפעלה הראשונה היא מהירה.
A 2-D array is a grid · מערך בעל ממד 2-D הוא רשת
A 2-D array is rows × columns; reach a cell with a[row][col]. · מערך בעל ממד 2-D הוא שורות × עמודות; הגע לתא באמצעות 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. (בנה כל שורה בסדר תאי שורה-עמודה, ולאחר מכן הדפס אותה.)
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.