Java Notes
AP Computer Science A
數字處理
- final = c++ 的 const
- error
- AC
- others
- other error
String
- equals
- compareTo
- length
- substring
- indexOf
Math
- random
- other methods
Array & Array List
- Array
- Array List
Class
extends & polymorphism 多態性 & implements
sorting
selection sort
最小放第一個,第二小放第二個,依此類推
insertion sort
將資料分為” 已排序 “ 和 “ 未排序 “,將未排序的第一筆值和已排序的資料由右而左相比大小並插入適當位置。
merge sort
將資料分割為左子樹以及右子樹,接著遞迴分割每一次分割後的左子樹以及右子樹,直到每個子樹只剩下一個元素,再將這些子樹依小到大(or 大到小)合併(Merge)。
CSE 123
Section 0 Warm up
Array versus ArrayList
- Print
- Length and Size
Programming Assignment 0
Map
HashMap vs. TreeMap
- Ordering:
- HashMap: Entries in a HashMap are not ordered in any specific way. There is no guarantee of the order in which elements were added or any natural ordering of the keys.
- TreeMap: Entries in a TreeMap are sorted based on their keys. By default, they are sorted in natural order or according to a specified comparator. This means that the keys are always sorted in the map.
- Performance:
- HashMap: Offers faster average-case performance for basic operations like insertion, deletion, and retrieval (O(1) on average). However, this may not hold true in all cases, as performance depends on the quality of the hash function and collision resolution strategy.
- TreeMap: Provides O(log n) time complexity for basic operations, which makes it slower than HashMap. However, this logarithmic time complexity is more predictable and consistent regardless of the distribution of keys.
- Null Keys:
- HashMap: Allows one null key. This means you can have one key-value pair with a null key in a HashMap.
- TreeMap: Does not allow null keys. Attempting to insert a null key will result in a NullPointerException.
- Custom Sorting:
- HashMap: Does not provide built-in support for custom sorting. Keys are inserted based on their hash code, and their order is not controlled by the developer.
- TreeMap: Allows you to specify a custom comparator to control the order of keys. This enables you to sort keys in a custom way.
- Memory Usage:
- HashMap: Typically consumes less memory compared to TreeMap because it does not need to maintain the sorted order of keys.
- TreeMap: Consumes more memory due to the overhead of maintaining a balanced tree structure to keep keys sorted.
Map methods
Set
Set methods
Queue
Queue methods
Stack
Stack methods
pre class work 1
pre class work 2
Pre-Condition
Accepted range of input
Post-Condition
Expected Output
Exception
Some important exceptions we will see and use throughout CSE 123 include
- NullPointerException
- IllegalArgumentException
- ArrayIndexOutOfBoundsException
- ClassCastException