Skip to content · ⁨דלג לתוכן⁩

Data Collections · ⁨אוספי נתונים⁩

AP Computer Science A · ⁨מדעי מחשב A - AP⁩ · Topic 4 · ⁨נושא 4⁩

Video lesson for this topic · ⁨שיעור וידאו לנושא זה⁩ Open the video page · ⁨פתח את עמוד הוידאו⁩
13:32

Data Collections · ⁨אוספי נתונים⁩

Take one photo on your phone. To the computer it is not a picture at all — it is a grid of numbers, one for every pixel, about twelve million of them. Now try…

English narration · English + 中文 subtitles burned in · ⁨קריאת קול באנגלית · תרגום אנגלי + סינית שרוף בתוך הסרטון⁩

4.1

The Ethics of Collecting Data · ⁨אתיקה באיסוף נתונים⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.1.A: Explain the risks to privacy from collecting and storing personal data on computer systems.

  • 4.1.A.1 When using a computer, personal privacy is at risk. When developing new programs, programmers should attempt to safeguard the personal privacy of the user.

Learning Objective 4.1.B: Explain the importance of recognizing data quality and potential issues when using a data set.

  • 4.1.B.1 Algorithmic bias describes systemic and repeated errors in a program that create unfair outcomes for a specific group of users.
  • 4.1.B.2 Programmers should be aware of the data set collection method and the potential for bias when using this method before using the data to extrapolate new information or drawing conclusions.
  • 4.1.B.3 Some data sets are incomplete or contain inaccurate data. Using such data in the development or use of a program can cause the program to work incorrectly or inefficiently.

Learning Objective 4.1.C: Identify an appropriate data set to use in order to solve a problem or answer a specific question.

  • 4.1.C.1 Contents of a data set might be related to a specific question or topic and might not be appropriate to give correct answers or extrapolate information for a different question or topic.
עברית

מטרת הלמידה 4.1.A: הסבר הסיכונים לפרטיות הנובעים מאיסוף ואחסון נתונים אישיים על מערכות מחשב.

  • 4.1.A.1 בעת שימוש במחשב, הפרטיות האישית נמצאת בסכנה. בפיתוח תוכנות חדשות, תוכנתנים צריכים לנסות לשמור על פרטיות המשתמש.

מטרת הלמידה 4.1.B: הסבר החשיבות בהכרה באיכות הנתונים ובבעיות אפשריות בעת שימוש במאגר נתונים.

  • 4.1.B.1 הטיה אלגוריתמית מתארת טעויות מערכתיות ומחוזקות בתוכנה היוצרות תוצאות לא הוגנות עבור קבוצת משתמשים ספציפית.
  • 4.1.B.2 תוכנתנים צריכים להיות מודעים לשיטת איסוף הנתונים ולסיכון ההטיה בעת שימוש בשיטה זו, לפני השימוש בנתונים לחילוץ מידע חדש או למסקנות.
  • 4.1.B.3 חלק ממאגרי הנתונים הם חסרים או מכילים נתונים לא מדויקים. שימוש בנתונים כאלה בפיתוח או בשימוש בתוכנה עלול לגרום לתוכנה לפעול בצורה לא נכונה או לא יעילה.

מטרת הלמידה 4.1.C: זיהוי מאגר נתונים מתאים לשימוש כדי לפתור בעיה או לענות על שאלה ספציפית.

  • 4.1.C.1 תוכן ערכי הנתונים עשוי להיות קשור לשאלה או נושא ספציפי ועשוי לא להיות מתאים למתן תשובות נכונות או להסקת מידע לשאלה או נושא אחרים.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Programs that gather data raise questions of privacy 隐私 and consent 同意. Collect only what is needed, protect it, and be honest about its use. Data can carry bias 偏见 if it does not represent everyone fairly, leading to unfair results – a responsibility that comes with storing information.

עברית
מארזי שרתים במרכז נתונים — אוסף נתונים גדול מעלה שאלות אתיות לגבי איסוף ושימוש
מארזי שרתים במרכז נתונים — אוסף נתונים גדול מעלה שאלות אתיות לגבי איסוף ושימוש

תוכניות שמאספות נתונים מעלות שאלות של פרטיות והסכמה. יש לאסוף רק מה שנדרש, לשמור עליו ולהיות כנים לגבי השימוש בו. נתונים יכולים לשאת טייט אם הם אינם מייצגים את כולם באופן הוגן, מה שמוביל לתוצאות לא הוגנות – אחריות זו נובעת מאחסון מידע.

4.2

Why We Need Data Structures · ⁨מדוע אנו זקוקים למבני נתונים⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.2.A: Represent patterns and algorithms that involve data sets found in everyday life using written language or diagrams.

  • 4.2.A.1 A data set is a collection of specific pieces of information or data.
  • 4.2.A.2 Data sets can be manipulated and analyzed to solve a problem or answer a question. When analyzing data sets, values within the set are accessed and utilized one at a time and then processed according to the desired outcome.
  • 4.2.A.3 Data can be represented in a diagram by using a chart or table. This visual can be used to plan the algorithm that will be used to manipulate the data.
עברית

מטרת הלמידה 4.2.A: להציג דפוסים ואלגוריתמים המשתמשים במאגרי נתונים הנמצאים בחיי היומיום באמצעות כתיבה או דיאגרמות.

  • 4.2.A.1 מאגר נתונים הוא אוסף של פרטי מידע או נתונים ספציפיים.
  • 4.2.A.2 ניתן לבצע עיבוד וניתוח על מאגרי נתונים כדי לפתור בעיה או לענות על שאלה. בעת ניתוח מאגרי נתונים, הערכים בתוך המאגר נגישים ומשתמשים בהם אחד-אחד, ולאחר מכן מעובדים בהתאם לתוצאה הרצויה.
  • 4.2.A.3 ניתן לייצג נתונים בדיאגרמה באמצעות טבלה או גרף. תוכן ויזואלי זה יכול לשמש לתכנון האלגוריתם שיישמש לעיבוד הנתונים.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

A single variable holds one value; real problems need to store many related values – a class roster, pixels, sensor readings. A data structure 数据结构 organizes a collection so we can store, find, and process items efficiently. The AP course uses three: the array, the ArrayList, and the 2D array.

עברית
ארון תיקיות: אוספים מאחסנים ערכים רבים תחת שם אחד כדי שהאלגוריתמים יוכלו לעבדם
ארון תיקיות: אוספים מאחסנים ערכים רבים תחת שם אחד כדי שהאלגוריתמים יוכלו לעבדם

משתנה אחד מחזיק ערך אחד; בעיות אמיתיות דורשות אחסון של מספר רב של ערכים קשורים – רשימת תלמידים, פיקסלים, קריאות חיישנים. מבנה נתונים מארגן קבוצה כך נוכל לאחסן, למצוא ולעבד פריטים ביעילות. הקורס AP משתמש בשלושה: מערך, ArrayList, ו-מערך דו-ממדי ⟨2⟩.

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
privacy/ˈprɪvəsi/ פרטיות
consent/kənˈsent/ הסכמה
bias/ˈbaɪəs/ שיפוטיות
data structure/ˈdeɪtə ˈstrʌktʃə/ מבנה נתונים
array/əˈreɪ/ מערך
Traverse/trəˈvɜːs/ עבר
ArrayList/əˈreɪ lɪst/ ArrayList
2D array/ˌtuː ˈdiː əˈreɪ/ מערך 2D
row-major order/rəʊ ˈmeɪdʒə ˈɔːdə/ סדר תלת-ממדי לפי שורות (row-major order)
4.3

