Java基础:Arrays类与Math类及其常用方法

Arrays类

package demo02;
import java.util.Arrays;

将数组变成字符串用toString,字符串变数组用toCharArray。
Arrays类位于java.util包下,是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组的常见的操作。
* Arrays类的常用方法:
public static String toString(数组);将参数数组变成字符串,格式:[s,d,a]
public static void sort();将参数数组升序(从小到大)排序,数字从小到大,字母从a到z
备注:如果是数值,从小到大
如果是字符串,按字母升序
如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口支持。
* */
public class Demo01Arrays {
    public static void main(String[] args) {
        //参数数组变成字符串
        int[] arr = {1,2,3};
        String str = Arrays.toString(arr);
        System.out.println(str);
        //排序并打印
        int[] arr1 = {1,24,546,5,23,63,};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));
    }

}
Arrays类常用方法练习
package demo02;
import java.util.Arrays;
/*使用Arrays相关API,将一个随机字符串中的所有字符升序排列,并倒序打印
* 思路:
* 升序排列,倒序打印 sort,toString,forr。
* */
public class Demo03ArraysTest {
    public static void main(String[] args) {
        String str = "adagguia214525436IKGIGI";
        //字符串变数组
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        //倒序输出每一项
        for (int i = chars.length - 1; i >= 0; i--) {
            System.out.print(chars[i]);
        }
    }
}

Math类

package demo02;

* Math类位于java.util包下,是数学相关的工具类,里面提供大量的静态方法,完成与数学运算相关的操作。
* public static double abs(double num):获取绝对值
* public static double ceil(double num);向上取整
* public static double floor(double num);向下取整
* public static long round(double num);四舍五入,本质是加0.5取整。
* Math.PI圆周率。
* */
public class Demo02Math {
    public static void main(String[] args) {
        //取绝对值
        double a = 32.0;
        double b = -12.5;
        double a1 = Math.abs(a);
        double b1 = Math.abs(b);
        //向下取整
        double c = 10.6;
        double d = 12.9;
        double c1 = Math.ceil(c);
        double d1 = Math.ceil(d);
        //向上取整
        double e = 11.1;
        double f = 18.9;
        double e1 = Math.floor(e);
        double f1 = Math.floor(f);
        //四舍五入
        double g = 12.4;
        double h = 19.9;
        double g1 = Math.round(g);
        double h1 = Math.round(h);
        System.out.println(a1);
        System.out.println(b1);
        System.out.println(c1);
        System.out.println(d1);
        System.out.println(e1);
        System.out.println(f1);
        System.out.println(g1);
        System.out.println(h1);
    }
}
Math类常用方法练习
package demo02;
题目:
计算在-10.85.9之间,绝对值大于6或者小于2.1的整数有多少个?
分析:
确定了范围,for
起点位置-10.8转化成-10,两种办法:
1.可以使用Math.ceil向上取整
2.强转成int,自动舍弃所有小数位
每个数字都是整数,所以步进表达式是num++,这样每次都是+1的
一旦发现一个数字,需要计数器++
* */
public class MathTest {
    public static void main(String[] args) {
        //符合要求的数量
        int count = 0;
        double max = 5.9;
        double min = -10.8;
        //这样处理i就是区间里的所有整数
        for (int i = (int)min; i < max;i++){
            int abs = Math.abs(i);
            if(abs > 6 || abs < 2.1){
                System.out.println(i);
                count++;
            }
        }
        System.out.println("符合要求的数的个数为:" + count);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值