2-D arrays: working with a grid · 2-Mصفوف: التعامل مع الشبكة
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-A هي شبكة: تحتوي على صفوف وأعمدة، مثل الجدول أو ورقة العمل.
- نستخدمها لألواح الألعاب، والصور، وجداول الدرجات، والمزيد.
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-A
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، أو أكمل الدالة المعروضة.
- اضغط تشغيل للمعالجة والتشغيل، ثم تحقق من الإجابة.
- كودك يُعالج ويُشغل على الخادم، لذا حتى التشغيل الأول سريع.
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. · اضغط تشغيل لرؤية المخرجات هنا.