Making and Reading an Array · ⁨יצירת וקריאה ממערך⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.3.A: Develop code used to represent collections of related data using one-dimensional (1D) array objects.

  • 4.3.A.1 An array stores multiple values of the same type. The values can be either primitive values or object references.
  • 4.3.A.2 The length of an array is established at the time of creation and cannot be changed. The length of an array can be accessed through the length attribute.
  • 4.3.A.3 When an array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.
  • 4.3.A.4 Initializer lists can be used to create and initialize arrays.
  • 4.3.A.5 Square brackets [ ] are used to access and modify an element in a 1D array using an index.
  • 4.3.A.6 The valid index values for an array are 0 through one less than the length of the array, inclusive. Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException.
עברית

מטרת הלמידה 4.3.A: פיתוח קוד המשמש לייצוג אוספים של נתונים קשורים באמצעות אובייקטי מערך חד-ממדי (1D).

  • 4.3.A.1 מערך מאחסן מספר ערכים מאותו סוג. הערכים יכולים להיות ערכים ראשוניים או הפניות לאובייקטים.
  • 4.3.A.2 אורך המערך נקבע בזמן היצירה ואינו ניתן לשינוי. ניתן לגשת לאורך המערך באמצעות ה-stalength attribute.
  • 4.3.A.3 כאשר מערך נוצר באמצעות המילה new, כל האלמנטים שלו מוגדרים לערכים ברירת מחדל של סוג הנתונים של האלמנט. הערך הרירת מחדל עבור int הוא 0, עבור double הוא 0.0, עבור boolean הוא false, ועבור סוג הפניה הוא null.
  • 4.3.A.4 ניתן להשתמש ברשימות מתחילים (initializer lists) ליצור ולאתחל מערכים.
  • 4.3.A.5 סוגריים מרובעים [ ] משמשים לגישה ולשינוי של אלמנט במערכת בעל-ממד 1 באמצעות אינדקס.
  • 4.3.A.6 ערכי האינדקס תקפים למערך הם 0 ועד אחת פחות מאורך המערך, כולל. שימוש בערך אינדקס מחוץ לטווח זה יוביל ל-staArrayIndexOutOfBoundsException.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

An array 数组 is a fixed-size, ordered collection of same-type values. Indices run from 0 to length - 1:

Accessing an index outside 0..length-1 throws an ArrayIndexOutOfBoundsException.

עברית

מערך הוא אוסף מסודר בגודל קבוע של ערכים מאותו סוג. האינדקסים נעים מ-0 עד length - 1:

מערך חד-ממדי (רשימה) עם האינדקסים והגבולות שלו
מערך חד-ממדי (רשימה) עם האינדקסים והגבולות שלו
int[] nums = new int[5];        // five zeros
int[] vals = {3, 1, 4, 1, 5};   // initialized
int first = vals[0];            // 3
int n = vals.length;            // 5 (a field, not a method)

ניסיון לגשת לאינדקס מחוץ ל-0..length-1 גורם לשגיאת ArrayIndexOutOfBoundsException.

4.4

Visiting Every Element of an Array · ⁨ביקור בכל אלמנט במערך⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.4.A: Develop code used to traverse the elements in a 1D array and determine the result of these traversals.

  • 4.4.A.1 Traversing an array is when repetition statements are used to access all or an ordered sequence of elements in an array.
  • 4.4.A.2 Traversing an array with an indexed for loop or while loop requires elements to be accessed using their indices.
  • 4.4.A.3 An enhanced for loop header includes a variable, referred to as the enhanced for loop variable. For each iteration of the enhanced for loop, the enhanced for loop variable is assigned a copy of an element without using its index.
  • 4.4.A.4 Assigning a new value to the enhanced for loop variable does not change the value stored in the array.
  • 4.4.A.5 When an array stores object references, the attributes can be modified by calling methods on the enhanced for loop variable. This does not change the object references stored in the array.
  • 4.4.A.6 Code written using an enhanced for loop to traverse elements in an array can be rewritten using an indexed for loop or a while loop.
עברית

מטרת למידה 4.4.A: פיתוח קוד המשמש לעבור על אלמנטים במערכת בעל-ממד 1 וקביעת תוצאת מעברים אלו.

  • 4.4.A.1 עברת מערך היא שימוש בפקודות חזרה כדי לגשת לכל האלמנטים או לסדרה מסודרת של אלמנטים במערך.
  • 4.4.A.2 עברת מערך באמצעות לולאת אינדקס for או לולאת while דורשת גישה לאלמנטים באמצעות האינדקסים שלהם.
  • 4.4.A.3 ראש לולאת增强ed (enhanced) for כולל משתנה, המכונה משתנה לולאת增强ed (enhanced) for. בכל איטרציה של לולאת增强ed (enhanced) for, משתנה לולאת增强ed (enhanced) for מקבל העתק של אלמנט ללא שימוש באינדקס שלו.
  • 4.4.A.4 הקצאת ערך חדש למשתנה לולאת增强ed (enhanced) for אינה משנה את הערך המאוחסן במערך.
  • 4.4.A.5 כאשר מערך מאחסן רפרנסים לאובייקטים, ניתן לשנות את התכונות על ידי קריאת מეთודים על משתנה לולאת增强ed (enhanced) for. הדבר אינו משנה את הרפרנסים לאובייקטים המאוחסנים במערך.
  • 4.4.A.6 קוד שכתוב באמצעות לולאת增强ed (enhanced) for לעבור על אלמנטים במערך יכול להיות מושב באמצעות לולאת אינדקס for או לולאת while.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Traverse 遍历 an array with a for loop (gives the index) or an enhanced for / for-each loop (gives each value, read-only):

עברית

מעבר במערך באמצעות לולאת for (נותנת את האינדקס) או לולאת enhanced for / for-each (נותנת כל ערך, לקריאה בלבד):

for (int i = 0; i < a.length; i++) { a[i] *= 2; }   // can modify
for (int v : a) { System.out.println(v); }          // read each value
4.5

Standard Array Algorithms · ⁨אלגוריתמים סטנדרטיים למערכות⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.5.A: Develop code for standard and original algorithms for a particular context or specification that involves arrays and determine the result of these algorithms.

  • 4.5.A.1 There are standard algorithms that utilize array traversals to:
    • determine a minimum or maximum value
    • compute a sum or average
    • determine if at least one element has a particular property
    • determine if all elements have a particular property
    • determine the number of elements having a particular property
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements
    • shift or rotate elements left or right
    • reverse the order of the elements
עברית

מטרת הלמידה 4.5.A: פיתוח קוד לאלגוריתמים סטנדרטיים וחדשים בהקשר או ספציפיקציה נתונים המעורבים במערכים, והחלטת תוצאות האלגוריתמים הללו.

  • 4.5.A.1 קיימים אלגוריתמים סטנדרטיים המשתמשים בעברות מערך עבור:
    • קביעת ערך מינימום או מקסימום
    • חישוב סכום או ממוצע
    • לקבוע אם לפחות אלמנט אחד בעל תכונה מסוימת
    • לקבוע אם לכל האלמנטים יש תכונה מסוימת
    • לקבוע את מספר האלמנטים שיש להם תכונה מסוימת
    • לגשת לכל הזוגות הרצופים של אלמנטים
    • לקבוע את נוכחותם או היעדרם של אלמנטים כפולים
    • להזיז או לסובב אלמנטים שמאלה או ימינה
    • להפוך את הסדר של האלמנטים

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Master these patterns: compute a sum or average, find the max/min, count items meeting a condition, check for a duplicate, and reverse or shift elements. Each is a traversal with a running result:

