序言
这一篇java基本语法介绍,专门提供给想要阅读《算法》第4版,但是苦于没有java基础,看不懂书中代码实现的人。文章将只学习《算法》4中涉及到的java语法,也就是说学习完这篇语法介绍后,就可以放心大胆地翻开算法4啦![干杯]
java程序的基本结构
开始
原始数据类型
语句
数组
静态方法
方法库与其API
开始
public class FileName{
// tip:此处的类名FileName一定一定要与文件名一样!
public static void main(String[] args){
}
project/ $ javac Operation.java // tip:将源代码文件编译成字节码
project/ $ java Operation // tip: 运行代码(只写文件名即可)
public class FinalVar{
// tip:此处的类名FinalVar一定一定要与文件名一样!
public static void main(String[] args){
final String FINAL_STRING = "flj zei cai";
System.out.println(FINAL_STRING);
}
}
类型转换问题
1,自动转换:如果不会损失信息,数值会被自动提升为高级的数据类型
eg:2 + 3.5中,2转换为2.0,结果返回5.5
2,强行转换:(int)3.5 = 3 (double)3 = 3.0
表达式
可计算出结果的一串字面量,变量,运算符的组合
表达式中的运算符根据优先级规则和从左到右规则起作用
布尔表达式: 含有 == != < > <= >= 的表达式 计算的结果是布尔型(并非参与比较的数据类型)。在条件判断语句中常见
int a1 = 2;
int a2 = 3;
boolean a1 <= a2; // 返回值不是int,而是false
语句
1,初始化语句 == 声明 + 赋值语句
2,while 可以理解为重复的 if
3,for 可以理解为带有变化索引的while
int i;
i = 2 + 3;
if(<boolean expression>){
<代码块>
}
while(<boolean expression>){
<代码块>
}
for(<initialize>; <boolean expression>; <increment/*递增*/>){
<代码块>
}
数组是可以储存多个相同数据类型的数据结构
创建并初始化数组
完整版代码:
double[] a; //声明数组
a = new double[N];
for (int i; i < N; i++)
a[i] = 0.0;
简化写法(数组所有元素都自动初始化为默认值 0 / 0.0 / false):
double[] a = new double[N]; // 默认了初始化后的数组元素全部为0.0
如果想初始化的数组不为初始值:使用for遍历赋值(同上),或者a = {0.1, 0.2}手动赋值
double max = a[0];
for (int i = 1; i < a.length; i++)
if(a[i] > max) max = a[i];
double res = 0, sum = 0;
for(int i; i < a.length; i++)
res += a[i];
res = sum / (a.length);
double[] b = new double[a.length];
for (int i = 0; i < a.length; i++)
b[i] = a[i];
tip:不可以使用“起别名”操作 double[] b = a; 此时两个变量指向同一个数组
for(int i = 0; i < a.leanth; i++){
double temp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
}
int N = a.length;
double[][] c = double[N][N];
for(int i = 0; i < N; i++){
// 只行列点乘
for(int j = 0; j < N; j++)
c[i][j] += a[i][j]*b[j][i];
}
同C++,python 中的方法定义,类似于C中的函数。
方法中封装了一系列运算,由“参数” 经过运算 产生“某类型的返回值”或“副作用”
Arr.sort(); // 对象.方法名()
isPrime(3); // 方法名(参数)
递归三原则 |
---|
递归总要包含一个最简情况 — 找到基线条件 |
递归总要分解成一个个的子问题 — 靠近基线条件 |
递归中的父问题与子问题(即前后的调用栈)不应该由交集 |
递归示例 — 二分查找的递归实现
//二分查找的递归实现
public static int rankMain(int key, int[] a){
// 调整函数的接口
return rank(key, a, 0, a.length - 1);
}
public static int rank(int key, int[] a, int low, int high){
if(low > high) return -1;
int mid = (high + low) / 2;
if(a[mid] < key) return rank(key, a, mid+1, high);
else if(a[mid] > key) return rank(key, a, low, mid - 1);
else return mid;
}
典型静态方法的实现
计算一个整数的绝对值
public static int abs(int x){
if(x < 0) return -x;
return x;
}
public static double abs(double x){
if (x < 0.0) return -x;
return x;
}
public static boolean isPrime(int t){
if(t < 2) return false;
for(int i = 0; i*i <= t; i++){
if (t % i == 0) return false;
return true;
}
}
public static double sqrt(double c){
if(c < 0) return Double.NaN;
double err = 1e-15 // 最大误差
double t = c;
while (Math.abs(t - c/t) > err*t)
t = (c/t + t) / 2.0;
return t;
}
public static double (double a, double b){
return Math.sqrt(a*a + b*b);
}
public static double H(int N){
double sum = 0.0;
for(int i = 0; i <= N; i++)
sum += 1.0 / i;
return sum;
}
常用的库 (待补充)
1,public class StdRandom
— public static double random(); 0-1之间的实数
— public static int uniform(int N); 0-N-1之间的整数
— public static int uniform(int low, int high); low - high之间的整数
2,public class StdStats
— public static double max(int[] arr); 数组中的最大值
— public static double min(int[] arr); 数组中的最小值
— public static double mean(int[] arr); 数组中的平均值
3,public class Arrays
— public static void sort(int[] arr); 将数组按升序排序
4,public class Math
— public static double sqrt(double a); 计算平方根
— public static double pow(double a, double b); 计算a的b次方
— public static double randow(); 产生[0, 1)之间的随机数