חלק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.
הנקודות החשובות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מוחק (והשאר זזים).addappends,get(i)reads,setreplaces,removedeletes (and the rest shift).- אוספים מחזיקים רק עצמים, ולכן כותבים
List<Integer>.Collections hold only objects, so you writeList<Integer>. - autoboxing: Java ממירה בין
intל-Integerלבד.Autoboxing: Java converts betweenintandIntegerby itself. - כמו במערך,
same = namesלא מעתיק את הרשימה: שתי הפניות לאותו עצם.As with arrays,same = namesdoesn'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:Integerforint,Doublefordouble. Collections hold only objects, henceList<Integer>. - autoboxingautoboxing: Java הופכת לבד
intל-Integerכשמכניסים לאוסף, ובחזרה כשמוציאים.Java turns anintinto anIntegerby 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.putwith 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.putwith 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:Integerforint,Doublefordouble. Collections hold only objects, henceList<Integer>. - autoboxingautoboxing: Java הופכת לבד
intל-Integerכשמכניסים לאוסף, ובחזרה כשמוציאים.Java turns anintinto anIntegerby 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 looping | java.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. |