עברית

שלוו את הדפוסים האלה: חשב סכום או ממוצע, מצא את ה-מקסימום/מינימום, ספר פריטים העומדים בתנאי מסוים, בדוק נוכחות של כפילות, והפוך או הזז רכיבים. כל אחד מהם הוא טרברסיה עם תוצאה מצטברת:

int sum = 0;
for (int v : a) sum += v;
double avg = (double) sum / a.length;
4.6

Reading Data from a Text File · ⁨קריאת נתונים מקובץ טקסט⁩

Syllabus · ⁨סיילבוס⁩
Learning ObjectiveEssential Knowledge

4.6.A
Develop code to read data from a text file.

  • 4.6.A.1 A file is storage for data that persists when the program is not running. The data in a file can be retrieved during program execution.
  • 4.6.A.2 A file can be connected to the program using the File and Scanner classes.
  • 4.6.A.3 A file can be opened by creating a File object, using the name of the file as the argument of the constructor.
    • File(String str) is the File constructor that accepts a String file name to open for reading, where str is the pathname for the file.
  • 4.6.A.4 When using the File class, it is required to indicate what to do if the file with the provided name cannot be opened. One way to accomplish this is to add throws IOException to the header of the method that uses the file. If the file name is invalid, the program will terminate.
  • 4.6.A.5 The File and IOException classes are part of the java.io package. An import statement must be used to make these classes available for use in the program.
  • 4.6.A.6 The following Scanner methods and constructor—including what they do and when they are used—are part of the Java Quick Reference:
    • Scanner(File f) is the Scanner constructor that accepts a File for reading.
    • int nextInt() returns the next int read from the file or input source if available. If the next int does not exist or is out of range, it will result in an InputMismatchException.
    • double nextDouble() returns the next double read from the file or input source. If the next double does not exist, it will result in an InputMismatchException.
    • boolean nextBoolean() returns the next boolean read from the file or input source. If the next boolean does not exist, it will result in an InputMismatchException.
    • String nextLine() returns the next line of text as a String read from the file or input source; can return the empty string if called immediately after another Scanner method that is reading from the file or input source.
    • String next() returns the next String read from the file or input source.
    • boolean hasNext() returns true if there is a next item to read in the file or input source; returns false otherwise.
    • void close() closes this scanner.
    • Exclusion statement: Accepting input from the keyboard is outside the scope of the AP Computer Science A course and exam.
  • 4.6.A.7 Using nextLine and the other Scanner methods together on the same input source sometimes requires code to adjust for the methods' different ways of handling whitespace.
    • Exclusion statement: Writing or analyzing code that uses both nextLine and other Scanner methods on the same input source is outside the scope of the AP Computer Science A course and exam.
  • 4.6.A.8 The following additional String method—including what it does and when it is used—is part of the Java Quick Reference:
    • String[] split(String del) returns a String array where each element is a substring of this String, which has been split around matches of the given expression del.
    • Exclusion statement: The parameter del uses a format called a regular expression. Writing or analyzing code that uses any of the special properties of regular expressions (e.g., \\*, \\.) is outside the scope of the AP Computer Science A course and exam.
  • 4.6.A.9 A while loop can be used to detect if the file still contains elements to read by using the hasNext method as the condition of the loop.
  • 4.6.A.10 A file should be closed when the program is finished using it. The close method from Scanner is called to close the file.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

File and IOException live in java.io, so a program that reads a file needs import java.io.*;. Opening a file can fail (it might not exist), and Java forces you to handle that – the simplest way is to add throws IOException to the method header. A Scanner then reads the file line by line, using hasNext... to test before reading:

Reading typed tokens with nextInt(), nextDouble(), or nextBoolean() throws an InputMismatchException if the next token is the wrong type – for example calling nextInt() when the next thing in the file is the word cat.

עברית

File ו-IOException חיים ב-java.io, לכן תוכנית הקוראת קובץ זקוקה ל-import java.io.*;. פתיחת קובץ עלולה להיכשל (הוא עשוי לא להיות קיים), ו-Java מחייבת אותך לטפל בכך – הדרך הפשוטה ביותר היא להוסיף throws IOException לחתימת המетודה. Scanner קורא אחר כך את הקובץ שורה בשורה, תוך שימוש ב-hasNext... לבדיקה לפני הקריאה:

import java.io.*;
...
public static void readFile() throws IOException {
    Scanner f = new Scanner(new File("data.txt"));
    while (f.hasNextLine()) {
        String line = f.nextLine();
    }
}

קריאת טוקנים מעוגנים עם nextInt(), nextDouble() או nextBoolean() מייצרת InputMismatchException אם הטוקן הבא הוא מהסוג הלא נכון – לדוגמה קריאת nextInt() כאשר הדבר הבא בקובץ הוא המילה cat.

4.7

Wrapping a Number in an Object · ⁨עטיפת מספר באובייקט⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.7.A: Develop code to use Integer and Double objects from their primitive counterparts and determine the result of using these objects.

  • 4.7.A.1 The Integer class and Double class are part of the java.lang package. An Integer object is immutable, meaning once an Integer object is created, its attributes cannot be changed. A Double object is immutable, meaning once a Double object is created, its attributes cannot be changed.
  • 4.7.A.2 Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is:
    • passed as a parameter to a method that expects an object of the corresponding wrapper class
    • assigned to a variable of the corresponding wrapper class
  • 4.7.A.3 Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is:
    • passed as a parameter to a method that expects a value of the corresponding primitive type
    • assigned to a variable of the corresponding primitive type
  • 4.7.A.4 The following class Integer method—including what it does and when it is used—is part of the Java Quick Reference:
    • static int parseInt(String s) returns the String argument as an int.
  • 4.7.A.5 The following class Double method—including what it does and when it is used—is part of the Java Quick Reference:
    • static double parseDouble(String s) returns the String argument as a double.
עברית

מטרת למידה 4.7.A: פיתוח קוד לשימוש בInteger ובאובייקטי Double ממקבילותיהן הפרימיטיביות וקביעת תוצאת השימוש באובייקטים אלו.

  • 4.7.A.1 המחלקה Integer והמחלקה Double הן חלק מארכיון java.lang. אובייקט Integer הוא בלתי-מתחלף (immutable), כלומר לאחר יצירת אובייקט Integer, תכונותיו אינן ניתנות לשינוי. אובייקט Double הוא בלתי-מתחלף, כלומר לאחר יצירת אובייקט Double, תכונותיו אינן ניתנות לשינוי.
  • 4.7.A.2 Autoboxing הוא ההמרה האוטומטית שמבצע מחלקת Java בין סוגים פרימיטיביים למחלקות העטיפה המתאימות שלהן. הדבר כולל המרת int לInteger והמרת double לDouble. מחלקת Java מפעילה autoboxing כאשר ערך פרימיטיבי הוא:
    • עובר כפרמטר לשיטה שמצפה לאובייקט מהמחלקה העטיפה (wrapper class) המתאימה
    • מוקצה למשתנה מהמחלקה העטיפה (wrapper class) המתאימה
  • 4.7.A.3 Unboxing הוא ההמרה האוטומטית שמבצע מחלקת Java ממחלקת העטיפה לסוג הפרימיטיבי. הדבר כולל המרת Integer לint והמרת Double לdouble. מחלקת Java מפעילה unboxing כאשר אובייקט ממחלקת עטיפה הוא:
    • עובר כפרמטר לשיטה שמצפה לערך מהסוג הגולמי (primitive type) המתאים
    • מוקצה למשתנה מהסוג הגולמי (primitive type) המתאים
  • 4.7.A.4 מתודת Integer הקלאס הבאה—כולל מה היא עושה ומתי משתמשים בה—חלק מההפניה המהירה ל-Java:
    • static int parseInt(String s) מחזירה את הארגומנט String כ-int.
  • 4.7.A.5 מתודת Double הקלאס הבאה—כולל מה היא עושה ומתי משתמשים בה—חלק מההפניה המהירה ל-Java:
    • static double parseDouble(String s) מחזירה את הארגומנט String כ-double.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

