Math类
常用方法
1. abs 绝对值
System.out.println(Math.abs(-11)); //输出11
2. pow 求幂
System.out.println(Math.pow(2,3)); //2的三次方
3. ceil 向上取整 floor 向下取整 round 四舍五入(实际上是 floor(n + 0.5))
System.out.println(Math.ceil(1.1));
System.out.println(Math.floor(1.1));
System.out.println(Math.round(1.4)); // Math.floor(n+0.5)
System.out.println(Math.round(1.5));
4. sqrt 求开方
System.out.println(Math.sqrt(9));
5. random 求随机数,返回一个 [0,1) 之间的double数。
获取 a-b 之间的一个随机整数:公式为 int num = (int)(a + Math.random()*(b - a + 1));
System.out.println((int)(2 + Math.random()*(6-2+1))); //返回[2,6]之间的数
返回 [2,6]之间的整数实际上就等于返回 [2,7)之间的整数。
6. max 求最大值,min 求最小值。
Arrays类
Arrays里面包含了一系列静态方法,用于管理或操作数组(比如排序和搜索)
1. toString 返回数组的字符串形式
int[] a = {1,2,3,4,5};
System.out.println(Arrays.toString(a)); // 输出 [1, 2, 3, 4, 5]
2. sort排序(自然排序和定制排序)
因为数组是引用类型,因此sort会直接改变原数组。
int[] a = {31,2,5,999,-3};
Arrays.sort(a); //自然排序,从小到大排列
System.out.println(Arrays.toString(a));// [-3, 2, 5, 31, 999]
需要重点理解的就是定制排序:它不仅需要传入一个数组,还需要实现一个匿名内部类(通过传入一个接口 Comparator 实现),传入的数组不能是基本类型,必须是包装类(如Integer),因为用到了泛型。
public static void main(String[] args) {
Integer[] a = {31,2,5,999,-3}; // 不能是基本类型
Arrays.sort(a, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1; // 从大到小排
}
});
System.out.println(Arrays.toString(a)); //[999, 31, 5, 2, -3]
}
对 return o2 - o1 的理解:实际上sort调用了父类的一个 binarySort的方法,而 o2 - o1的结果会影响方法的走向。
而在C++中,实现自定义排序则是通过自定义函数cmp。
int cmmp(object a,object b){
return a>b; // 从大到小排序
// 从小到大排序是 return a<b;
}
而在Java并不能用boolean来代表整形,因此只能用相减是否为正来判断该走哪一步。只要记住返回 o2 - o1 是从大到小排就行了。
3. 用冒泡排序模拟定制排序
public static void bubbleSort(Integer a[],Comparator b){
for(int i = 0;i<a.length-1;i++){
for(int j = 0;j<a.length-i-1;j++){ //冒泡排序
if(b.compare(a[j],a[j+1])<0){ //根据compare的返回结果判定是否交换
// 这与真正的compare思想一致
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}//调用Comparator接口的compare方法
}
}
}
public static void main(String[] args) {
Integer[] a = {31,2,5,999,-3};
bubbleSort(a, new Comparator() { //匿名内部类
@Override
public int compare(Object o1, Object o2){
int n1 = (Integer)o1;
int n2 = (Integer)o2;
return n1 - n2;
}
});
System.out.println(Arrays.toString(a)); // [999, 31, 5, 2, -3]
}
根据compare的具体实现,n1 - n2 < 0时进行交换,即n1小于n2时交换,也就是说最后是从大到小排列。
4. binarySearch 二分查找
通过二分搜索法进行查找,返回对应元素的索引,要求必须排好序。如果该数组中不存在该元素,则返回 -(low + 1),其中low最终是 查找元素应该位于的索引。
Integer[] a = {1,2,3,4,5};
int index1 = Arrays.binarySearch(a,5); // 输出4
int index2 = Arrays.binarySearch(a,6); // 输出-6 ,如果6存在的话索引应该是5
5. copyOf 数组元素的复制
Integer[] a = new Integer[]{1,2,3,4,5};
Integer[] newArr = Arrays.copyOf(a,3); //复制3个元素
System.out.println(Arrays.toString(newArr)); //[1, 2, 3]
如果拷贝的长度 > a.length ,那么就在新数组的后面增加 null,如果拷贝长度 < 0,就会抛出异常NegativeArraySizeException。
Integer[] newArr = Arrays.copyOf(a,10);
//[1, 2, 3, 4, 5, null, null, null, null, null]
Integer[] newArr = Arrays.copyOf(a,-1);
// Exception in thread "main" java.lang.NegativeArraySizeException
6. fill 数组元素的填充
Integer[] a = new Integer[]{1,2,3,4,5};
Arrays.fill(a,1000);
System.out.println(Arrays.toString(a)); // [1000, 1000, 1000, 1000, 1000]
7. equals 比较两个数组元素内容是否完全一致
Integer[] a = new Integer[]{1,2,3,4,5};
Integer[] b = new Integer[]{1,2,3,4,5};
Integer[] c = new Integer[]{1,3,2,4,5};
System.out.println(Arrays.equals(a,b)); // true
System.out.println(Arrays.equals(a,c)); // false,顺序不一样也不行
8. asList 将一组值转换成list(链表)
asList编译类型为 List(接口),运行类型为 java.util.Arrays$ArrayList,是Arrays类的。
List<Integer> asList = Arrays.asList(2,3,4,5,6,1);
System.out.println(asList.getClass()); // class java.util.Arrays$ArrayList
System类
1. exit 退出当前程序。
System.exit(0); // 0表示一个正常的状态。
2. arraycopy:复制数组元素,比较适合底层调用。一般还是使用 Arrays.copyOf 完成复制数组。
System.arraycopy(src,srcPos,dest,destPos,length);
src:源数组(提供数据)。srcPos:从源数组的哪个索引位置开始拷贝。dest:目标数组。
destPos:从源数组的数据拷贝到目标数组的哪个索引。 length:从源数组拷贝多少个数据。
3. currentTimeMillens:返回当前时间距离1970-1-1的毫秒数。
System.out.println(System.currentTimeMillis());
4. gc:运行垃圾回收机制 System.gc();