第1章 课程导言
基础知识
计算机基础
- 操作系统
- 网络
- 数据库
编程语言基础
- 数据类型
- 装箱与拆箱
编码技巧
- 递归控制
- 循环控制
- 边界控制
- 数据结构
- 数的遍历
面向对象
- 类与对象
- 接口与实现
- 继承与封装
- 不可变类型
- 泛型
设计模式
- 再谈Singleton
- 变继承关系为组合关系
- 对象的如果创建
高级知识点
- 并行计算
- 多线程
- 资源管理
讲授方式
- 基础知识
划重点的方式讲授基础 - 编码
注重思想方法和理论结合
在线笔试
image.png
第2章 操作系统
进程和线程
进程
进程包含一个或则多个线程,都有一个内存空间,进程可以打开同一个文件,可以同时抢夺同一个端口,我们看到的进程是一个寻址空间,每个进程都是相互独立的
image.png
线程
线程是进程中的一部分,线程包括栈,从主函数mian函数开始,每次调用函数时,就把所有的参数和返回地址压到栈里,还包含一些局部变量压到栈里
线程还有包括PC(program control),就是下一条指令的地址,其实计算机在运行时,是线程的运行,进程就像是一个容器,装线程用的,PC放在内存里
image.png
TLS(Thread Local Storage)线程独有的内存,存放线程内部数据和变量
存储和寻址
存储
image.png
寻址
- 寻址空间
32位 ---- 4G
64位 ---- 10^19 Bytes
64位JVM
image.png
第3章 网络
image.png
不可靠
- 丢包,重复包
- 出错
- 乱序
TCP协议中给出了解决方案
不安全
- 中间人攻击
- 窃取
- 篡改
滑动窗口协议
- TCP协议中使用
-
维持发送方/接受方缓冲区
发送一个包,确认一个包
image.png
发送多个包,再确认
image.png
滑动窗口的实现
提供一个缓冲区
image.png
丢包重发
image.png
第4章 数据库
概述
关系型数据库
- 基于关系代数理论
- 缺点:表的结构不直观,实现复杂,速度慢
- 优点:健壮性高,社区庞大
事物
ACID
- Atomicity
- Consistency
- Isolation
- Durability
事物的隔离级别
- Read uncommitted
- Read Committed
- Repeatable Reads
- Serializable
乐观锁
- 读取数据,记录Timestamp
- 修改数据
- 检查和提交数据
应用场景,冲突不多的时候,很好。冲突很多时不好,因为数据库操作很慢,可以放在内存中操作
第5章 程序设计语言基础
归类
类型检查
- 编译时:C,C++,Java,Go
- 运行时:Python,Perl,JavaScript, Ruby
运行/编译
- 编译为机器代码运行:C,C++
编译成机器代码速度比较快,然而需要与操作系统打交道,不同的机器操作系统有差别,所有跨平台性差 - 编译为中间代码,在虚拟机运行:Java, C#
编译为中间代码,在虚拟机运行,提供程序的跨平台性 - 解释执行:Python,Perl,JavaScript
在解释器上执行,也需要跟操作系统打交道,有时候移植也不是那么方便
编程范式
- 面向过程:C,Visual Basic
用的不多 - 面向过程:Java,C#, C++, Scala
现在的主流 - 函数式:Haskell,Erlang
现在非常火
数据类型
Java的数据类型
image.png
image.png
image.png
有+0 和-0 多了一个0
补码
image.png
image.png
采用补码和我们直观的记法是不一样的
可以是两个数互为相反数相加为0
image.png
浮点数与定点数
image.png
浮点数的比较
image.png
Java的数据类型
image.png
image.png
image.png
装箱和拆箱
image.png
image.png
- 拆箱后比较值
- 比较地址
- 在-128~127直接用系统的,不在内就false
- Integer.valueOf(2)就是这个箱子,在intValue()把值拿出来
- 比较值是否相等
第6章 编码技巧
在白板上写程序
image.png
递归控制
作者:火焰烨兹
链接:https://www.zhihu.com/question/42377120/answer/278637121
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
一、所谓递归 在数学与计算机科学中,递归(Recursion)是指在函数的定义中使用函数自身的方法。
二、所谓递归三要素1、明确递归终止条件;2、给出递归终止时的处理办法;3、提取重复的逻辑,缩小问题规模。
简单来给你讲一讲这两点,你应该能初步了解递归,首先给你一段代码
public class Recursion(){
public static void main(String[] args) {
Long l =fibonacci(10);
System.out.println(l);
}
public static Long fibonacci(int i){
if(i==1 || i==2)
return 1L;
else
return fibonacci(i-1)+fibonacci(i-2);
}
}
上面的fibonacci()就是一个递归的方法,首先,他在方法内部调用了自己(return fibonacci(i-1)+fibonacci(i-2););然后,有明确的递归终止条件(当i为1,或者为2的时候);其次,有给出递归终止时的处理办法(return 1);最后,提取重复的逻辑,缩小问题规模(假如不使用递归,可以使用循环实现,理论上所有递归都可以用循环实现,但是复杂了很多)
递归的书写方法
image.png
链表的创建
image.png
image.png
- 定义一个Node
public class Node {
private final int value;
private Node next;
public Node(int value) {
super();
this.value = value;
this.next = null;
}
public int getValue() {
return value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public static void printLinkList(Node head) {
while(head != null) {
System.out.print(head.getValue() + " ");
head = head.getNext();
}
System.out.println();
}
}
- 创建
package chapter6;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LinkedListCreator {
/**
* Creates a linked list.
* @param data the data to create the list
* @return head of the linked list. The returned linked ends
* with last node with getNext() == null.
*/
public Node createLinkedList(List<Integer> data) {
if (data.isEmpty()) {
return null;
}
Node firstNode = new Node(data.get(0));
Node headOfSublist = createLinkedList(data.subList(1, data.size()));
firstNode.setNext(headOfSublist);
return firstNode;
}
public static void main(String[] args) {
LinkedListCreator creator = new LinkedListCreator();
Node.printLinkList(
creator.createLinkedList(new ArrayList<Integer>()));
Node.printLinkList(
creator.createLinkedList(Arrays.asList(1)));
Node.printLinkList(creator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5)));
}
}
链表的反转
image.png
-
假设后面的能够反转,如下图所示
image.png
-
让2指向1,再让1指向null
image.png
package chapter6;
import java.util.ArrayList;
import java.util.Arrays;
public class LinkedListReverser {
/**
* Reverses a linked list.
*
* @param head the linked list to reverse
* @return head of the reversed linked list
*/
public Node reverseLinkedList(Node head) {
// size == 0
if (head == null || head.getNext() == null) {
return head;
}
// size == 1
// if (head.getNext() == null) {
// return head;
// }
Node newHead = reverseLinkedList(head.getNext());
head.getNext().setNext(head);
head.setNext(null);
return newHead;
}
public static void main(String[] args) {
LinkedListCreator creator = new LinkedListCreator();
LinkedListReverser reverser = new LinkedListReverser();
Node.printLinkList(reverser.reverseLinkedList(
creator.createLinkedList(new ArrayList<Integer>())));
Node.printLinkList(reverser.reverseLinkedList(creator.createLinkedList(Arrays.asList(1, 2))));
Node.printLinkList(reverser.reverseLinkedList(
creator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5))));
}
}
列出所有的组合
image.png
-
缩小规模
image.png
package chapter6;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Combinations {
/**
* Generates all combinations and output them,
* selecting n elements from data.
* @param data
* @param n
*/
public void combinations(
List<Integer> selected, List<Integer> data, int n) {
// initial value for recursion
// how to select element
// how to output
if (n == 0) {
// output all selected elements
for (Integer i : selected) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
return;
}
if (data.isEmpty()) {
return;
}
// select element 0
selected.add(data.get(0));
combinations(selected, data.subList(1, data.size()), n - 1);
// un-select element 0
selected.remove(selected.size() - 1);
combinations(selected, data.subList(1, data.size()), n);
}
public static void main(String[] args) {
Combinations comb = new Combinations();
comb.combinations(new ArrayList<Integer>(), Arrays.asList(1, 2, 3, 4), 2);
}
}
递归的缺点
image.png
循环控制
循环书写的方法
image.png
列表反转
-
从中间切一刀
image.png
-
各放一个变量来指示这两个链表
image.png
-
向前推进
image.png
-
刚开始的满足反转不变式
image.png
package chapter6;
import java.util.ArrayList;
import java.util.Arrays;
public class LinkedListReverser2 {
public Node reverseLinkedList(Node head) {
Node newHead = null;
Node curHead = head;
// Loop invariant:
// newHead points to the linked list already reversed.
// curHead points to the linked list not yet reversed.
while(curHead != null) {
// 循环体 考虑已经做了很多轮的循环的中间体
Node next = curHead.getNext();
curHead.setNext(newHead);
newHead = curHead;
curHead = next;
}
return newHead;
}
public static void main(String[] args) {
LinkedListCreator creator = new LinkedListCreator();
LinkedListReverser2 reverser = new LinkedListReverser2();
Node.printLinkList(reverser.reverseLinkedList(
creator.createLinkedList(new ArrayList<Integer>())));
Node.printLinkList(reverser.reverseLinkedList(creator.createLinkedList(Arrays.asList(1, 2))));
Node.printLinkList(reverser.reverseLinkedList(
creator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5))));
}
}
链表删除节点
image.png
image.png
image.png
package chapter6;
import java.util.Arrays;
public class LinkedListDeletor {
// 考虑特殊处理
public Node deleteIfEquals(Node head, int value) {
while (head != null && head.getValue() == value) {
head = head.getNext();
}
if (head == null ) {
return null;
}
Node prev = head;
while(prev.getNext() != null) {
if (prev.getNext().getValue() == value) {
prev.setNext(prev.getNext().getNext());
} else {
prev = prev.getNext();
}
}
return head;
}
// 增加虚节点
public Node deleteIfEquals2(Node head, int value) {
Node prev = new Node(0);
prev.setNext(head);
while(prev.getNext() != null) {
if (prev.getNext().getValue() == value) {
prev.setNext(prev.getNext().getNext());
} else {
prev = prev.getNext();
}
}
return head;
}
public static void main(String[] args) {
LinkedListCreator creator = new LinkedListCreator();
LinkedListDeletor deletor = new LinkedListDeletor();
Node.printLinkList(deletor.deleteIfEquals(
creator.createLinkedList(Arrays.asList(1, 2, 3, 2, 2)), 2));
Node.printLinkList(deletor.deleteIfEquals2(
creator.createLinkedList(Arrays.asList(1, 2, 3, 2, 2)), 2));
}
}
边界控制
image.png
image.png
package chapter6;
public class BinarySerach {
public static int binarySerach(int[] array,int start, int end, int value) {
while(start < end){
int mid = (start + end) / 2;
if (array[mid] < value) {
return binarySerach(array, mid + 1, end, value);
} else if (array[mid] > value) {
return binarySerach(array, start, mid - 1, value);
}else {
return mid;
}
}
return -1;
}
public static int binarySerach(int[] arr, int k) {
int a = 0;
int b = arr.length - 1;
// Loop invariant: [a, b) is a valid range. (a <= b)
// k may only be within range [a, b)
while(a < b) {
// a + b 可能会overflow
// int m = (a + b) / 2;
int m = a + (b - a) / 2;
if (k < arr[m]) {
b = m - 1;
} else if (k > arr[m]) {
a = m + 1;
} else {
return m;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {1, 3, 4, 5, 6};
int result = binarySerach(array, 0, array.length - 1, 1);
System.out.println(result);
int result2 = binarySerach(array, 1);
System.out.println(result2);
}
}
数据结构
- 数据结构的回顾
- 树的遍历
-
算法复杂性分析
image.png
image.png
image.png
image.png
image.png
image.png
树
树的遍历
- 前序遍历
⑴ 访问根结点;
⑵ 遍历左子树;
⑶ 遍历右子树。 - 中序遍历
⑴遍历左子树;
⑵访问根结点;
⑶遍历右子树。 - 后续遍历
⑴遍历左子树;
⑵遍历右子树;
⑶访问根结点。 -
层次遍历
image.png
- Nodetree
package chapter6;
public class TreeNode {
private final char value;
private TreeNode left;
private TreeNode right;
public TreeNode(char value) {
this.value = value;
this.left = null;
this.right = null;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public char getValue() {
return value;
}
}
- 创建一个简单的树
public class TreeCreator {
public TreeNode createSampleTree() {
TreeNode root = new TreeNode('A');
root.setLeft(new TreeNode('B'));
root.getLeft().setLeft(new TreeNode('D'));
root.getLeft().setRight(new TreeNode('E'));
root.getLeft().getRight().setLeft(new TreeNode('G'));
root.setRight(new TreeNode('C'));
root.getRight().setRight(new TreeNode('F'));
return root;
}
}
- 创建数
public class TreeCreator {
public TreeNode createSampleTree() {
TreeNode root = new TreeNode('A');
root.setLeft(new TreeNode('B'));
root.getLeft().setLeft(new TreeNode('D'));
root.getLeft().setRight(new TreeNode('E'));
root.getLeft().getRight().setLeft(new TreeNode('G'));
root.setRight(new TreeNode('C'));
root.getRight().setRight(new TreeNode('F'));
return root;
}
}
- 遍历
package chapter6;
public class TreeTraversal {
// 前序 根左右
public void preOrder(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.getValue());
preOrder(root.getLeft());
preOrder(root.getRight());
}
// 中序 左根右
public void inOrder(TreeNode root) {
if (root == null) {
return;
}
inOrder(root.getLeft());
System.out.print(root.getValue());
inOrder(root.getRight());
}
// 后续 左右根
public void postOrder(TreeNode root) {
if (root == null) {
return;
}
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.print(root.getValue());
}
public static void main(String[] args) {
TreeCreator creator = new TreeCreator();
TreeTraversal traversal = new TreeTraversal();
TreeNode sampleTree = creator.createSampleTree();
traversal.preOrder(sampleTree); //ABDEGCF
System.out.println();
traversal.inOrder(sampleTree); // DBGEACF
System.out.println();
traversal.postOrder(sampleTree); // DGEBFCA
System.out.println();
}
}
寻找中序遍历的下一个节点
image.png
第7章 面向对象
概述
- 类与对象
- 接口与实现
- 继承与封装
- 不可变对象
- 泛型
类与对象
- 类的成员变量 --> 对象的状态
- 类的成员函数 --> 对象行为
- 类的静态变量
- 类的静态函数
-
逻辑结构
image.png
-
物理结构
image.png
image.png
类的特殊函数
- 构造函数
- equals
- hashCode
- toString
接口与抽象类
image.png
-
接口的多重实现
image.png
实现Iterable接口
public class LinkedList implements Iterable<Integer> {
Node head;
Node tail;
public LinkedList() {
head = null;
tail = null;
}
public void add(int value) {
Node node = new Node(value);
if (head == null) {
head = node;
} else {
tail.setNext(node);
}
tail = node;
}
class ListIterator implements Iterator<Integer> {
Node currentNode;
public ListIterator(Node head) {
currentNode = head;
}
@Override
public boolean hasNext() {
if (currentNode == null) {
return false;
}
return true;
}
@Override
public Integer next() {
if (currentNode == null) {
throw new NoSuchElementException();
}
// TODO Auto-generated method stub
int value = currentNode.getValue();
currentNode = currentNode.getNext();
return value;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
@Override
public Iterator<Integer> iterator() {
return new ListIterator(head);
}
}
继承与封装
image.png
image.png
不可变性
image.png
image.png
泛型
image.png
image.png
image.png
image.png
第8章 设计模式
简介
再谈Singletion模式
image.png
image.png
image.png
image.png
image.png
image.png
第9章 高级知识点
并行计算
image.png
image.png
image.png
image.png
image.png
image.png
为了防止太多次IO,添加一个缓冲区
image.png
多线程
image.png
image.png
image.png
资源管理
image.png











网友评论