An ArrayList stores objects, not primitives, so a primitive is wrapped in an object: Integer wraps int, Double wraps double. Java does this with autoboxing 自动装箱 (int to Integer) and unboxing (back again) automatically, so you can write list.add(5) and int x = list.get(0).

עברית

ArrayList מאחסן אובייקטים, ולא טיפים ראשוניים, ולכן טיפ ראשוני מועטף בתוך אובייקט: Integer מעטף את int, Double מעטף את double. Java עושה זאת באמצעות autoboxing (מ-int ל-Integer) ו-unboxing (חזרה לאחור) אוטומטית, כך שניתן לכתוב list.add(5) ו-int x = list.get(0).

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
autoboxing/ˌɔːtəʊˈbɒksɪŋ/ אוטובוקסינג
4.8

The ArrayList Toolbox · ⁨ערכת הכלים של ArrayList⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.8.A: Develop code for collections of related objects using ArrayList objects and determine the result of calling methods on these objects.

  • 4.8.A.1 An ArrayList object is mutable in size and contains object references.
  • 4.8.A.2 The ArrayList constructor ArrayList() constructs an empty list.
  • 4.8.A.3 Java allows the generic type ArrayList<E>, where the type parameter E specifies the type of the elements. When ArrayList<E> is specified, the types of the reference parameters and return type when using the ArrayList methods are type E. ArrayList<E> is preferred over ArrayList. For example, ArrayList<String> names = new ArrayList<String>(); allows the compiler to find errors that would otherwise be found at run-time.
  • 4.8.A.4 The ArrayList class is part of the java.util package. An import statement must be used to make this class available for use in the program.
  • 4.8.A.5 The following ArrayList methods—including what they do and when they are used—are part of the Java Quick Reference:
    • int size() returns the number of elements in the list.
    • boolean add(E obj) appends obj to end of list; returns true.
    • void add(int index, E obj) inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size.
    • E get(int index) returns the element at position index in the list.
    • E set(int index, E obj) replaces the element at position index with obj; returns the element formerly at position index.
    • E remove(int index) removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index.
  • 4.8.A.6 The indices for an ArrayList start at 0 and end at the number of elements - 1.
עברית

מטרת הלמידה 4.8.A: פיתוח קוד לקולקציות של אובייקטים קשורים באמצעות אובייקטי ArrayList וקביעת התוצאה של קריאת מתודות על אובייקטים אלו.

  • 4.8.A.1 אובייקט ArrayList הוא משתנה בגודלו ומכיל רעיונות לאובייקטים.
  • 4.8.A.2 בונה ArrayList ArrayList() בונה רשימה ריקה.
  • 4.8.A.3 Java מאפשרת את הסוג הגנרי ArrayList<E>, שבו הפרמטר E מצין את סוג האלמנטים. כאשר ArrayList<E> מצויין, סוגי פרמטרי הרעיון והסוג ההחזרתי בעת שימוש במתודות ArrayList הם סוג E. ArrayList<E> מועדף על פני ArrayList. לדוגמה, ArrayList<String> names = new ArrayList<String>(); מאפשר למורכב להציג שגיאות שהיו נמצאות רק בזמן הרצה.
  • 4.8.A.4 הקלאס ArrayList חלק ממארגון java.util. צריך להשתמש בהצהרה import כדי להפוך קלאס זה לזמין לשימוש בתוכנית.
  • 4.8.A.5 מתודות ArrayList הבאות—כולל מה הן עושות ומתי משתמשים בהן—חלק מההפניה המהירה ל-Java:
    • int size() מחזירה את מספר האלמנטים ברשימה.
    • boolean add(E obj) מוסיף obj בסוף הרשימה; מחזיר true.
    • void add(int index, E obj) מכניס obj במיקום index (0 <= index <= size), מזיז אלמנטים במיקום index ובגבוה יותר ימינה (מוסיף 1 למדגמאות שלהם) ומוסיף 1 לגודל.
    • E get(int index) מחזירה את האלמנט במיקום index ברשימה.
    • E set(int index, E obj) מחליף את האלמנט במיקום index ב-obj; מחזיר את האלמנט ששכן בעבר במיקום index.
    • E remove(int index) מסיר אלמנט ממיקום index, מזיז אלמנטים במיקום index + 1 ובגבוה יותר שמאלה (מפחית 1 מדגמאות שלהם) ומפחית 1 מגודל; מחזיר את האלמנט שהיה במקום index לפני כן.
  • 4.8.A.6 הדגמאות עבור ArrayList מתחילות ב-0 ומסתיימות במספר האלמנטים - 1.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
What an ArrayList really is

An ArrayList 动态数组 grows and shrinks as you add or remove items. Declare it with the element type in <>:

עברית
מהו reallyArrayList

ArrayList גדל וקטן כשמוסיפים או מורידים פריטים. הצהר אותו עם סוג האלמנט ב-<>:

ArrayList<String> names = new ArrayList<String>();
names.add("Amy");           // append
names.add(0, "Bob");        // insert at index
names.get(0);               // read
names.set(1, "Cara");       // replace
names.remove(0);            // delete, shifts the rest left
names.size();               // count (a method, unlike array.length)
4.9

Visiting Every Element of an ArrayList · ⁨סיור בכל רכיב של ArrayList⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.9.A: Develop code used to traverse the elements of an ArrayList and determine the results of these traversals.

  • 4.9.A.1 Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements in an ArrayList.
  • 4.9.A.2 Deleting elements during a traversal of an ArrayList requires the use of special techniques to avoid skipping elements.
  • 4.9.A.3 Attempting to access an index value outside of its range will result in an IndexOutOfBoundsException.
  • 4.9.A.4 Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.
עברית

מטרת למידה 4.9.A: פיתוח קוד לביצוע איטרציה על מילויי ArrayList וקביעת תוצאות האיטרציות הללו.

  • 4.9.A.1 ביצוע איטרציה על ArrayList מתרחש כאשר משתמשים בפקודות איטרציה או רקורסיביות כדי לגשת לכל המילויים או לסדרה מסודרת מהם ב-ArrayList.
  • 4.9.A.2 מחיקת מילויים במהלך איטרציה על ArrayList דורשת שימוש בטכניקות ייעודיות כדי למנוע דילוג על מילויים.
  • 4.9.A.3 ניסיון לגשת לערך אינדקס החוצה מתחום התקף שלו יוביל ל-IndexOutOfBoundsException.
  • 4.9.A.4 שינוי גודל של ArrayList במהלך איטרציה באמצעות לולאת for מוגברת עשויה להוביל ל-ConcurrentModificationException. לכן, בעת שימוש בלולאת for מוגברת לביצוע איטרציה על ArrayList, אין להוסיף או להסיר מילויים.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Traverse with an index loop or a for-each loop, just like arrays (use size() and get(i)):

Exam skill: when removing items in an index loop, either loop backwards or do not increment i after a removal – otherwise removing shifts elements left and you skip one. And never add or remove elements while traversing an ArrayList with a for-each loop: changing its size mid-loop throws a ConcurrentModificationException, so use an index loop (backwards, as above) whenever you must remove.

עברית

