Arrays类和Math类

Arrays类

概述:

在java.util.Arrays此类主要是用来操作数组的,里面提供了很多操做的api方法,如排序和搜索功能,其所有的方法均为静态方法,调用起来非常简单

操作数组的方法

  • public static String toString(int[] a)   
    

    ​ 返回指定数组内容的字符串形式表示

public static void main(String[] args){
    //初始化一个int类型的数组 动态初始化至指定长度,不指定内容,静态初始化只指定内容,不指定长度
    int[] arr = {12,23,34,5,78,16};
    //打印数组,输出的直接是内存地址值
    System.out.println(arr);//地址值
    //把数组内容转换成字符串
    String str = Arrays.toString(arr);
    //打印字符串 输出数组内容
    System.out.println(str);//[12,23,34,5,78,16]  内容顺序不变
}
  • public static void sort(int[] arr)  
    

    ​ 对指定的int数组按照数组升序进行排序,从小到大

public static void main(String[] args) {
    int[] arr = {12,23,34,5,78,16};
    //输出排序前的内容
     System.out.println(Arrays.toString(arr));//[12,23,34,5,78,16]
    //进行升序排序
    Arrays.sort(arr);
   System.out.println(Arrays.toString(arr););//[5,12,16,23,34,78]
}

练习:使用Arrays相关的api方法,将一个任意给定的字符串中的所有字符按升序排序,倒叙打印输出

import java.util.Arrays;

public class ArraysDemo01 {

    public static void main(String[] args) {
        //任意给定字符串
        String str = "drtDDghYT7878";
        //转换成char数组
        char[] chs = str.toCharArray();
        //使用sort方法
        Arrays.sort(chs);
        //倒叙打印输出 反向遍历
        for (int i = chs.length - 1; i >= 0; i--) {
            System.out.print(chs[i] + " ");
        }
    }
}
//结果:t r h g d Y T D D 8 8 7 7 

升序 ascending 简称asc 降序 descending 简称 desc dst 目的地 src来源

  • public static int binarySearch(int[] a, int key)   
    

    ​ 使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。此时key指的是要搜索的值,而返回值 声明的int指的是搜索值对应的索引值
    ​ 使用binarySearch()方法搜索数组当中的元素是,数组必须是有序的。升序
    ​ 得到的索引值是排序之后的新的数组索引值
    ​ 如果没有找到对应的索引值,得到的索引值是负值。

 public static void main(String[] args) {
     //定义一个int类型的数组
     int[] arr = {10,20,5,30,40,15,18};
     //对arr数组排序
     Arrays.sort(arr);
     //搜索5这个数值在arr数组当中的索引值
     int index = Arrays.binarySearch(arr,5);
     //输出index的值
     System.out.print("5这个数值在arr数组当中的索引值" + index);
     
 }
//结果:未排序:-1   排序之后:0
  • public static void fill(int[] arr,int val)
    

    ​ 将指定的 int 值分配给指定 int 型数组的每个元素

public static void main(String[] args){
    //定义int类型的数组
     int[] arr = {10,20,5,30,40,15,18};
    //想要把100元素值分配给arr数组
    Arrays.fill(arr,100);//填满数组当中的每一个元素
    //打印输出
    System.out.print(Arrays.toString(arr));
}
//结果:[100, 100, 100, 100, 100, 100, 100]

Math类

概述

java.lang.Math 包含了用于执行基本的数学运算的方法,如:指数,幂次方,求对数,平方根,三角函数等运算。里面的方法均是静态方法,不需要创建对象,调用起来方便

基本运算方法
  • public static double abs(double a)
    

    ​ 返回double值的绝对值

double d1 = Math.abs(-5.3);//值为5.3
double d2 = Math.abs(5.3);//值为5.3
double d3 = Math.abs(0.0);//值为0.0
  • ceil(double a)

    public static double ceil(double a)
    

    ​ 返回大于等于参数的最小整数。往上取整

double d1 = Math.ceil(5.3);//值为6.0
double d2 = Math.ceil(6);//值为6.0
double d3 = Math.ceil(-5.9);//值为-5.0
  • public static double floor(double a)
    

    ​ 返回小于等于参数的最大整数。往下取整

double d1 = Math.floor(5.9);//5.0
double d2 = Math.floor(5.1);//5.0
double d3 = Math.floor(-5.9);//-6
  • public static long round(double a)
    public static int round(float a)
    

    ​ 返回最接近参数的int类型值。相当于四舍五入

long d1 = Math.round(5.5);//6
long d2 = Math.round(5.4);//5
long d3 = Math.round(-5.5);//-5
long d4 = Math.round(-5.6);//-6

练习:使用Mathi相关的API方法,计算-10.8到5.9之间,,绝对值大于6或者小于2.1的整数有多少个

public static void main(String[] args) {
    int count = 0;
    double max = 5.9;
    double min = -10.8;
    for (double i = Math.ceil(min); i <= Math.floor(max); i++){
            //判断
        if (Math.abs(i) > 6 || Math.abs(i) < 2.1){
            count++;
            System.out.print(i + " ");
            }
        }

        System.out.println();
        System.out.println(count)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值