נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
ArrayList ו-HashMap ראשוניםFirst ArrayList & HashMap
מפגש 9: רשימה שגדלה לבד, מילון של מפתח וערך, טיפוסים עטופים (Integer) ו-autoboxing.Session 9: A list that grows by itself, a key-value map, wrapper types (Integer) and autoboxing.
רשימה שגדלה לבד, מילון של מפתח וערך, טיפוסים עטופים (Integer) ו-autoboxing. בסוף המפגש מוסיפים צעד לפרויקט המלווה.A list that grows by itself, a key-value map, wrapper types (Integer) and autoboxing. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- ArrayList: רשימה שגדלהArrayList: a list that grows
- רשימה של מספרים: Integer ו-autoboxingA list of numbers: Integer and autoboxing
- רשימה היא עצם: שתי הפניותA list is an object: two references
- HashMap: מפתח וערךHashMap: key and value
- ספירה עם מילוןCounting with a map
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- ArrayList: רשימה שגדלה לבד:ArrayList: A list that grows by itself:
- HashMap: מילון של מפתח וערך:HashMap: A key-value map:
- מפתח וערך: במילון כל מפתח מופיע פעם אחת, ולכל מפתח ערך אחד.key and value: In a map each key appears once, with one value.
- סוגריים משולשים <>: אומרים מה יש בתוך האוסף:angle brackets <>: They say what's inside the collection:
- טיפוס עוטף: גרסת עצם של טיפוס פרימיטיבי:wrapper type: The object version of a primitive type:
- autoboxing: Java הופכת לבד
int ל-Integer כשמכניסים לאוסף, ובחזרה כשמוציאים.autoboxing: Java turns an int into an Integer by itself when you put it in a collection, and back when you take it out.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
ArrayList: רשימה שגדלהArrayList: a list that grows
ArrayList: רשימה שגדלהArrayList: a list that grows
- בניגוד למערך, ל-ArrayList אין גודל קבוע.Unlike an array, an ArrayList has no fixed size.
add מוסיף לסוף, get(i) קורא, set מחליף, remove מוחק (והשאר זזים).add appends, get(i) reads, set replaces, remove deletes (and the rest shift).- הטיפוס בתוך
<>.The type goes inside <>.
בניגוד למערך, ל-ArrayList אין גודל קבוע. add מוסיף לסוף, get(i) קורא, set מחליף, remove מוחק (והשאר זזים). הטיפוס בתוך <>.Unlike an array, an ArrayList has no fixed size. add appends, get(i) reads, set replaces, remove deletes (and the rest shift). The type goes inside <>.נושאTopic 3
רשימה של מספרים: Integer ו-autoboxingA list of numbers: Integer and autoboxing
רשימה של מספרים: Integer ו-autoboxingA list of numbers: Integer and autoboxing
- אוספים מחזיקים רק עצמים, ולכן כותבים
List<Integer>.Collections hold only objects, so you write List<Integer>. - autoboxing: Java ממירה בין
int ל-Integer לבד.Autoboxing: Java converts between int and Integer by itself.
אוספים מחזיקים רק עצמים, ולכן כותבים List<Integer>. autoboxing: Java ממירה בין int ל-Integer לבד.Collections hold only objects, so you write List<Integer>. Autoboxing: Java converts between int and Integer by itself.נושאTopic 4
רשימה היא עצם: שתי הפניותA list is an object: two references
רשימה היא עצם: שתי הפניותA list is an object: two references
- כמו במערך,
same = names לא מעתיק את הרשימה: שתי הפניות לאותו עצם.As with arrays, same = names doesn't copy the list: two references to the same object. - בזיכרון רואים רשימה אחת ושני חצים.In memory you see one list and two arrows.
כמו במערך, same = names לא מעתיק את הרשימה: שתי הפניות לאותו עצם. בזיכרון רואים רשימה אחת ושני חצים.As with arrays, same = names doesn't copy the list: two references to the same object. In memory you see one list and two arrows.נושאTopic 5
HashMap: מפתח וערךHashMap: key and value
HashMap: מפתח וערךHashMap: key and value
- HashMap ממפה מפתח לערך.A HashMap maps a key to a value.
put מוסיף או מחליף, get מחזיר (או null אם אין), ו-getOrDefault נותן ערך ברירת מחדל.put adds or replaces, get returns it (or null if missing), and getOrDefault gives a default.- הסדר של HashMap לא מובטח, ולכן בדוגמה מדפיסים לפי מפתח.A HashMap's order isn't guaranteed, so the example prints by key.
HashMap ממפה מפתח לערך. put מוסיף או מחליף, get מחזיר (או null אם אין), ו-getOrDefault נותן ערך ברירת מחדל. הסדר של HashMap לא מובטח, ולכן בדוגמה מדפיסים לפי מפתח.A HashMap maps a key to a value. put adds or replaces, get returns it (or null if missing), and getOrDefault gives a default. A HashMap's order isn't guaranteed, so the example prints by key.נושאTopic 6
ספירה עם מילוןCounting with a map
ספירה עם מילוןCounting with a map
- התבנית הכי שימושית: לכל פריט,
getOrDefault(x, 0) + 1.The most useful pattern: for each item, getOrDefault(x, 0) + 1. TreeMap שומר את המפתחות ממוינים, ולכן ההדפסה תמיד באותו סדר.A TreeMap keeps its keys sorted, so printing is always in the same order.
התבנית הכי שימושית: לכל פריט, getOrDefault(x, 0) + 1. TreeMap שומר את המפתחות ממוינים, ולכן ההדפסה תמיד באותו סדר.The most useful pattern: for each item, getOrDefault(x, 0) + 1. A TreeMap keeps its keys sorted, so printing is always in the same order.נושאTopic 7
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.נושאTopic 8
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 9: רשימה וספירה לפי סופרShelf, step 9: a list and a count per author
שמרו את הספרים ב-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.ArrayList: רשימה שגדלה לבד:ArrayList: A list that grows by itself:
המונחים של המפגשThis session's termsHashMap: מילון של מפתח וערך:HashMap: A key-value map:
המונחים של המפגשThis session's termsמפתח וערך: במילון כל מפתח מופיע פעם אחת, ולכל מפתח ערך אחד.key and value: In a map each key appears once, with one value.
המונחים של המפגשThis session's termsסוגריים משולשים <>: אומרים מה יש בתוך האוסף:angle brackets <>: They say what's inside the collection:
המונחים של המפגשThis session's termsטיפוס עוטף: גרסת עצם של טיפוס פרימיטיבי:wrapper type: The object version of a primitive type:
המונחים של המפגשThis session's termsautoboxing: Java הופכת לבד int ל-Integer כשמכניסים לאוסף, ובחזרה כשמוציאים.autoboxing: Java turns an int into an Integer by itself when you put it in a collection, and back when you take it out.
המונחים של המפגשThis session's termsבניגוד למערך, ל-ArrayList אין גודל קבוע.Unlike an array, an ArrayList has no fixed size.
ArrayList: רשימה שגדלהArrayList: a list that growsadd מוסיף לסוף, get(i) קורא, set מחליף, remove מוחק (והשאר זזים).add appends, get(i) reads, set replaces, remove deletes (and the rest shift).
ArrayList: רשימה שגדלהArrayList: a list that growsהטיפוס בתוך <>.The type goes inside <>.
ArrayList: רשימה שגדלהArrayList: a list that growsאוספים מחזיקים רק עצמים, ולכן כותבים List<Integer>.Collections hold only objects, so you write List<Integer>.
רשימה של מספרים: Integer ו-autoboxingA list of numbers: Integer and autoboxingautoboxing: Java ממירה בין int ל-Integer לבד.Autoboxing: Java converts between int and Integer by itself.
רשימה של מספרים: Integer ו-autoboxingA list of numbers: Integer and autoboxingכמו במערך, same = names לא מעתיק את הרשימה: שתי הפניות לאותו עצם.As with arrays, same = names doesn't copy the list: two references to the same object.
רשימה היא עצם: שתי הפניותA list is an object: two referencesבזיכרון רואים רשימה אחת ושני חצים.In memory you see one list and two arrows.
רשימה היא עצם: שתי הפניותA list is an object: two referencesHashMap: מפתח וערךHashMap: key and valueput מוסיף או מחליף, get מחזיר (או null אם אין), ו-getOrDefault נותן ערך ברירת מחדל.put adds or replaces, get returns it (or null if missing), and getOrDefault gives a default.
HashMap: מפתח וערךHashMap: key and valueהסדר של HashMap לא מובטח, ולכן בדוגמה מדפיסים לפי מפתח.A HashMap's order isn't guaranteed, so the example prints by key.
HashMap: מפתח וערךHashMap: key and valueהתבנית הכי שימושית: לכל פריט, getOrDefault(x, 0) + 1.The most useful pattern: for each item, getOrDefault(x, 0) + 1.
ספירה עם מילוןCounting with a mapTreeMap שומר את המפתחות ממוינים, ולכן ההדפסה תמיד באותו סדר.A TreeMap keeps its keys sorted, so printing is always in the same order.
ספירה עם מילוןCounting with a mapjava.util.ConcurrentModificationException:java.util.ConcurrentModificationException:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 9: רשימה וספירה לפי סופרShelf, step 9: a list and a count per author
הצעד של היום בפרויקט מדףToday's step in the Shelf projectאיך יוצרים רשימה של מספרים שלמים?How do you create a list of whole numbers?
List<int> l = new ArrayList<>();List<int> l = new ArrayList<>();List<Integer> l = new ArrayList<>();List<Integer> l = new ArrayList<>();int[] l = new ArrayList();int[] l = new ArrayList();ArrayList l = {1, 2};ArrayList l = {1, 2};
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.List<Integer> l = new ArrayList<>();List<Integer> l = new ArrayList<>();
נכון: Integer, הטיפוס העוטף.Right: Integer, the wrapper type.מה מחזיר map.get("x") כשאין מפתח כזה?What does map.get("x") return when there's no such key?
- 00
nullnull- חריגהAn exception
- מחרוזת ריקהAn empty string
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.nullnull
נכון. לכן getOrDefault כל כך שימושי.Right. That's why getOrDefault is so handy.list היא [a, b, c]. מה היא אחרי list.remove(0)?list is [a, b, c]. What is it after list.remove(0)?
[b, c][b, c][a, b][a, b][null, b, c][null, b, c][a, b, c][a, b, c]
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.[b, c][b, c]
נכון: מחקנו את האינדקס 0, והשאר זזו שמאלה.Right: index 0 was removed and the rest shifted.מה למדנוWhat we learned
- ArrayList: רשימה שגדלה לבד:ArrayList: A list that grows by itself:
- בניגוד למערך, ל-ArrayList אין גודל קבוע.Unlike an array, an ArrayList has no fixed size.
- אוספים מחזיקים רק עצמים, ולכן כותבים
List<Integer>.Collections hold only objects, so you write List<Integer>. - כמו במערך,
same = names לא מעתיק את הרשימה: שתי הפניות לאותו עצם.As with arrays, same = names doesn't copy the list: two references to the same object. - HashMap ממפה מפתח לערך.A HashMap maps a key to a value.
- התבנית הכי שימושית: לכל פריט,
getOrDefault(x, 0) + 1.The most useful pattern: for each item, getOrDefault(x, 0) + 1.
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.