נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

מערכים ומערכים דו-ממדייםArrays & 2D arrays

מפגש 8: יצירה, גישה ואורך, לולאות על מערכים, Arrays.toString ו-sort, ומערכים דו-ממדיים כמו לוח משחק.Session 8: Creating, accessing and length, looping over arrays, Arrays.toString and sort, and 2D arrays like a game board.

מה נעשה היום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
נושא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:
נושאTopic 2

יצירה, אינדקס ו-lengthCreating, indexing and length

יצירה, אינדקס ו-lengthCreating, indexing and length

  • מערך בגודל קבוע.An array has a fixed size.
  • התא הראשון באינדקס 0 והאחרון ב-length - 1.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.
נושא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.
שאלה לכיתה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]);

    שני משתנים, מערך אחד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
    נושא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].
    נושא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:

    טעות נפוצה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.
    נושאTopic 7

    פרויקט מדףShelf project

    הצעד של היום בפרויקט מדףToday's step in the Shelf project

    • מדף, צעד 8: מערך של ספריםShelf, step 8: an array of books

    מערך: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע: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:

    מערך בגודל קבוע.An array has a fixed size.

    התא הראשון באינדקס 0 והאחרון ב-length - 1.The first cell is index 0 and the last is length - 1.

    מערך חדש של מספרים מתחיל באפסים.A new number array starts full of zeros.

    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.

    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.

    Arrays.sort ממיין במקום.Arrays.sort sorts in place.

    מערך דו-ממדי הוא מערך של שורות: grid[r][c].A 2D array is an array of rows: grid[r][c].

    Index 3 out of bounds for length 3:Index 3 out of bounds for length 3:

    מדף, צעד 8: מערך של ספריםShelf, step 8: an array of books

    מה האינדקס של התא האחרון במערך באורך 5?What's the index of the last cell in an array of length 5?

    • 55
    • 44
    • 66
    • 11

    44

    מה ידפיס הקוד?What does it print?

    • 11
    • 99
    • שגיאהAn error
    • 22

    99

    איך מדפיסים מערך בצורה קריאה?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()

    Arrays.toString(a)Arrays.toString(a)

    ממוצע ציוניםAverage of grades

    • במערך {85, 92, 78, 95} חשבו בלולאה את הממוצע והדפיסו `Average:For the array {85, 92, 78, 95} compute the average in a loop and print `Average:

    הכי חם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.

    מה למדנוWhat we learned

    • מערך: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:array: A row of cells of the same type, with a fixed size:
    • מערך בגודל קבוע.An array has 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:

    עכשיו לתרגולNow, practice

    6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
    הערות מרצהSpeaker notes

    נבנה על ידיBuilt by Ariel Yohimovich