<aside> 📌 SUMMARY:
</aside>
Static
array VS Arrays VS Array
Exceptions
Members(fields, methods, classes) belong to class, not object(instance).
Arrays.sort(array): use binary search, TC: O(nlogn)
new array: int[] = new int[3]
array length cannot be changed after the array object is created. (final).
Arrays: Java里放Array function的class
Array: Java里的class
array是一个object
ArrayIndexOutOfBoundException!
NullPointerException!
<aside> 📌 SUMMARY:
</aside>
ArrayList
Abstract class and interface
ArrayList is regarded as a resizable array.
class ArrayList<E> E: generic type (any reference type), but not primitive type.
如果不用generic type,默认存储和读取的是Object type
注意区分
get(int index): array[index] TC: O(1)
set(int index, E e): array[index] = e TC: O(1)
add(E e) - amortized O(1), 如果需要扩容,时间复杂度是O(n),但是均摊下来也是O(1)
add(int index, E e) index → [0, size] —worst O(n)
remove(int index) index → [0, size] —worst O(n)
when use ArrayList or LinkedList
implements 实现
@Override,重写
一个class可以implements多个interface,但是只能inheritance一个super class,把interface理解成技能包
Benefits: More flexible, more extensible
不能用interface去 instantiate一个object,但是class可以
<aside> 📌 SUMMARY: 优先级:int[]→ArrayList→LinkedList, 尽量不要使用LinkedList,除非你一定要用.next,因为LinkedList,overhead大,locality 差
</aside>
Inheritance
Override
Polymorphism多态
Overload
Abstract class vs. interface
<aside> 📌 SUMMARY:
</aside>