נבנה על ידי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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • ArrayList: רשימה שגדלה לבד:ArrayList: A list that grows by itself:
  • HashMap: מילון של מפתח וערך:HashMap: A key-value map:
  • בניגוד למערך, ל-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).
  • אוספים מחזיקים רק עצמים, ולכן כותבים 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.

מונחיםTerms

  • ArrayListArrayList: רשימה שגדלה לבד: List<String> l = new ArrayList<>();, l.add(x), l.get(i), l.size().A list that grows by itself: List<String> l = new ArrayList<>();, l.add(x), l.get(i), l.size().
  • HashMapHashMap: מילון של מפתח וערך: Map<String, Integer> m = new HashMap<>();, m.put(k, v), m.get(k). הסדר לא מובטח.A key-value map: Map<String, Integer> m = new HashMap<>();, m.put(k, v), m.get(k). The order isn't guaranteed.
  • סוגריים משולשים <>angle brackets <>: אומרים מה יש בתוך האוסף: List<String> היא רשימה של מחרוזות. המהדר לא ייתן להכניס משהו אחר.They say what's inside the collection: List<String> is a list of strings. The compiler won't let anything else in.
  • טיפוס עוטףwrapper type: גרסת עצם של טיפוס פרימיטיבי: Integer ל-int, Double ל-double. אוספים מחזיקים רק עצמים, ולכן List<Integer>.The object version of a primitive type: Integer for int, Double for double. Collections hold only objects, hence List<Integer>.
  • autoboxingautoboxing: Java הופכת לבד int ל-Integer כשמכניסים לאוסף, ובחזרה כשמוציאים.Java turns an int into an Integer by itself when you put it in a collection, and back when you take it out.
  • מפתח וערךkey and value: במילון כל מפתח מופיע פעם אחת, ולכל מפתח ערך אחד. put עם מפתח קיים מחליף את הערך.In a map each key appears once, with one value. put with an existing key replaces the value.
  • ArrayListArrayList: רשימה שגדלה לבד: List<String> l = new ArrayList<>();, l.add(x), l.get(i), l.size().A list that grows by itself: List<String> l = new ArrayList<>();, l.add(x), l.get(i), l.size().
  • HashMapHashMap: מילון של מפתח וערך: Map<String, Integer> m = new HashMap<>();, m.put(k, v), m.get(k). הסדר לא מובטח.A key-value map: Map<String, Integer> m = new HashMap<>();, m.put(k, v), m.get(k). The order isn't guaranteed.
  • מפתח וערךkey and value: במילון כל מפתח מופיע פעם אחת, ולכל מפתח ערך אחד. put עם מפתח קיים מחליף את הערך.In a map each key appears once, with one value. put with an existing key replaces the value.
  • סוגריים משולשים <>angle brackets <>: אומרים מה יש בתוך האוסף: List<String> היא רשימה של מחרוזות. המהדר לא ייתן להכניס משהו אחר.They say what's inside the collection: List<String> is a list of strings. The compiler won't let anything else in.
  • טיפוס עוטףwrapper type: גרסת עצם של טיפוס פרימיטיבי: Integer ל-int, Double ל-double. אוספים מחזיקים רק עצמים, ולכן List<Integer>.The object version of a primitive type: Integer for int, Double for double. Collections hold only objects, hence List<Integer>.
  • autoboxingautoboxing: Java הופכת לבד int ל-Integer כשמכניסים לאוסף, ובחזרה כשמוציאים.Java turns an int into an Integer by itself when you put it in a collection, and back when you take it out.

Cheatsheet

ArrayList: רשימה שגדלהArrayList: a list that grows
List<String> books = new ArrayList<>();
books.add("Dune");
books.add("Emma");
books.add(0, "Ulysses");
System.out.println(books);
books.remove("Emma");
System.out.println(books.get(1) + " " + books.size() + " " + books.contains("Emma"));
רשימה של מספרים: Integer ו-autoboxingA list of numbers: Integer and autoboxing
List<Integer> nums = new ArrayList<>();
nums.add(5);
nums.add(12);
nums.add(7);
int total = 0;
for (int n : nums) {
    total += n;
}
System.out.println(nums + " sum " + total);
רשימה היא עצם: שתי הפניותA list is an object: two references
List<String> names = new ArrayList<>();
names.add("Noa");
names.add("Avi");
List<String> same = names;
same.add("Dana");
System.out.println(names.size());
HashMap: מפתח וערךHashMap: key and value
Map<String, Integer> pages = new HashMap<>();
pages.put("Dune", 412);
pages.put("Emma", 474);
pages.put("Dune", 896);
System.out.println(pages.get("Dune"));
System.out.println(pages.get("Hamlet"));
System.out.println(pages.getOrDefault("Hamlet", 0));
System.out.println(pages.containsKey("Emma") + " " + pages.size());
ספירה עם מילוןCounting with a map
String text = "to be or not to be";
Map<String, Integer> count = new TreeMap<>();
for (String w : text.split(" ")) {
    count.put(w, count.getOrDefault(w, 0) + 1);
}
System.out.println(count);

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
ConcurrentModificationExceptionצייד באגים: מחיקה תוך כדי לולאהBug hunter: removing while loopingjava.util.ConcurrentModificationException: אסור לשנות רשימה בזמן שלולאת for-each עוברת עליה. removeIf עושה את זה בבטחה.java.util.ConcurrentModificationException: you can't change a list while a for-each is going over it. removeIf does it safely.

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