Class Inheritance & Interface Implements
- class method - static methods (Math.man(); Math.sqry()....)
- instance method - non-static methods
public static void main(String[] args) { }
Overrding vs Overloading
overriding: same name and same sigatures
overloading: same name but different signatures(parameters)
Static type vs Dynamic type
List a = new ArrayList() // Static type a = new Dynamic type ()
Dynamic method selection: java checks static type when calls method
- Compiler plays it safe and only allows us to do things according to the static type.
- For overridden methods (not overloaded methods), the actual method invoked is based
on the dynamic type of the invoking expression (如果static type没有此方法, then compiletime error) - Can use casting to overrule compiler type checking.
Extends
Constructors Are Not Inherited: super()
interface vs astract class
interface:
- All methods must be public.
- All variables must be public static final.
- Cannot be instantiated
- All methods are by default abstract unless specified to be
default
- Can implement more than one interface per class
abstract class:
- Methods can be public or private
- Can have any types of variables
- Cannot be instantiated
- Methods are by default concrete unless specified to be abstract
- Can only implement one per class
Lists: ordered collection of items
IntList
需要改进, 创建添加都很麻烦
为了后续 将IntLis
t命名为 IntNode
public class IntNode{
public int item;
public IntNode next;
public IntNode(int i, IntNode n){
item= i;
next= n;
}
}
类LinkedList(SLList)
在list前面加一个sentinel 一个dummyhead通常会有利于对于special case, Null 的处理
大概逻辑如下:
public class SLList {
public IntNode first;
public int size;
public SLList() {
first = null;
size = 0;
}
public SLList(int x) {
first = new IntNode(x, null);
size = 0;
} /
** Adds an item to the front of the list. */
public void addFirst(int x) {
first = new IntNode(x, first);
size += 1;
}
}
Arrays
int x = new int[3];
y = new int[]{1,2,3,4};
int[] z = {1,2,3,4};
int[][] 2D = new int[4][];
arrays.length;
AList (Array based list)
resize array when size == items.length
. resize(size * RFACTOR);
Java 标准list库
ArrayList
ArrayList a = new ArrayList<>();
a.add(int index, E element); // a.add(E element); add to the end of the list
a.contains();
a.size();
a.get(index);
a.remove();
a.isEmpty();
a.indexof();
a.lastIndexof();
a.toArray();
a.sort(Comparator<> c)
a.clear();
a.set(index, element); //replace
a.clone(); shallow copy
LinkedList (和上面基本一致,可以getLast/addLast)
collection list用add get / Deque用offer/peek/poll
List l = new LinkedList<>();
l.add(); // same for get 和remove: l.get(); l.getFirst(); l.getLast()
l.addFirst();
l.addLast();
l.toArray();
Set and Map
Set: unordered collection of strictly unique items
HashSet
Set s = new HashSet<>();
s.add();
s.contains();
s.isempty();
s.remove();
TreeSet
还没用过
Map: collection of key/value pairs
HashMap
Map map = new HashMap<>();
map.containsKey();
map.containsValue();
map.keySet();
map.value();
map.get();
map.getOrDefault();
map.put(K,V);
map.remove();
map.replace();
Comparable & Iterable
Comparable
import java.util.Comparator;
public class Dog implements Comparable {
...
public int compareTo(Dog uddaDog) {
return this.size - uddaDog.size;
}
private static class NameComparator implements Comparator {
public int compare(Dog a, Dog b) {
return a.name.compareTo(b.name);
}
}
public static Comparator getNameComparator() {
return new NameComparator();
}
}
when use: Comparator
retrieve the new created comparator
Iterable
如果要给class需要循环,需要 implements Iterable
public interface Iterable{
Iterator iterator();
}
public interface Iterator{
boolean hasNext();
T next();
}
//举例
public class ArraySet implements Iterable{
...
public Iterator iterator() {
return new ArraySetIterator();
}
private class ArraySetIterator implements Iterator {
private int wizPos;
public ArraySetIterator() {
wizPos = 0;
}
public boolean hasNext() {
return wizPos < size;
}
public T next() {
T returnItem = items[wizPos];
wizPos += 1;
return returnItem;
}
...
public static void main(String[] args) {
ArraySet aset = new ArraySet<>();
aset.add(5);
aset.add(23);
//iteration
for (int i : aset) {
System.out.println(i);
}
}
}
StringBuilder sb = new StringBuilder("a");
sb.append("b");
sb.toString;
throw/catch exception
package