1-D arrays: store, index, and traverse · מערכים 1D: אחסון, אינדקס וטיול
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.
ערכים רבים בקופסה אחת
- array מחזיק ערכים רבים באותו סוג בסדר.
- עד כן משתנה אחד החזיק ערך אחד. array מחזיק רשימה שלמה.
- לדוגמה, ציוני מבחן של כיתה יכולים להיות ב-
intאחד.
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.
ליצור array עם ערכים
- אם אתה יודע את הערכים, ספר אותם בתוך סוגריים מעוגלים
{ }. - הסוג הוא סוג האלמנט פלו
[], כמו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
}
}
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
}
}
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ערכים. - כל תא ב
intמתחיל אוטומטית ב-0. - לאחר מכן ממלאים את התאים אחד אחר אחד.
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
}
}
.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)
}
}
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
}
}
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 משופרת
- ה-enhanced 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
}
}
Common mistakes
a.lengthhas no(); valid indexes run0toa.length - 1.- Going out of range throws ArrayIndexOutOfBoundsException.
טעויות נפוצות
a.lengthאין לו(); אינדקסים תקפים נעים מ-0עד ל-a.length - 1.- יציאה מחוץ לגבול גורמת להוצאת חריגת ArrayIndexOutOfBoundsException.
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.
כעת תנסו בעצמכם
- חלק מהמשימות מדפיאות תוצאה, ואחרים מבצעים שיטות ש-Harness קורא להן.
- זכרו: אינדקסים מתחילים ב-0, והאינדקס האחרון הוא
length - 1. - לחץ על Run להתקפלה, ולאחר מכן לחץ על Check answer.
A 1-D array, indexed · מערך 1D, מאינדקס
An array stores values in order; reach one with a[i]. · מערך אוסף ערכים בסדר; ניתן לגשת לאחד באמצעות 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. · המערך nums מלא כבר. השתמש בלולאה כדי לחבר את כל הערכים ולהדפיס את הסכום בשורה אחת. עבור {4, 8, 15, 16, 23, 42} התוצאה היא 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. · השלם sum(int[] a) כך שיתחזיר את הסכום של כל הערכים במערך. מערך ריק צריך להחזיר 0. הבוקר (checker) יקרא את הפונקציה עם מספר מערכים שונים.
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}. · השלם doubleAll(int[] a). הוא לא מחזיר דבר. במקום זאת הוא משנה את המערך באופן מקומי כך שכל ערך יהפוך לגדול פי שניים. {1, 2, 3} הופך ל{2, 4, 6}.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.