1-D arrays: store, index, and traverse
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
Many values in one box
- An array holds many values of the same type in order.
- Until now one variable held one value. An array holds a whole list.
- For example, the test scores of a class can live in one
intarray.
العربية
قيم متعددة في صندوق واحد
- المصفوفة تحتفظ بالعديد من القيم من نفس النوع بالترتيب.
- حتى الآن كان متغير واحد يحتفظ بقيمة واحدة. المصفوفة تحتفظ بقائمة كاملة.
- على سبيل المثال، يمكن أن توجد درجات اختبار الفصل في مصفوفة
intواحدة.
English
Make an array with values
- If you know the values, list them in curly braces
{ }. - The type is the element type plus
[], likeint[]. - Java counts how many values you gave and makes the array that size.
العربية
إنشاء مصفوفة بقيم
- إذا كنت تعرف القيم، فاذكرها بين أقواس مجعدة
{ }. - النوع هو نوع العنصر زائد
[]، مثلint[]. - تحسب Java عدد القيم التي قدمتها وتصنع المصفوفة بنفس الحجم.
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
System.out.println(scores[0]); // 90
System.out.println(scores[2]); // 70
}
}
English
Index positions
- Each value has an index (its position). Indexes start at 0.
- So the first value is
scores[0], the second isscores[1], and so on. - You can also change a value:
scores[1] = 85;.
العربية
مواقع الفهرس
- لكل قيمة فهرس (موقعها). تبدأ الفهارس من 0.
- لذلك القيمة الأولى هي
scores[0]، والثانية هيscores[1]، وهكذا. - يمكنك أيضًا تغيير قيمة:
scores[1] = 85;.
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
scores[1] = 85; // change the middle value
System.out.println(scores[1]); // 85
}
}
English
Make an empty array
- Use
new int[n]to make an array ofnvalues. - Every
intslot starts at 0 automatically. - Then you fill the slots one by one.
العربية
إنشاء مصفوفة فارغة
- استخدم
new int[n]لإنشاء مصفوفة منnقيم. - يبدأ كل slot
intمن 0 تلقائيًا. - ثم املأ Slots واحدا تلو الآخر.
public class Main {
public static void main(String[] args) {
int[] nums = new int[3]; // {0, 0, 0}
nums[0] = 5;
nums[1] = 6;
System.out.println(nums[0] + " " + nums[1] + " " + nums[2]); // 5 6 0
}
}
English
.length
array.lengthtells you how many values the array has.- Note: no parentheses — it is
scores.length, notscores.length(). - The last valid index is always
length - 1.
العربية
.length
array.lengthيخبرك بعدد القيم الموجودة في المصفوفة.- ملاحظة: بدون أقواس — هي
scores.length، وليستscores.length(). - آخر فهرس صالح هو دائمًا
length - 1.
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
System.out.println(scores.length); // 3
System.out.println(scores[scores.length - 1]); // 70 (last value)
}
}
English
Traverse with a for loop
- To visit every value, loop the index from
0up tolength - 1. - Use
i < array.lengthas the stop test. - Below we add up every value.
العربية
التجول باستخدام حلقة for
- لزيارة كل قيمة، حرك الفهرس من
0حتىlength - 1. - استخدم
i < array.lengthكاختيار التوقف. - في الأسفل نقوم بجمع كل قيمة.
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
int total = 0;
for (int i = 0; i < scores.length; i++) {
total = total + scores[i];
}
System.out.println(total); // 240
}
}
English
Enhanced for loop
- The enhanced for (or "for-each") visits each value directly.
- Read it as: "for each
int vinscores". - Use it when you only need the values, not the index.
العربية
حلقة for المحسنة
- حلقة for المحسنة (أو "for-each") تزور كل قيمة مباشرة.
- اقرأها كـ: "لكل
int vفيscores". - استخدمها عندما تحتاج فقط إلى القيم، وليس الفهرس.
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
int total = 0;
for (int v : scores) {
total = total + v;
}
System.out.println(total); // 240
}
}
English
Common mistakes
a.lengthhas no(); valid indexes run0toa.length - 1.- Going out of range throws ArrayIndexOutOfBoundsException.
العربية
أخطاء شائعة
a.lengthليس لديه()؛ الفهارس الصالحة تمتد من0إلىa.length - 1.- الخروج عن النطاق يرمي استثناء ArrayIndexOutOfBoundsException.
English
Now you try
- Some tasks print a result; others complete a method the Harness calls.
- Remember: indexes start at 0, and the last index is
length - 1. - Press Run to compile, then Check answer.
العربية
الآن جرب بنفسك
- بعض المهام تطبع نتيجة؛ وأخرى تكمل طريقة يستدعيها محرّك الاختبار.
- تذكر: تبدأ الفهارس من 0، والفهرس الأخير هو
length - 1. - اضغط تشغيل للتجميع، ثم فحص الإجابة.
Explore · استكشف
A 1-D array, indexed
An array stores values in order; reach one with a[i].
The array nums is already filled. Use a loop to add up all its values and print the total on one line. For {4, 8, 15, 16, 23, 42} the output is 108.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete sum(int[] a) so it returns the sum of all values in the array. An empty array should return 0. The checker calls it with several arrays.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete doubleAll(int[] a). It does not return anything. Instead it changes the array in place so every value becomes twice as big. {1, 2, 3} becomes {2, 4, 6}.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.