Java-10-Arrays类和Math类

14. Arrays 类

java.util.Arrays 是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。

package com.tipdm.demo06;

import java.util.Arrays;

/**
 * java.util.Arrays 是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。
 *
 * public static String toString(数组):将参数数组变成字符串(按照默认格式:[元素1, 元素2, 元素3...])
 * public static void sort(数组):按照默认升序(从小到大)对数组的元素进行排序
 *
 * 备注:
 * 1. 如果是数值,sort默认按照升序从小到大
 * 2. 如果是字符串,sort默认按照字母升序
 * 3. 如果是自定义类型,那么自定义的类需要有Comparable或者Comparator接口的支持。(今后学习)
 */
public class demo1 {
    public static void main(String[] args) {
        int[] inArray = {10, 20, 30};
        // 将int[]数组按照默认格式变成字符串
        String intStr = Arrays.toString(inArray);
        System.out.println(intStr);  // [10, 20, 30]

        int[] array1 = {2, 3, 1, 5, 8};
        Arrays.sort(array1);
        System.out.println(Arrays.toString(array1));  // [1, 2, 3, 5, 8]

        String[] array2 = {"bbb", "aaa", "ccc"};
        Arrays.sort(array2);
        System.out.println(Arrays.toString(array2));
    }
}

14.1 题目:请使用Arrays相关的API,将一个随机字符中的所有字符升序排列,并倒序打印。

package com.tipdm.demo06;

import java.util.Arrays;

/**
 * 题目:
 * 请使用Array2相关的API,将一个随机字符中的所有字符升序排列,并倒序打印。
 */
public class demo2 {
    public static void main(String[] args) {
        String str = "saucxkzjqwfuiosboppwequiow";
        char[] charArr = str.toCharArray();

        Arrays.sort(charArr);

        for (int i = charArr.length - 1; i >= 0; i--) {
            System.out.print(charArr[i] + " ");
        }  // z x w w w u u u s s q q p p o o o k j i i f e c b a
    }
}

15. Math 类

package com.tipdm.demo06;

/**
 * java.util.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 近似的圆周率
 */
public class demo3 {
    public static void main(String[] args) {
        // 获取绝对值
        System.out.println(Math.abs(-23.2));  // 23.2

        // 向上取整
        System.out.println(Math.ceil(23.1));  // 24.0

        // 向下取整
        System.out.println(Math.floor(23.9));  // 23.0

        // 四舍五入
        System.out.println(Math.round(23.4));  // 23
        System.out.println(Math.round(23.5));  // 24

        // 圆周率
        System.out.println(Math.PI);
    }
}

15.1 题目:计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?

package com.tipdm.demo06;

import java.util.ArrayList;

/**
 * 题目:
 * 计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
 */
public class demo4 {
    public static void main(String[] args) {
        double minDouble = -10.8;
        double maxDouble = 5.9;
        double min = Math.ceil(minDouble);
        double max = Math.floor(maxDouble);
        int i = (int) min;
        ArrayList<Integer> listInt = new ArrayList<>();
        while(i <= max){
            if(Math.abs(i) > 6 || Math.abs(i) < 2.1){
                listInt.add(i);
            }
            i++;
        }
        System.out.println("总共有:" + listInt.size() + "个");  // 9个
        System.out.println(listInt);  // [-10, -9, -8, -7, -2, -1, 0, 1, 2]
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值