package java08; /* java.util.Arrays是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组常见的操作 public static String toString(数组);将参数数组变成字符串形式 pubic static void sort(数组);就按默认升序(从小到大)对数组元素进行排序 备注: 1、如果是数字,sort默认按照升序从小到大 2、如果是字符串,sort默认按照字母升序 3、如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口 * * */ import java.util.Arrays; public class DemoArrays { public static void main(String[] args) { int[] listArray = {10,30,20}; Arrays.sort(listArray); System.out.println(Arrays.toString(listArray));//[10, 20, 30] String[] strlist = {"abc","cdf","bhk"}; Arrays.sort(strlist); System.out.println(Arrays.toString(strlist));//[abc, bhk, cdf] } }
package java08; import java.util.Arrays; /* 题目:请使用Arrays相关的API,将一个随机字符串中的所有字符串升序排列,并倒序打印 * * */ public class Arraypractise { public static void main(String[] args) { String str = "qwertyuioplkjhgfdsazxcvbnm"; char[] charstr = str.toCharArray();//.toCharArray是将字符串转换成字符数组 Arrays.sort(charstr);//Arrays.sort的使用对象是数组,所以,需要把字符串变成数组 System.out.println(charstr); for (int i =charstr.length-1; i >=0; i--) {//判断条件是大于等于0 System.out.print(charstr[i]); System.out.print(" "); } } }
package java08; /* java类中Math数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关操作 public static double abs(double num);获取绝对值,有多种重载 public static double ceil(double num);向上取整 public static double floor(double num);向下取整 public static long round(double num);四舍五入 Math.PI代表近似的圆周率常量(double) * */ public class Mathpractise { public static void main(String[] args) { //获取绝对值 System.out.println(Math.abs(-1.2));//1.2 System.out.println(Math.abs(1.2));//1.2 System.out.println(Math.abs(0));//0 System.out.println("=========="); //向上取整 System.out.println(Math.ceil(3.1));//4.0 System.out.println(Math.ceil(3.5));//4.0 System.out.println(Math.ceil(3.9));//4.0 System.out.println(Math.ceil(3.999999999));//4.0 System.out.println("==============="); //向下取整 System.out.println(Math.floor(30.1));//30.0 System.out.println(Math.floor(30.9));//30.0 System.out.println(Math.floor(30.0));//30.0 System.out.println("==================="); //四舍五入(没有小数点) System.out.println(Math.round(30.1));//30 System.out.println(Math.round(30.5));//31 System.out.println(Math.round(30.0));//30 System.out.println("============="); System.out.println(Math.PI);//3.141592653589793 } }