ArrayList: a resizable array.
- maintain an array with potential unused cells.
- will expand the array if there is no unused cells available, by replacing the maintained array with a new larger array (1.5 times larger by default).
1 | class ArrayList<E> { |
LinkedList: 1 by 1,并且中间空了以后需要挪动。
ArrayList: 中间空了以后需要挪动
When ArrayList, When LinkedList?
- a lot of random access: ArrayList
- always add/remove at the end: ArrayList. (when time complexity is similar for using ArrayList and LinkedList, use ArrayList. ArrayList uses memory more efficiently. For each element, LinkedList needs to create different ListNode in memory, extra cost of storing the “next”, “prev” reference (overhead), and the allocation is not contiguous.)
- Stack and Vector class not recommended. Vector => use ArrayList. Stack => use Deque (LinkedList, ArrayDeque).
Logical Operator Short Circuit
Bitwise AND (&)
The AND operator compares each binary digit of two integers and gives back 1 if both are 1, otherwise it returns 0.
This is similar to the && operator with boolean values. When the values of two booleans are true the result of a && operation is true.
Let’s use the same example as above, except now using the & operator instead of the | operator:
1
2
3
4
5
6
7
public void givenTwoIntegers_whenAndOperator_thenNewDecimalNumber() {
int value1 = 6;
int value2 = 5;
int result = value1 & value2;
assertEquals(4, result);
}Let’s also see the binary representation of this operation:
1
2
3
40110
0101
-----
01000100 is 4 in decimal, therefore, the result is:
1
result : 4
&& is logical AND operator comparing boolean values of operands only.