טרברס עם לולאת אינדקס או לולאת for-each, בדיוק כמו במערכות (שתמש ב-size() ו-get(i)):

for (int i = 0; i < list.size(); i++) { ... list.get(i) ... }
for (String s : list) { ... }

מיומנות לבחינה: כאשר מסיר פריטים בלולאת אינדקס, או שמלולאת הפוכה או שלא מעלים את i לאחר הרדה – אחרת ההסרה מזיזה רכיבים שמאלה ומדלגים על אחד. ואין להוסיף או להסיר רכיבים בזמן טרברסיה של ArrayList עם לולאת for-each: שינוי הגודל במהלך הלולאה מייצר ConcurrentModificationException, לכן השתמש בלולאת אינדקס (הפוכה, כפי שצוין לעיל) בכל פעם שיש להסיר.

4.10

Standard ArrayList Algorithms · ⁨אלגוריתמים סטנדרטיים של ArrayList⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.10.A: Develop code for standard and original algorithms for a particular context or specification that involve ArrayList objects and determine the result of these algorithms.

  • 4.10.A.1 There are standard ArrayList algorithms that utilize traversals to:
    • determine a minimum or maximum value
    • compute a sum or average
    • determine if at least one element has a particular property
    • determine if all elements have a particular property
    • determine the number of elements having a particular property
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements
    • shift or rotate elements left or right
    • reverse the order of the elements
    • insert elements
    • delete elements
  • 4.10.A.2 Some algorithms require multiple String, array, or ArrayList objects to be traversed simultaneously.
עברית

מטרות למידה 4.10.A: פיתוח קוד לאלגוריתמים סטנדרטיים וחדשניים עבור הקשר או מפרט מסוים המעורבים בArrayList אובייקטים והחלטת התוצאה של אלגוריתמים אלו.

  • 4.10.A.1 ישנם אלגוריתמים סטנדרטיים ArrayList המשתמשים במעברים כדי:
    • קביעת ערך מינימום או מקסימום
    • חישוב סכום או ממוצע
    • לקבוע אם לפחות אלמנט אחד בעל תכונה מסוימת
    • לקבוע אם לכל האלמנטים יש תכונה מסוימת
    • לקבוע את מספר האלמנטים שיש להם תכונה מסוימת
    • לגשת לכל הזוגות הרצופים של אלמנטים
    • לקבוע את נוכחותם או היעדרם של אלמנטים כפולים
    • להזיז או לסובב אלמנטים שמאלה או ימינה
    • להפוך את הסדר של האלמנטים
    • להכניס אלמנטים
    • למחוק אלמנטים
  • 4.10.A.2 חלק מהאלגוריתמים דורשים מעבר סימולטני על מספר String, אובייקט מארץ, או ArrayList.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

The same algorithms as arrays – max/min, count, sum – plus insertion and deletion that arrays cannot do easily. A common task is to remove all elements matching a condition, handling the index-shift carefully.

עברית

אותם אלגוריתמים כמו במערכות – מקסימום/מינימום, ספירה, סכום – בנוסף להוספה והסרה שאין ביצוען קל במערכות. משימה נפוצה היא להסיר את כל האלמנטים העומדים בתנאי מסוים, תוך טיפול זהיר בהזזת האינדקס.

4.11

Grids: Two-Dimensional Arrays · ⁨רשתות: מערכות דו-ממדיות⁩

Syllabus · ⁨סיילבוס⁩
Learning ObjectiveEssential Knowledge

4.11.A
Develop code used to represent collections of related data using two-dimensional (2D) array objects.

  • 4.11.A.1 A 2D array is stored as an array of arrays. Therefore, the way 2D arrays are created and indexed is similar to 1D array objects. The size of a 2D array is established at the time of creation and cannot be changed. 2D arrays can store either primitive data or object reference data.
    • Exclusion statement: Nonrectangular 2D array objects are outside the scope of the AP Computer Science A course and exam.
  • 4.11.A.2 When a 2D array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.
  • 4.11.A.3 The initializer list used to create and initialize a 2D array consists of initializer lists that represent 1D arrays; for example, int[][] arr2D = { {1, 2, 3}, {4, 5, 6} };.
  • 4.11.A.4 The square brackets [row][col] are used to access and modify an element in a 2D array. For the purposes of the exam, when accessing the element at arr[first][second], the first index is used for rows, the second index is used for columns.
  • 4.11.A.5 A single array that is a row of a 2D array can be accessed using the 2D array name and a single set of square brackets containing the row index.
  • 4.11.A.6 The number of rows contained in a 2D array can be accessed through the length attribute. The valid row index values for a 2D array are 0 through one less than the number of rows or the length of the array, inclusive. The number of columns contained in a 2D array can be accessed through the length attribute of one of the rows. The valid column index values for a 2D array are 0 through one less than the number of columns or the length of any given row of the array, inclusive. For example, given a 2D array named values, the number of rows is values.length and the number of columns is values[0].length. Using an index value outside of these ranges will result in an ArrayIndexOutOfBoundsException.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

A 2D array 二维数组 is a grid (rows and columns) – an array of arrays:

עברית

מערכת 2D היא רשת (שורות ועמודות) – מערכת של מערכות:

מערכת דו-ממדית (טבלה) עם אינדקסים לשורה ועמודה
מערכת דו-ממדית (טבלה) עם אינדקסים לשורה ועמודה
int[][] grid = new int[3][4];   // 3 rows, 4 columns
grid[r][c] = 7;                 // row r, column c
int rows = grid.length;         // 3
int cols = grid[0].length;      // 4
Explore · ⁨חקור⁩

Index a 2D array by row and column · ⁨מדד מערך דו-ממדי 2 לפי שורה ועמודה⁩

A 2D array is a grid addressed by [row][col]. Move the indices and watch which cell they select — row first, then column, both counting from 0. · ⁨מערך D-2 הוא רשת המיועדת ב[row][col]. הזז את האינדקסים וצפה איזה תא הם בוחרים — שורה קודם, ואז עמודה, שניהם סופרים מ0.⁩

4.12

Walking Through a Grid · ⁨הליכה דרך רשת⁩

Syllabus · ⁨סיילבוס⁩
Learning ObjectiveEssential Knowledge

4.12.A
Develop code used to traverse the elements in a 2D array and determine the result of these traversals.

  • 4.12.A.1 Nested iteration statements are used to traverse and access all or an ordered sequence of elements in a 2D array. Since 2D arrays are stored as arrays of arrays, the way 2D arrays are traversed using for loops and enhanced for loops is similar to 1D array objects. Nested iteration statements can be written to traverse the 2D array in row-major order, column-major order, or a uniquely defined order. Row-major order refers to an ordering of 2D array elements where traversal occurs across each row, whereas column-major order traversal occurs down each column.
  • 4.12.A.2 The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows. Therefore, the enhanced for loop variable must be the type of each row, which is a 1D array. The inner loop traverses a single row. Therefore, the inner enhanced for loop variable must be the same type as the elements stored in the 1D array. Assigning a new value to the enhanced for loop variable does not change the value stored in the array.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
Traversing a 2-D array

Visit every cell with nested loops – the outer over rows, the inner over columns (row-major order 行主序):

עברית
עבודה על מערך 2-D

ביקור בכל תא באמצעות לולאות פנימיות – החיצונית על שורות, הפנימית על עמודות (סדר טור-עיקרי):

for (int r = 0; r < grid.length; r++)
    for (int c = 0; c < grid[0].length; c++)
        System.out.print(grid[r][c]);
4.13

