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

חזרה למפגשBack
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.

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

מונחיםTerms

המונחים של המפגש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.
נושא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 <>.
נושא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.
נושא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.
נושא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.
נושא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.
נושאTopic 7

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:
נושאTopic 8

פרויקט מדףShelf project

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

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

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.

בניגוד למערך, ל-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 <>.

אוספים מחזיקים רק עצמים, ולכן כותבים List<Integer>.Collections hold only objects, so you write List<Integer>.

autoboxing: Java ממירה בין int ל-Integer לבד.Autoboxing: Java converts between int and Integer by itself.

כמו במערך, 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.

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.

התבנית הכי שימושית: לכל פריט, 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.

java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:

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

איך יוצרים רשימה של מספרים שלמים?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};

List<Integer> l = new ArrayList<>();List<Integer> l = new ArrayList<>();

מה מחזיר map.get("x") כשאין מפתח כזה?What does map.get("x") return when there's no such key?

  • 00
  • nullnull
  • חריגהAn exception
  • מחרוזת ריקהAn empty string

nullnull

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]

[b, c][b, c]

מה למדנו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.

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

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

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