מפגש 8: יצירה, גישה ואורך, לולאות על מערכים, Arrays.toString ו-sort, ומערכים דו-ממדיים כמו לוח משחק.Session 8: Creating, accessing and length, looping over arrays, Arrays.toString and sort, and 2D arrays like a game board.
יצירה, גישה ואורך, לולאות על מערכים, Arrays.toString ו-sort, ומערכים דו-ממדיים כמו לוח משחק. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Creating, accessing and length, looping over arrays, Arrays.toString and sort, and 2D arrays like a game board. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מונחיםTerms
יצירה, אינדקס ו-lengthCreating, indexing and length
לולאות על מערך: סכום ומקסימוםLooping over an array: sum and max
שני משתנים, מערך אחדTwo variables, one array
מיון ומערך דו-ממדיSorting and a 2D array
טעויות נפוצותCommon mistakes
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
מונחיםTerms
המונחים של המפגשThis session's terms
מערך: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:array: A row of cells of the same type, with a fixed size:
length: מספר התאים במערך:length: The number of cells in an array:
שתי הפניות לאותו עצם: int[] b = a; לא מעתיק את המערך:aliasing: int[] b = a; doesn't copy the array:
מערך דו-ממדי: מערך של מערכים:2D array: An array of arrays:
ArrayIndexOutOfBoundsException: החריגה כשפונים לתא שלא קיים:ArrayIndexOutOfBoundsException: The exception for a cell that doesn't exist:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.
נושאTopic 2
יצירה, אינדקס ו-lengthCreating, indexing and length
יצירה, אינדקס ו-lengthCreating, indexing and length
התא הראשון באינדקס 0 והאחרון ב-length - 1.The first cell is index 0 and the last is length - 1.
מערך חדש של מספרים מתחיל באפסים.A new number array starts full of zeros.
מערך בגודל קבוע. התא הראשון באינדקס 0 והאחרון ב-length - 1. מערך חדש של מספרים מתחיל באפסים.An array has a fixed size. The first cell is index 0 and the last is length - 1. A new number array starts full of zeros.
נושאTopic 3
לולאות על מערך: סכום ומקסימוםLooping over an array: sum and max
לולאות על מערך: סכום ומקסימוםLooping over an array: sum and max
for רגיל כשצריך את האינדקס, for-each כשצריך רק את הערכים.A plain for when you need the index, a for-each when you only need the values.
למקסימום מתחילים מהתא הראשון.For a maximum, start from the first cell.
for רגיל כשצריך את האינדקס, for-each כשצריך רק את הערכים. למקסימום מתחילים מהתא הראשון.A plain for when you need the index, a for-each when you only need the values. For a maximum, start from the first cell.
נושאTopic 4
שני משתנים, מערך אחדTwo variables, one array
שני משתנים, מערך אחדTwo variables, one array
b = a מעתיק את ההפניה, לא את המערך: שניהם מצביעים לאותו עצם (שתי הפניות לאותו עצם).b = a copies the reference, not the array: both point to the same object (aliasing).
clone() יוצר מערך חדש.clone() makes a new array.
צפו בזיכרון: b מצביע לאותו # כמו a, ו-c לעצם אחר.Watch the memory: b points to the same # as a, and c to another object.
b = a מעתיק את ההפניה, לא את המערך: שניהם מצביעים לאותו עצם (שתי הפניות לאותו עצם). clone() יוצר מערך חדש. צפו בזיכרון: b מצביע לאותו # כמו a, ו-c לעצם אחר.b = a copies the reference, not the array: both point to the same object (aliasing). clone() makes a new array. Watch the memory: b points to the same # as a, and c to another object.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
int[] a = {5, 6, 7};
int[] b = a;
b[0] =50;
int[] c = a.clone();
c[1] =60;
System.out.println(a[0] +" "+ a[1]);
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
שני משתנים, מערך אחדTwo variables, one array
Main.java · main
int[] a = {5, 6, 7};
int[] b = a;
b[0] =50;
int[] c = a.clone();
c[1] =60;
System.out.println(a[0] +" "+ a[1]);
פלטOutput
50 6
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושאTopic 5
מיון ומערך דו-ממדיSorting and a 2D array
מיון ומערך דו-ממדיSorting and a 2D array
Arrays.sort ממיין במקום.Arrays.sort sorts in place.
מערך דו-ממדי הוא מערך של שורות: grid[r][c].A 2D array is an array of rows: grid[r][c].
Arrays.sort ממיין במקום. מערך דו-ממדי הוא מערך של שורות: grid[r][c].Arrays.sort sorts in place. A 2D array is an array of rows: grid[r][c].
נושאTopic 6
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
Index 3 out of bounds for length 3:Index 3 out of bounds for length 3:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.
טעות נפוצהCommon mistake
Main.java · main
int[] a = {10, 20, 30};
int sum =0;
for (int i =0; i <= a.length; i++) {
sum += a[i];
}
System.out.println(sum);
מה Java עונהWhat Java says
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
Index 3 out of bounds for length 3: ב-i <= a.length הלולאה מגיעה ל-i = 3, אבל האינדקס האחרון הוא 2. מתקנים ל-i < a.length.Index 3 out of bounds for length 3: with i <= a.length the loop reaches i = 3, but the last index is 2. Fix it to i < a.length.
צייד באגים: מעבר לקצהBug hunter: past the edge
נושאTopic 7
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
מדף, צעד 8: מערך של ספריםShelf, step 8: an array of books
מערך String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"} ומערך int[] years = {1815, 1965, 1922, 1987}. הדפיסו את הספר הישן ביותר (Oldest: Emma (1815)), ואת השמות ממוינים לפי א-ב (Arrays.sort על עותק).An array String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"} and int[] years = {1815, 1965, 1922, 1987}. Print the oldest book (Oldest: Emma (1815)), and the titles sorted alphabetically (Arrays.sort on a copy).
מערך: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:array: A row of cells of the same type, with a fixed size:
המונחים של המפגשThis session's terms
length: מספר התאים במערך:length: The number of cells in an array:
המונחים של המפגשThis session's terms
שתי הפניות לאותו עצם: int[] b = a; לא מעתיק את המערך:aliasing: int[] b = a; doesn't copy the array:
המונחים של המפגשThis session's terms
מערך דו-ממדי: מערך של מערכים:2D array: An array of arrays:
המונחים של המפגשThis session's terms
ArrayIndexOutOfBoundsException: החריגה כשפונים לתא שלא קיים:ArrayIndexOutOfBoundsException: The exception for a cell that doesn't exist:
יצירה, אינדקס ו-lengthCreating, indexing and length
התא הראשון באינדקס 0 והאחרון ב-length - 1.The first cell is index 0 and the last is length - 1.
יצירה, אינדקס ו-lengthCreating, indexing and length
מערך חדש של מספרים מתחיל באפסים.A new number array starts full of zeros.
יצירה, אינדקס ו-lengthCreating, indexing and length
for רגיל כשצריך את האינדקס, for-each כשצריך רק את הערכים.A plain for when you need the index, a for-each when you only need the values.
לולאות על מערך: סכום ומקסימוםLooping over an array: sum and max
למקסימום מתחילים מהתא הראשון.For a maximum, start from the first cell.
לולאות על מערך: סכום ומקסימוםLooping over an array: sum and max
b = a מעתיק את ההפניה, לא את המערך: שניהם מצביעים לאותו עצם (שתי הפניות לאותו עצם).b = a copies the reference, not the array: both point to the same object (aliasing).
שני משתנים, מערך אחדTwo variables, one array
clone() יוצר מערך חדש.clone() makes a new array.
שני משתנים, מערך אחדTwo variables, one array
צפו בזיכרון: b מצביע לאותו # כמו a, ו-c לעצם אחר.Watch the memory: b points to the same # as a, and c to another object.
שני משתנים, מערך אחדTwo variables, one array
Arrays.sort ממיין במקום.Arrays.sort sorts in place.
מיון ומערך דו-ממדיSorting and a 2D array
מערך דו-ממדי הוא מערך של שורות: grid[r][c].A 2D array is an array of rows: grid[r][c].
מיון ומערך דו-ממדיSorting and a 2D array
Index 3 out of bounds for length 3:Index 3 out of bounds for length 3:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
מדף, צעד 8: מערך של ספריםShelf, step 8: an array of books
הצעד של היום בפרויקט מדףToday's step in the Shelf project
מה האינדקס של התא האחרון במערך באורך 5?What's the index of the last cell in an array of length 5?
55
44
66
11
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
44
נכון: מ-0 עד 4.Right: 0 to 4.
מה ידפיס הקוד?What does it print?
11
99
שגיאהAn error
22
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
99
נכון: a ו-b הם אותו מערך.Right: a and b are the same array.
איך מדפיסים מערך בצורה קריאה?How do you print an array readably?
System.out.println(a);System.out.println(a);
Arrays.toString(a)Arrays.toString(a)
a.toString()a.toString()
a.print()a.print()
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
Arrays.toString(a)Arrays.toString(a)
נכון: [1, 2, 3].Right: [1, 2, 3].
ממוצע ציוניםAverage of grades
במערך {85, 92, 78, 95} חשבו בלולאה את הממוצע והדפיסו `Average:For the array {85, 92, 78, 95} compute the average in a loop and print `Average:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
הכי חםThe hottest
במערך טמפרטורות {24, 31, 28, 35, 30} מצאו את הגבוהה ואת היום שלה (אינדקס מ-0), והדפיסו Max 35 on day 3.In the temperature array {24, 31, 28, 35, 30} find the highest and its day (index from 0), and print Max 35 on day 3.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
מה למדנוWhat we learned
מערך: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:array: A row of cells of the same type, with a fixed size:
for רגיל כשצריך את האינדקס, for-each כשצריך רק את הערכים.A plain for when you need the index, a for-each when you only need the values.
b = a מעתיק את ההפניה, לא את המערך: שניהם מצביעים לאותו עצם (שתי הפניות לאותו עצם).b = a copies the reference, not the array: both point to the same object (aliasing).
Arrays.sort ממיין במקום.Arrays.sort sorts in place.
Index 3 out of bounds for length 3:Index 3 out of bounds for length 3:
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.
עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked