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

JavaJava
9
חלקPart 2: מתודות ומערכיםMethods & arrays כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

ArrayList ו-HashMap ראשוניםFirst ArrayList & HashMap

רשימה שגדלה לבד, מילון של מפתח וערך, טיפוסים עטופים (Integer) ו-autoboxing.

A list that grows by itself, a key-value map, wrapper types (Integer) and autoboxing.

נסו לפתור לבד לפני שמציצים. בנגן: חיצים במקלדת או הכפתורים. כל צעד מראה איפה (קובץ ושורה), מה כותבים ולמה.Try on your own before peeking. In the player use the arrow keys or the buttons. Each step shows where (file and line), what to write, and why.
תרגילExercise 1

רשימת קניותShopping list

תזכורת: מה ביקשוReminder: the task

צרו ArrayList של מחרוזות, הוסיפו milk, bread, eggs, מחקו את bread, והדפיסו את הרשימה ואת הגודל: [milk, eggs] 2.

Create an ArrayList of strings, add milk, bread, eggs, remove bread, and print the list and its size: [milk, eggs] 2.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("milk");
        list.add("bread");
        list.add("eggs");
        list.remove("bread");
        System.out.println(list + " " + list.size());
    }
}
פלטOutput
[milk, eggs] 2

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 2

רק הזוגייםOnly the evens

תזכורת: מה ביקשוReminder: the task

כתבו static List<Integer> evens(List<Integer> nums) שמחזירה רשימה חדשה עם הזוגיים בלבד. הדפיסו את evens(List.of(3, 8, 5, 12, 6)): [8, 12, 6].

Write static List<Integer> evens(List<Integer> nums) returning a new list with only the evens. Print evens(List.of(3, 8, 5, 12, 6)): [8, 12, 6].

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static List<Integer> evens(List<Integer> nums) {
        List<Integer> out = new ArrayList<>();
        for (int n : nums) {
            if (n % 2 == 0) {
                out.add(n);
            }
        }
        return out;
    }

    public static void main(String[] args) {
        System.out.println(evens(List.of(3, 8, 5, 12, 6)));
    }
}
פלטOutput
[8, 12, 6]

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 3

ספר טלפוניםPhone book

תזכורת: מה ביקשוReminder: the task

צרו HashMap<String, String> עם שני אנשי קשר (Noa → 050-1111111, Avi → 052-2222222). עדכנו את המספר של Noa ל-050-9999999, והדפיסו את המספר של Noa ואת getOrDefault("Dana", "unknown").

Create a HashMap<String, String> with two contacts (Noa → 050-1111111, Avi → 052-2222222). Update Noa's number to 050-9999999, and print Noa's number and getOrDefault("Dana", "unknown").

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> phones = new HashMap<>();
        phones.put("Noa", "050-1111111");
        phones.put("Avi", "052-2222222");
        phones.put("Noa", "050-9999999");
        System.out.println(phones.get("Noa"));
        System.out.println(phones.getOrDefault("Dana", "unknown"));
    }
}
פלטOutput
050-9999999
unknown

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 4

ספירת אותיותCounting letters

תזכורת: מה ביקשוReminder: the task

ספרו כמה פעמים מופיעה כל אות במילה "banana" בעזרת TreeMap<Character, Integer>, והדפיסו את המילון: {a=3, b=1, n=2}.

Count how many times each letter appears in "banana" with a TreeMap<Character, Integer>, and print the map: {a=3, b=1, n=2}.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        String word = "banana";
        Map<Character, Integer> counts = new TreeMap<>();
        for (char c : word.toCharArray()) {
            counts.put(c, counts.getOrDefault(c, 0) + 1);
        }
        System.out.println(counts);
    }
}
פלטOutput
{a=3, b=1, n=2}

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 5

צייד באגים: מחיקה תוך כדי לולאהBug hunter: removing while looping

תזכורת: מה ביקשוReminder: the task

הקוד מנסה למחוק מהרשימה את כל המספרים השליליים, וקורס. צפו ב"איפה זה נשבר", ותקנו עם removeIf כך שיודפס [4, 7].

The code tries to remove every negative number from the list, and crashes. Watch "where it breaks", and fix it with removeIf so it prints [4, 7].

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(4, -2, 7, -5));
        nums.removeIf(n -> n < 0);
        System.out.println(nums);
    }
}
פלטOutput
[4, 7]

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 6

מדף, צעד 9: רשימה וספירה לפי סופרShelf, step 9: a list and a count per author

תזכורת: מה ביקשוReminder: the task

שמרו את הספרים ב-List<String> בפורמט Title|Author: Dune|Herbert, Emma|Austen, Persuasion|Austen, Children of Dune|Herbert, Ulysses|Joyce. ספרו ב-TreeMap כמה ספרים יש לכל סופר והדפיסו Austen: 2 וכן הלאה, ובסוף Total: 5.

Keep the books in a List<String> as Title|Author: Dune|Herbert, Emma|Austen, Persuasion|Austen, Children of Dune|Herbert, Ulysses|Joyce. Count with a TreeMap how many books each author has and print Austen: 2 and so on, then Total: 5.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        List<String> books = List.of("Dune|Herbert", "Emma|Austen", "Persuasion|Austen", "Children of Dune|Herbert", "Ulysses|Joyce");
        Map<String, Integer> byAuthor = new TreeMap<>();
        for (String b : books) {
            String author = b.substring(b.indexOf("|") + 1);
            byAuthor.put(author, byAuthor.getOrDefault(author, 0) + 1);
        }
        for (Map.Entry<String, Integer> e : byAuthor.entrySet()) {
            System.out.println(e.getKey() + ": " + e.getValue());
        }
        System.out.println("Total: " + books.size());
    }
}
פלטOutput
Austen: 2
Herbert: 2
Joyce: 1
Total: 5

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 1

הכפול הראשוןThe first repeat

תזכורת: מה ביקשוReminder: the task

כתבו static String firstRepeat(List<String> words) שמחזירה את המילה הראשונה שמופיעה בפעם השנייה (או none). עבור [red, blue, green, blue, red] היא blue. השתמשו ב-HashSet (add מחזיר false אם כבר היה).

Write static String firstRepeat(List<String> words) returning the first word that shows up a second time (or none). For [red, blue, green, blue, red] it's blue. Use a HashSet (add returns false if it was already there).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    static String firstRepeat(List<String> words) {
        Set<String> seen = new HashSet<>();
        for (String w : words) {
            if (!seen.add(w)) {
                return w;
            }
        }
        return "none";
    }

    public static void main(String[] args) {
        System.out.println(firstRepeat(List.of("red", "blue", "green", "blue", "red")));
    }
}
פלטOutput
blue

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 2

ציון ממוצע לכל תלמידAverage grade per student

תזכורת: מה ביקשוReminder: the task

מילון Map<String, List<Integer>> שממפה תלמיד לרשימת ציונים: Noa → 90, 80; Avi → 70, 75, 95. הדפיסו לכל תלמיד (לפי א-ב) Avi: 80.0 ו-Noa: 85.0.

A Map<String, List<Integer>> mapping a student to a list of grades: Noa → 90, 80; Avi → 70, 75, 95. Print for each student (alphabetically) Avi: 80.0 and Noa: 85.0.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

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

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, List<Integer>> grades = new TreeMap<>();
        grades.put("Noa", List.of(90, 80));
        grades.put("Avi", List.of(70, 75, 95));
        for (String name : grades.keySet()) {
            int sum = 0;
            for (int g : grades.get(name)) {
                sum += g;
            }
            System.out.println(name + ": " + (double) sum / grades.get(name).size());
        }
    }
}
פלטOutput
Avi: 80.0
Noa: 85.0

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

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