韩顺平Java28——Math类
Math类
- 基本介绍
- 常用方法
取a-b之间整数公式
int r =(int)(a+Math.random()*(b-a+1));
Arrays类
(1)
int[] a= {1,45,56}; System.out.println(Arrays.toString(a)); //[1, 45, 56]
(2)
Integer[] a= {0,-1,45,56,34}; Arrays.sort(a, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); System.out.println(Arrays.toString(a));
- 定制冒泡排序模拟:
package homework; import java.util.Arrays; import java.util.Comparator; /** * @author 紫英 * @version 1.0 * @discription 定制冒泡排序 */ public class Homework06 { public static void main(String[] args) { int[] array = {56, 63, 54, 2, 89, 1}; System.out.println("排序前:"); System.out.println(Arrays.toString(array)); b_sort(array, new Comparator() { //升序 @Override public int compare(Object o1, Object o2) { Integer n1 = (Integer) o1; Integer n2 = (Integer) o2; return n1-n2; } }); System.out.println("排序后:"); System.out.println(Arrays.toString(array)); b_sort(array, new Comparator() { //降序 @Override public int compare(Object o1, Object o2) { Integer n1 = (Integer) o1; Integer n2 = (Integer) o2; return n2-n1; } }); System.out.println("排序后:"); System.out.println(Arrays.toString(array)); } public static void b_sort(int[] array,Comparator c) { for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - 1 - i; j++) { if (c.compare(array[j],array[j + 1])>0) { //如果后面的大 就和前面的交换 int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } } }
(3)
(4)
(5)
(6)
(7)
本文来自博客园,作者:紫英626,转载请注明原文链接:https://www.cnblogs.com/recorderM/p/15741537.html