Java基础
一、数组1. 数组的定义2. 数组的静态初始化3. 数组的遍历4. 数组拷贝Arrays.copyOf()5. 数组排序Arrays.sort()6. 数组的二分查找Arrays.binarySearhc()7. Arrays.fill()8. 数组常见异常(1) 空指针异常(2) 数组越界异常
9. 二维数组(1) 二维数组声明(2) 交换二维数组的两行(3) “不规则”数组(4) 二维数组的遍历①②
二、Java高精度处理三、BigInteger1. 赋值2. 三个常量3. 加减乘除求余4. 求公约数5. 求幂6. 比较
四、BigDecimal1. 赋值2. 三个常量3. 加减乘除4. 求幂5. 比较6. 格式化小数点BigDecimal.setScale()
一、数组
1. 数组的定义
int[] a
= new int[10];
int b
[] = new int[10];
int size
= 5;
int[] c
= new int[size
];
System
.out
.println(a
[0]);
2. 数组的静态初始化
int[] a
= new int[] {1,2,3};
int[] b
= {6,7,8};
数组长度属性
int[] a
= {1,2,3};
int len
= a
.length
;
3. 数组的遍历
import java.util.Arrays;
int[] a
= {1,3,5};
for (int i
= 0; i
< a
.length
; i
++)
System
.out
.print(a
[i
] + " ");
System
.out
.println();
for (int i
: a
)
System
.out
.print(i
+ " ");
System
.out
.println();
System
.out
.println(Arrays
.toString(a
));
System
.out
.println(a
);
[I@15db9742[ 表示数组I 表示Integer@表示at 16进制地址15db9742
4. 数组拷贝Arrays.copyOf()
int[] a
={1,3,5,7,9};
int[] b
=Arrays
.copyOf(a
,a
.length
);
System
.out
.println(Arrays
.toString(b
));
a
=Arrays
.copyOf(a
,2*a
.length
);
System
.out
.print(Arrays
.toString(a
));
5. 数组排序Arrays.sort()
import java
.util
.Arrays
;
import java
.util
.Collections
;
public class Main {
public static void main(String
[] args
) {
int[] a
= {3,5,2,1,4};
int[] b
= a
;
Arrays
.sort(b
);
print(b
);
b
= Arrays
.copyOf(a
, a
.length
);
Arrays
.sort(b
,1,4);
print(b
);
Integer
[] c
= {3,5,2,1,4};
Integer
[] d
= Arrays
.copyOf(c
, c
.length
);
Arrays
.sort(c
,Collections
.reverseOrder());
print(c
);
Arrays
.sort(d
,1,4,Collections
.reverseOrder());
print(d
);
}
public static void print(int[] a
) {
for(int i
:a
) {
System
.out
.print(i
+" ");
}
System
.out
.println();
}
public static void print(Object
[] a
) {
for(Object i
:a
) {
System
.out
.print(i
+" ");
}
System
.out
.println();
}
}
6. 数组的二分查找Arrays.binarySearhc()
int[] a
= { 3, 5, 2, 1, 4 };
Arrays
.sort(a
);
int i
= Arrays
.binarySearch(a
, 3);
System
.out
.println(i
);
7. Arrays.fill()
只能初始化1维数组
int[] a
= new int[5];
Arrays
.fill(a
, 9);
System
.out
.println(Arrays
.toString(a
));
8. 数组常见异常
空指针异常数组越界异常
(1) 空指针异常
int[] a
= null
;
a
[0] = 9;
(2) 数组越界异常
int[] a
= {1,2,3};
System
.out
.println(a
[3]);
9. 二维数组
(1) 二维数组声明
int[][] a
= new int[2][10];
int[][] b
= new int[2][];
int[][] c
= {{1,2},{3,4}};
特殊写法
int[][] a
;
int b
[][];
int[] c
[];
int[] x
,y
[];
(2) 交换二维数组的两行
int[][] a
= { { 1, 3, 5 }, { 2, 4, 6 } };
int[] b
= a
[0];
a
[0] = a
[1];
a
[1] = b
;
System
.out
.println(Arrays
.deepToString(a
));
System
.out
.println(Arrays
.toString(a
));
(3) “不规则”数组
如果二维数组的每一行的长度不同,则称为“不规则”数组。
int[][] a
= new int[2][];
a
[0] = new int[3];
a
[1] = new int[5];
(4) 二维数组的遍历
①
int[][] a
= {{1,3,5},{2,4}};
for(int i
=0;i
<a
.length
;i
++) {
for(int j
=0;j
<a
[i
].length
;j
++) {
System
.out
.print(a
[i
][j
]+" ");
}
System
.out
.println();
}
②
int[][] a
= {{1,3,5},{2,4}};
for(int[] i
:a
) {
for(int j
:i
) {
System
.out
.print(j
+" ");
}
System
.out
.println();
}
运行结果 1 3 5 2 4
二、Java高精度处理
Java的大整数处理 BigInteger类Java的高精度浮点数处理 BigDecimal类
三、BigInteger
1. 赋值
import java
.math
.BigInteger
;
public class Main {
public static void main(String
[] args
) {
BigInteger x
= BigInteger
.valueOf(666);
BigInteger y
= new BigInteger("99");
System
.out
.println(x
);
System
.out
.println(y
);
}
}
2. 三个常量
BigInteger b1
= BigInteger
.ZERO
;
BigInteger b2
= BigInteger
.ONE
;
BigInteger b3
= BigInteger
.TEN
;
3. 加减乘除求余
BigInteger x
= BigInteger
.valueOf(6);
BigInteger y
= new BigInteger("2");
System
.out
.println(x
.add(y
));
System
.out
.println(x
.subtract(y
));
System
.out
.println(x
.multiply(y
));
System
.out
.println(x
.divide(y
));
System
.out
.println(x
.mod(y
));
4. 求公约数
BigInteger x
= BigInteger
.valueOf(24);
BigInteger y
= new BigInteger("16");
System
.out
.println(x
.gcd(y
));
5. 求幂
BigInteger x
= BigInteger
.valueOf(2);
System
.out
.println(x
.pow(10));
6. 比较
BigInteger x
= BigInteger
.valueOf(15);
BigInteger y
= BigInteger
.valueOf(9);
if (x
.compareTo(y
) == -1)
System
.out
.print("x<y");
else if (x
.compareTo(y
) == 0)
System
.out
.print("x=y");
else
System
.out
.print("x>y");
相等判断有专有函数equals
BigInteger x
= BigInteger
.valueOf(15);
BigInteger y
= BigInteger
.valueOf(9);
if (x
.equals(y
))
System
.out
.print("equal");
else
System
.out
.print("not equal");
四、BigDecimal
与BigInteger非常相似
1. 赋值
import java
.math
.BigDecimal
;
public class Main {
public static void main(String
[] args
) {
BigDecimal x
= BigDecimal
.valueOf(6.66);
BigDecimal y
= new BigDecimal("9.9");
System
.out
.println(x
);
System
.out
.println(y
);
}
}
2. 三个常量
BigDecimal b1
= BigDecimal
.ZERO
;
BigDecimal b2
= BigDecimal
.ONE
;
BigDecimal b3
= BigDecimal
.TEN
;
3. 加减乘除
BigDecimal x
= BigDecimal
.valueOf(3.6);
BigDecimal x
= BigDecimal
.valueOf(3.6);
BigDecimal y
= new BigDecimal("2.0");
System
.out
.println(x
.add(y
));
System
.out
.println(x
.subtract(y
));
System
.out
.println(x
.multiply(y
));
System
.out
.println(x
.divide(y
));
System
.out
.println(x
.divide(y
, 2, BigDecimal
.ROUND_HALF_UP
));
4. 求幂
BigDecimal x
= BigDecimal
.valueOf(0.2);
System
.out
.println(x
.pow(3));
5. 比较
BigDecimal x
= BigDecimal
.valueOf(1.5);
BigDecimal y
= BigDecimal
.valueOf(9.0);
if (x
.compareTo(y
) == -1)
System
.out
.print("x<y");
else if (x
.compareTo(y
) == 0)
System
.out
.print("x=y");
else
System
.out
.print("x>y");
相等判断有专有函数equals
BigDecimal x
= BigDecimal
.valueOf(2.00);
BigDecimal y
= BigDecimal
.valueOf(2.0);
if (x
.equals(y
))
System
.out
.print("equal");
else
System
.out
.print("not equal");
注:像2.0和2.00,就被认为是相等的(2和2.0不等)
6. 格式化小数点BigDecimal.setScale()
BigDecimal x
= BigDecimal
.valueOf(1.2);
x
= x
.pow(10);
System
.out
.println(x
.setScale(5,BigDecimal
.ROUND_HALF_UP
));
舍入方式含义
ROUND_HALF_UP四舍五入ROUND_UP远离0的舍入模式ROUND_DOWN接近0的舍入模式ROUND_CEILING接近正无穷大的舍入模式ROUND_FLOOR接近负无穷大的舍入模式