String, StringBuilder, StringBuffer String is a unchangeable object(although it does not belong to the 8 primitive types). If we change the string, we rebuild a new object that a reference “points” to it. So for an an object that always changes, the speed will slow down (JVM GC). At this time, we use StringBuffer(not thread safe) or StringBuilder(thread … More Java – String
Initialize a series of class objects-Array: Class Dog: Dog dog1 = new dog(); dog2,dog3,… Use a for loop to initialize but this is hard to name all new objects. Use Array instead: Dog.
Divide and Conquer if(start == end) return Item(start) mid = start + (end – start) / 2 *Compared with directly (end + start) / 2, this can calculate bigger integers. left part : start ~ mid right part : mid + 1 ~ end recursion middle part: Consider if part of the result is … More Algorithm Study Note(1) Summarize
Input: map: map.put(key,value); set: set.add(object); list: list.add(object); Class type and Primitive type: Used in map ,list: int -> Integer, boolean -> Boolean, char -> Character Contains: set.contains(object); length: list: list.size(); sort: array: Arrays.sort(int);
Reverse a Linklist The basic idea of reversing a linked list is “take out items and put in a new list(actually, this is not a real new list but a new reference), let the next item point to the former one”.
FIFO: Queue FILO: Stack Queue Since java 5 we can use queue by importing import java.util.Queue; Insert an item at the end of a queue and pop out from the head of the queue. Queue is an interface that there are several implementing classes, but the most used class is LinkedList Queue<TreeNode> queue = new … More Java Queue, Stack