Standard 2D Array Algorithms · ⁨אלגוריתמי מערך דו-ממדי ⟨2⟩ סטנדרטיים⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.13.A: Develop code for standard and original algorithms for a particular context or specification that involves 2D arrays and determine the result of these algorithms.

  • 4.13.A.1 There are standard algorithms that utilize 2D array traversals to:
    • determine a minimum or maximum value of all the elements or for a designated row, column, or other subsection
    • compute a sum or average of all the elements or for a designated row, column, or other subsection
    • determine if at least one element has a particular property in the entire 2D array or for a designated row, column, or other subsection
    • determine if all elements of the 2D array or a designated row, column, or other subsection have a particular property
    • determine the number of elements in the 2D array or in a designated row, column, or other subsection having a particular property
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection
    • shift or rotate elements in a row left or right or in a column up or down
    • reverse the order of the elements in a row or column
עברית

מטרת למידה 4.13.A: פיתוח קוד לאלגוריתמים סטנדרטיים וחדשניים בהקשר או מפרט מסוים המשתמש במערכים בעל-ממד 2 וקביעת תוצאת האלגוריתמים הללו.

  • 4.13.A.1 קיימים אלגוריתמים סטנדרטיים המשתמשים במעבר (traversal) על מערכים בעל-ממד 2 כדי:
    • לקבוע ערך מינימום או מקסימום של כל האלמנטים או עבור שורה, עמודה או תת-אזור מוגדר אחר
    • לחשב סכום או ממוצע של כל האלמנטים או עבור שורה, עמודה או תת-אזור מוגדר אחר
    • לקבוע אם לפחות אלמנט אחד בעל מאפיין מסוים בכל המערכת בעל-ממד 2 או בשורה, עמודה או תת-חלק אחר ממוינת
    • לקבוע אם כל האלמנטים במערכת בעל-ממד 2 או בשורה, עמודה או תת-חלק אחר ממוינים הם בעלי מאפיין מסוים
    • לקבוע את מספר האלמנטים במערכת בעל-ממד 2 או בשורה, עמודה או תת-חלק אחר ממוינים המצוינים במאפיין מסוים
    • לגשת לכל הזוגות הרצופים של אלמנטים
    • לקבוע את נוכחותם או היעדרם של אלמנטים כפולים במערכת בעל-ממד 2 או בשורה, עמודה או תת-חלק אחר ממוינים
    • להזיז או לסובב אלמנטים בשורה שמאלה או ימינה או בעמודה למעלה או למטה
    • להפוך את סדר האלמנטים בשורה או בעמודה

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English

Typical grid tasks: sum a row or column, find the max in the grid, count matching cells, or sum a diagonal (where r == c). Each is a nested traversal with a running result.

עברית

משימות רשת נפוצות: חיבור שורה או עמודה, מציאת הערך המקסימום ברשת, ספירת תאים תואמים, או חיבור אלכסון (כאן r == c). כל אחת מהן היא טרברס פנימי עם תוצאה מצטברת.

4.14

Finding a Value: Linear and Binary Search · ⁨מציאת ערך: חיפוש ליני ואינסולטיבי⁩

Syllabus · ⁨סיילבוס⁩
Learning ObjectiveEssential Knowledge

4.14.A
Develop code used for linear search algorithms to search for specific information in a collection and determine the results of executing a search.

  • 4.14.A.1 Linear search algorithms are standard algorithms that check each element in order until the desired value is found or all elements in the array or ArrayList have been checked. Linear search algorithms can begin the search process from either end of the array or ArrayList.
  • 4.14.A.2 When applying linear search algorithms to 2D arrays, each row must be accessed then linear search applied to each row of the 2D array.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
Binary search: halve and conquer
  • Linear search 线性搜索 checks each element in turn – works on any list, taking up to $n$ steps.
  • Binary search 二分搜索 works only on a sorted list: check the middle, then discard the half that cannot contain the target, repeating. It takes about $\log_2 n$ steps – far faster on large data.

Exam skill: binary search requires sorted data; know how many comparisons it makes and how lo, hi, mid update.

Worked example. Search for target = 40 in the sorted array {3, 9, 14, 23, 31, 42, 55} (indices 0–6). Start lo=0, hi=6:

  • mid = (0+6)/2 = 3, a[3]=23 < 40, so lo = 4;
  • mid = (4+6)/2 = 5, a[5]=42 > 40, so hi = 4;
  • mid = (4+4)/2 = 4, a[4]=31 < 40, so lo = 5;
  • now lo (5) > hi (4), so the loop ends – 40 is not present.

Each step halved the range, so even this miss took only three comparisons.

עברית
חיפוש בינארי: חלוקה לשתיים וכיבוש
  • חיפוש ליני בודק כל אלמנט בסדר – עובד על כל רשימה, לוקח עד $n$ צעדים.
  • חיפוש אינסולטיבי עובד רק על רשימה מוסדרת: בודק את האמצע, ולאחר מכן מתעלם מחצית שאינה יכולה להכיל את המטרה, ומשחזר. לוקח כ-$\log_2 n$ צעדים – הרבה מהיר יותר על נתונים גדולים.
חיפוש אינסולטיבי חוצה את הטווח במחצית בכל צעד
חיפוש אינסולטיבי חוצה את הטווח במחצית בכל צעד
חיפוש ליני בודק כל אלמנט בסדר עד שמצוי המטרה
חיפוש ליני בודק כל אלמנט בסדר עד שמצוי המטרה
int lo = 0, hi = a.length - 1;
while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (a[mid] == target) return mid;
    else if (a[mid] < target) lo = mid + 1;
    else hi = mid - 1;
}

מיומנות לבחינה: חיפוש אינסולטיבי דורש נתונים מסודרים; יש לדעת כמה השוואות הוא מבצע וכיצב lo, hi, mid מתעדכנים.

דוגמה מפורטת. חפש את target = 40 ברשימה המוסדרת {3, 9, 14, 23, 31, 42, 55} (אינדקסים 0–6). התחל ב-lo=0, hi=6:

  • mid = (0+6)/2 = 3, a[3]=23 < 40, ולכן lo = 4;
  • mid = (4+6)/2 = 5, a[5]=42 > 40, ולכן hi = 4;
  • mid = (4+4)/2 = 4, a[4]=31 < 40, ולכן lo = 5;
  • עכשיו lo (5) > hi (4), ולכן הלולאה נגמרת – 40 לא קיים.

בכל צעד הטווח הועבר במחצית, ולכן גם כשל זה לקח רק שלוש השוואות.

Explore · ⁨חקור⁩

Compare linear and binary search · ⁨השוו חיפוש ליניארי לחיפוש בינארי⁩

Linear search checks every element in turn; binary search halves a sorted list each step. Watch binary search reach the target in far fewer comparisons. · ⁨חיפוש ליניארי בודק כל אלמנט בתורו; חיפוש בינארי מחצית רשימה מוערכת בכל צעד. צפו כיצד החיפוש הבינארי מגיע למטרה במספר השוואות פחות משמעותי.⁩

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
Linear search/ˈlɪnɪə sɜːtʃ/ חיפוש ליניארי
Binary search/ˈbaɪnəri sɜːtʃ/ חיפוש ביינארי
Selection sort/sɪˈlekʃn sɔːt/ מיון בחירה
4.15

Putting Data in Order: Selection and Insertion Sort · ⁨סידור נתונים: מיון בחירה ומיון הכנסה⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.15.A: Determine the result of executing each step of sorting algorithms to sort the elements of a collection.

  • 4.15.A.1 Selection sort and insertion sort are iterative sorting algorithms that can be used to sort elements in an array or ArrayList.
  • 4.15.A.2 Selection sort repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it into its correct (and final) position in the sorted portion of the list.
  • 4.15.A.3 Insertion sort inserts an element from the unsorted portion of a list into its correct (but not necessarily final) position in the sorted portion of the list by shifting elements of the sorted portion to make room for the new element.
עברית

מטרות למידה 4.15.A: לקבוע את התוצאה של ביצוע כל שלב באלגוריתמי מיון כדי למיין את אלמנטיה של אוסף.

  • 4.15.A.1 מיון בחירה ומיון הכנסה הם אלגוריתמי מיון איטרטיביים (איטרטיביים) שניתן להשתמש בהם למיין אלמנטים במערך או ArrayList.
  • 4.15.A.2 מיון בחירה בוחר באופן חוזר את האלמנט הקטן ביותר (או הגדול ביותר) מהחלק הלא-מומין של הרשימה ומחליף אותו למיקומו הנכון (וגם הסופי) בחלק המומין של הרשימה.
  • 4.15.A.3 מיון הכנסה מכניס אלמנט מהחלק הלא-מומין של רשימה למיקומו הנכון (אולם לא נרחב הסופי) בחלק המומין של הרשימה על ידי הזזת אלמנטים מהחלק המומין כדי לפנות מקום לאלמנט החדש.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
Insertion sort
Bubble sort, pass by pass
  • Selection sort 选择排序 repeatedly finds the smallest remaining element and swaps it into place.
  • Insertion sort 插入排序 grows a sorted front, inserting each new element where it belongs.

Both are simple and take about $n^2$ steps on average – fine for small arrays. Be able to trace the array after each pass.

עברית
מיון הכנסה
מיון בועות, מעבר אחר מעבר
  • מיון בחירה מוצא שוב ושוב את הקטן ביותר שנותר ומחליף אותו למקומו.
  • מיון הכנסה בונה חזית מסודרת, ומכניס כל אלמנט חדש למיקומו הנכון.
מיון הכנסה, המעביר כל מפתח למקומו מעבר לעבר
מיון הכנסה, המעביר כל מפתח למקומו מעבר לעבר

שניהם פשוטים ודורשים כ-$n^2$ צעדים בממוצע – מתאימים לאריי קטנים. יש להצליח לעקוב אחרי האריי לאחר כל מעבר.

Explore · ⁨חקור⁩

Watch a sorting algorithm order a list · ⁨צפו אלגוריתם מיון מסדר רשימה⁩

A sort rearranges elements into order. Step through selection/insertion sort to see the sorted region grow one element at a time. · ⁨מיון מסדר מחדש אלמנטים לסדר. צעדו לאורך מיון בחירה/הכנסה כדי לראות את האזור הממוין גדל אלמנט אחד בכל פעם.⁩

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
Insertion sort/ɪnˈsɜːʃn sɔːt/ מיון הכנסה
4.16

Methods That Call Themselves: Recursion · ⁨שיטות הקוראות את עצמן: רקורסיה⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.16.A: Determine the result of calling recursive methods.

  • 4.16.A.1 A recursive method is a method that calls itself. Recursive methods contain at least one base case, which halts the recursion, and at least one recursive call. Recursion is another form of repetition.
  • 4.16.A.2 Each recursive call has its own set of local variables, including the parameters. Parameter values capture the progress of a recursive process, much like loop control variable values capture the progress of a loop.
  • 4.16.A.3 Any recursive solution can be replicated through the use of an iterative approach and vice versa.
    • Exclusion statement: Writing recursive code is outside the scope of the AP Computer Science A course and exam.
עברית

מטרות למידה 4.16.A: לקבוע את התוצאה של קריאת מתודות רקורסיביות.

  • 4.16.A.1 מתודה רקורסיבית היא מתודה הנקראת לעצמה. מתודות רקורסיביות מכילות לפחות מקרה בסיס אחד, המעצור את הרקורסיה, ולפחות קריאה רקורסיבית אחת. רקורסיה היא צורה אחרת של חזרה.
  • 4.16.A.2 לכל קריאה רקורסיבית ישנו ערך עצמי של משתנים מקומיים, כולל הפרמטרים. ערכי הפרמטרים תופסים את התקדמות תהליך רקורסיבי, בדומה לכך שערכי משתנה בקרת מחזור תופסים את התקדמות מחזור.
  • 4.16.A.3 כל פתרון רקורסיבי ניתן לשחזר באמצעות גישת איטרציה ולהפך.
    • הצהרת יציאה: כתיבת קוד רקורסיבי אינה בטווח הלימודים של קורס ובחינת AP Computer Science A.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
Recursion & the call stack

Recursion 递归 is a method that calls itself on a smaller input. It needs a base case 基本情况 that stops the calls, and a recursive case that moves toward the base:

Without a reachable base case, recursion never stops (a stack overflow).

Recursion and iteration are interchangeable. Any recursive solution can be rewritten with a loop (an iterative approach), and any loop can be rewritten with recursion - they solve the same problems. The factorial above is identical in effect to an iterative version:

So the choice is about clarity, not capability: recursion reads naturally for problems with a self-similar structure (trees, merge sort), while iteration avoids the memory cost of stacking a call frame per step. The exam may ask you to convert one into the other.

עברית
רקורסיה ותור הפקודות

רקורסיה היא שיטה הקוראת את עצמה על קלט קטן יותר. היא דורשת מקרה בסיס שמפסיק את ההתקשרויות, ומקרה רקורסיבי שמקרב אותן למקרה הבסיס:

public static int factorial(int n) {
    if (n <= 1) return 1;          // base case
    return n * factorial(n - 1);   // recursive case
}

ללא מקרה בסיס הניתן להישג, הרקורסיה לעולם אינה נפסקת (שפל עומס).

רקורסיה ואיטרציה הם חלופיים. כל פתרון רקורסיבי ניתן לשינוי באמצעות לולאה (גישה איטרטיבית), וכל לולאה ניתנת לשינוי ברקורסיה – הן פותרות את אותם בעיות. ה-factorial למעלה זהה בהשפעה לגרסה איטרטיבית:

public static int factorial(int n) {
    int result = 1;
    for (int i = 2; i <= n; i++) result *= i;   // same answer, no self-call
    return result;
}

לכן הבחירה היא בנוגע ל-בהירות, לא ליכולת: רקורסיה נקראת טבעית לבעיות עם מבנה דומה-לעצמו (עצים, מיזוג), בעוד שאיטרציה נמנעת מהעלות הזיכרון של ערימת מסגרת קריאה בכל צעד. במבחן ייתכן שישאלו להמיר אחת לשנייה.

Explore · ⁨חקור⁩

Unfold a recursive call · ⁨פרקו קריאה רקורסיבית⁩

A recursive method calls itself on a smaller input until it hits a base case, then the results fold back up. Step through to watch the calls stack and unwind. · ⁨מתודה רקורסיבית קוראת לעצמה על קלט קטן יותר עד שהיא מגיעה למקרה בסיס, ולאחר מכן התוצאות מתקפלות חזרה. צעדו כדי לצפות בערימת הקריאות ובביטולן.⁩

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
Recursion/rɪˈkɜːʃn/ רקורסיה
base case/beɪs keɪs/ מקרה בסיסי
4.17

Recursive Search and Merge Sort · ⁨חיפוש רקורסיבי ומיון מיזוג⁩

Syllabus · ⁨סיילבוס⁩
English

Learning Objective 4.17.A: Determine the result of executing recursive algorithms that use strings or collections.

  • 4.17.A.1 Recursion can be used to traverse String objects, arrays, and ArrayList objects.

Learning Objective 4.17.B: Determine the result of each iteration of a binary search algorithm used to search for information in a collection.

  • 4.17.B.1 Data must be in sorted order to use the binary search algorithm. Binary search starts at the middle of a sorted array or ArrayList and eliminates half of the array or ArrayList in each recursive call until the desired value is found or all elements have been eliminated.
  • 4.17.B.2 Binary search is typically more efficient than linear search.
    • Exclusion statement: Search algorithms other than linear and binary search are outside the scope of the AP Computer Science A course and exam.
  • 4.17.B.3 The binary search algorithm can be written either iteratively or recursively.

Learning Objective 4.17.C: Determine the result of each iteration of the merge sort algorithm when used to sort a collection.

  • 4.17.C.1 Merge sort is a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.
    • Exclusion statement: Sorting algorithms other than selection, insertion, and merge sort are outside the scope of the AP Computer Science A course and exam.
  • 4.17.C.2 Merge sort repeatedly divides an array into smaller subarrays until each subarray is one element and then recursively merges the sorted subarrays back together in sorted order to form the final sorted array.
עברית

מטרות למידה 4.17.A: לקבוע את התוצאה של ביצוע אלגוריתמים רקורסיביים המשתמשים במילים או באוספים.

  • 4.17.A.1 ניתן להשתמש ברקורסיה כדי לעבור על String אובייקטים, מערכים וArrayList אובייקטים.

מטרות למידה 4.17.B: לקבוע את התוצאה של כל איטרציה של אלגוריתם חיפוש בינארי המשמש לחיפוש מידע באוסף.

  • 4.17.B.1 נתונים חייבים להיות במיון כדי להשתמש באלגוריתם חיפוש בינארי. חיפוש בינארי מתחיל באמצע מערך ממוין או ArrayList ומסיר חצי מהמערך או ArrayList בכל קריאה רקורסיבית עד שמצאים את הערך הרצוי או שנכלו כל האלמנטים.
  • 4.17.B.2 חיפוש בינארי הוא בדרך כלל יעיל יותר מחיפוש ליניארי.
    • הצהרת יציאה: אלגוריתמי חיפוש אחרים מחיפוש ליניארי ובינארי אינם בטווח הלימודים של קורס ובחינת AP Computer Science A.
  • 4.17.B.3 אלגוריתם החיפוש הבינארי יכול להיות מוגדר באופן איטרטיבי או רקורסיבי.

מטרת הלמידה 4.17.C: לקבוע את התוצאה של כל איטרציה של אלגוריתם ה-Merge Sort בעת מיון קבוצת נתונים.

  • 4.17.C.1 Mergesort הוא אלגוריתם מיון רקורסיבי שיכול לשמש למיון אלמנטים במערכת או במערכת בעל-ממד ArrayList.
    • הערה על היקף: אלגוריתמי מיון אחרים מאלו שנקראו (Selection Sort, Insertion Sort, Merge Sort) אינם נכללים בקורס ובמבחן AP Computer Science A.
  • 4.17.C.2 Merge Sort מחלק באופן חוזר מחדש את המערך לזרים קטנים יותר עד שכל זר מכיל אלמנט אחד בלבד, ולאחר מכן מאחד רקורסיבית את הזרים הממוינים יחד בסדר ממוין כדי ליצור את המערך הסופי הממוין.

Source: College Board AP Course and Exam Description · ⁨מקור: תיאור הקורס והמבחן של College Board AP⁩

English
Merge sort: split, then merge

Recursion powers efficient algorithms. Binary search can be written recursively (search the correct half). Merge sort 归并排序 splits the array in half, sorts each half recursively, then merges the two sorted halves – taking about $n\log_2 n$ steps, much faster than selection or insertion sort on large data.

Worked example. Trace factorial(4). Each call defers to a smaller one: factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1). factorial(1) hits the base case and returns 1, so the calls unwind inward: 2 * 1 = 2, then 3 * 2 = 6, then 4 * 6 = 24. Writing each call above its returned value is the reliable way to trace recursion.

Exam skill: trace a recursive method by writing out each call and its return value, and know that merge sort's efficiency ($n\log n$) beats the $n^2$ simple sorts.

עברית
מיזוג-סידור: פיצול, ואז מיזוג

רקורסיה מניעה אלגוריתמים יעילים. חיפוש בינארי ניתן לכתיבה רקורסיבית (חיפוש בחצי הנכון). מיזוג מחלק את האריי לחציים, ממין כל חצי רקורסיבית, ולאחר מכן ממזג את שני החצאים הממוינים – תוך כ-$n\log_2 n$ צעדים, מהיר משמעותית מאיחוי או מיון הכנסה על נתונים גדולים.

מיזוג מחלק את האריי לאלמנטים בודדים, ולאחר מכן ממזג חצאים ממוינים חזרה כלפי מעלה
מיזוג מחלק את האריי לאלמנטים בודדים, ולאחר מכן ממזג חצאים ממוינים חזרה כלפי מעלה

דוגמה פעילה. עקוב אחרי factorial(4). כל קריאה מעוכבת לקריאה קטנה יותר: factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1). factorial(1) פוגש את המקרה הבסיס ומחזיר 1, ולכן הקריאות מתפרקות פנימה: 2 * 1 = 2, ולאחר מכן 3 * 2 = 6, ולאחר מכן 4 * 6 = 24. כתיבת כל קריאה מעל הערך שהוחזר היא הדרך האמינה לעקוב אחר רקורסיה.

מיומנות במבחן: עקוב אחר שיטה רקורסיבית על ידי כתיבת כל קריאה וערך ההחזרה שלה, ודע כי יעילות המיזוג ($n\log n$) עולה על סיווג פשוטים כמו $n^2$.

Vocabulary · ⁨מילון מונחים⁩ Train · ⁨אימון⁩
English עברית
Merge sort/mɜːdʒ sɔːt/ מיון מיזוג
4.17

Exam tips · ⁨טיפים לבחינות⁩

English
  • Weigh both benefits and harms of collecting data — this unit is tested through short written justification, not code.
  • Protect personally identifiable information (PII) and explain privacy and security risks in context.
  • Name real harms: data breaches, surveillance, and algorithmic bias from unrepresentative data.
  • Respect intellectual property and licensing when you reuse code or data.
  • Give a specific, reasoned answer — a vague "it could be bad" earns no marks.
עברית
  • שקול את היתרונות והנזקים של איסוף נתונים – יחידה זו נבדקת דרך נימוק כתוב קצר, לא קוד.
  • הגן על מידע זיהוי אישי (PII) והסבר סיכוני פרטיות ואבטחה בהקשר.
  • ציין נזקים אמיתיים: הפרות נתונים, ניטור, והטיה אלגוריתמית מהנתונים שאינם מייצגים.
  • כבד ברוח הרוחנית וברישויים כאשר אתה מוסיף קוד או נתונים.
  • תן תשובה ספציפית ומנומקת – "ייתכן שזה רע" הוא תשובה מטושטשת שאינה זוכה לנקודות.

Interactive lessons on this topic · ⁨שיעורים אינטראקטיביים בנושא זה⁩

Work through it step by step, with instant-check exercises. · ⁨לעבור על הדברים צעד אחר צעד, עם תרגילים לבדיקה מיידית.⁩

Past Papers · ⁨מבחני עבר⁩

More topics in AP Computer Science A · ⁨מדעי מחשב A - AP⁩ · ⁨נושאים נוספים בAP Computer Science A · ⁨מדעי מחשב A - AP⁩⁩

Log in or create account · ⁨היכנס או צור חשבון⁩

IGCSE, A-Level & AP