Day33常用API

常用API

  1. Math类的常用方法

    方法名说明
    public static int abs(int a)返回参数的绝对值
    public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数
    public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数
    publci static int round(float a)按照四舍五入返回最接近参数的int
    public static int max(int a, int b)返回两个int值中的较大值
    public static int min (int a, int b)返回两个int值中的较小值
    public static double pow(double a, double b)返回a的b次幂的值
    public static double random()返回值为double的正值,[0.0 , 1.0]
public class MathDemo{
    public static void main(String[] args){
        //返回参数绝对值
        System.out.println(Math.abs(-88));//结果88
        //返回大于或者等于参数的最小double值,等于一个整数
        System.out.println(Math.ceil(12.34));//结果13
        System.out.println(Math.ceil(12.58));//结果13
        //返回小于或者等于参数的最大double值,等于一个整数
         System.out.println(Math.floor(12.34));//结果12
         System.out.println(Math.floor(12.58));//结果12
        //按照四舍五入返回最接近参数的int
        System.out.println(Math.round(12.34F));//结果12
        System.out.println(Math.round(12.58F));//结果13 
        //返回两个int值中的较大值
        System.out.println(Math.max(66,88));//结果88
        //返回a的b次幂
        System.out.println(Math.pow(2.0 ,3.0));//结果8.0
        //返回值为double的正值[0.0 , 1.0)
        System.out.println(Math.random());//结果随机0.8540476900537749
    }
}
  1. System类,System包含几个有用的类字段和方法,它不能被实例化。

    方法名说明
    public static void exit (int status)终止当前运行的Java虚拟机,非零表示异常终止
    public static long currentTimeMillis ()返回当前时间(以毫秒为单位)
    //System类的常用方法
    public class SystemDemo{
        public static void main(String[] args){
            System.out.println("开始");
            //终止当前运行Java虚拟机,非零表示异常终止
            System.exit(0);
            System.out.println("结束");    //结果“开始”
            
            //返回当前时间(以毫秒为单位)
            System.out.println(System.currentTimeMillis());
            //返回当前时间和1970年之间的毫秒值  (1681367238743)
            System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365 + "年");
            //计算从1970到如今多少年   53.31581219970827年
        }
    }
  2. Object类是类层次的根,每个类都可以将Object作为超类,所有类都直接或者间接继承自该类

    构造方法:public Object()

    方法名说明
    public String toString()返回对象的字符串表示形式。建议所有子类重写该方法,自动生成
    public boolean equals (Object obj)比较对象是否相等。默认比较地址,重写可以比较内容,自动生成
  3. Arrays类包含用于操作数组的各种方法

    方法名说明
    public static String toString (int [] a)返回指定数组的内容的字符串表示形式
    public static void sort (int [] a)按照数字顺序排列指定的数组

    //工具类的设计思想:构造方法用private修饰(防止外界传输对象) 成员方法用public static修饰(使用类名就可以访问)

    public class ArraysDemo{
        public static void main(String[] args){
            //定义一个数组
            int[] arr = {24, 69, 80, 57, 13};
            System.out.println("排序前:"+ Arrays.toString(arr));
            Arrays.sort(arr);
            System.out.println("排序后:"+ Arrays.toString(arr));
        }
    }          //结果:排序前:[24, 69, 80, 57, 13]   排序后:[13, 24, 57, 69, 80]

    // 冒泡排序:有n个数据进行排序,总共需要比较n-1次,每次比较完,下一次比较就会少一个数据参与。

public class ArrayDemo{
    public static void main(String[] args){
        //定义一个数组
        int[] arr = {24, 69, 80, 57, 13};
        System.out.println("排序前:"+ arrayToString(arr));
        //因为一共要比较4次(也就是arr.length-1)
       for(int x=0 x<arr.length-1; x++){
           for(int i=0; i<arr.length-1-x; i++){
               if(arr[i] > arr[i+1]){
                   int temp = arr[i];
                   arr[i] = arr[i+1];
                   arr[i+1] = temp;
               }
           }
       } 
        System.out.println("排序后:"+ arryToString(arr));
    }
}          //结果:排序前:[24, 69, 80, 57, 13]   排序后:[13, 24, 57, 69, 80]
  1. 基本类型包装类,将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据,常用的操作之一:用于基本数据类型与字符串之间的转换。

    基本数据类型包装类
    byteByte
    shortShort
    intInteger
    longLong
    floatFloat
    doubleDouble
    charCharacter
    booleanBoolean

    //Integer:包装一个对象中的原始类型int的值

    方法名说明
    public Integer(int value)根据int值创建Integer对象(过时)
    public Integer(String s)根据String值创建Integer对象(过时)
    public static Integer valueOf(int i)返回表示指定的int值的Inter实例
    public static Integer valueOf(String s)返回一个保存指定值的Integer对象String

    //int和String转换

    public class IntegerDemo{
        public static void main(String[] args){
            //int转换String
            int number = 100;
            //方式1
            String s1 = "" + number;
            System.out.println(s1);
            //方式2
            String s2 = String.valueOf(number);
            System.out.println(s2);
            
            //String转换成int类型
            String s = "100";
            //方式1  String----Integer----int
            Integer i = Integer.valueOf(s);
            int x = i.intValue();
            System.out.println(x); 
            //方式2 
            int y = Integer.parseInt(s);
            System.out.println(y);
        }
    }

    //1. int转换为String : public static String valueOf(int i):返回int参数的字符串表示形式。该方法时String类中的方法。 2.String 转换为int :public static int parseInt (String s) : 将字符串解析为int类型,该方法时Integer类中的方法。

    //案例字符串中数据排序

    public class IntegerTest{
        public static void main(String[] args){
            //定义一个字符串
            String s = "91 27 46 38 50";
            //把字符串中的数字数据存储到一个int类型的数组中
            String[] strArray = s.split("");
            //定义一个int数组,把String[]数组中的每一个元素存储到int数组中
            int[] arr = new int[strArray.length];
            for(int i = 0; i<arr.length; i++){
                arr[i] = Integer.parseInt(strArray[i]);
            }
            //对int数组进行排序
            Arrays.sort(arr);
            //把排序后的int数组中的元素进行拼接得到一个字符串,这里拼接采用StringBuilder来实现
            StringBuilder sb = new StringBuilder();
          for(int i = 0; i<arr.length; i++){
              if(i == arr.length-1){
                  sb.append(arr[i]);
              }else{
                  sb.append(arr[i]).append("");
              }
          }  
            String result = sb.toString();
            //输出结果
            System.out.println("result:" + result);
        }
    }

    //自动装箱和拆箱

    装箱:把基本数据类型转换为对应的包装类类型 拆箱:把包装类类型转换为对应的基本数据类型。

    //如果要生成一个数值为10的Integer对象 Integer i = new Integer(10); 而装箱是: Integer i = 10; 拆箱:int n = i;

public class IntegerDemo{
    public static void main(String[] args){
        //装箱:把基本数据类型转换为对应的包装类类型
        Integer i = Integer.valueOf(100);
        Integer ii = 100;
        //拆箱:把包装类类型转换为对应的基本数据类型
        //ii += 200;
        ii.inValue() + 200; 自动拆箱
        System.out.println(ii);//结果为300
        
        Integer iii = null;
        if(iii != null){
        iii += 300;
        }
    }
}

//注意:在使用包装类类型的时候,如果做操作,最好先判断是否为null;推荐只要是对象,在使用前就必须进行不为null的判断。

//Date类代表一个特定时间,精确到毫秒

方法名说明
public Date()分配一个Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
public Date (long date)分配一个Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
public class DateDemo01{
    public static void main(String[] args){
        //public Date():分配一个Date对象,并初始化,以便它代表它分配的时间,精确到毫秒
        Date d1 = new Date();
        System.out.println(d1);//结果:Sun Aug 09 09:29:39 CST 2023
        //public Date(long date):分配一个Date对象,并将其初始化为代表从标准基准时间起指定的毫秒数
        long date = 1000*60*60;
        Date d2 = new Date(date);
        System.out.println(d2);//结果:Thu Jan 09:00:00 CST 1970
    }
}
  1. 日期类:

    //SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

    常用的模式字母对应关系:

    yMdHms

    SimpleDateFormat的构造方法

    方法名说明
    public SimpleDateFormat()构造一个SimpleDateFormat,使用默认模式和日期格式
    public SimpleDateFormat(String pattern)构造一个SimpleDateFormat使用给定的模式和默认的日期格式

    //格式化(从Date到String):public final String format(Date date):将日期格式化成日期/时间字符串

    //解析(从String到Date):public Date parse(String source):从给定字符串的开始解析文本以生成日期

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class SimpleDateFormatDemo {
        public static void main(String[] args) throws ParseException {  //抛出异常
            //格式化:从Date到String
            //首先需要一个Date对象(日期)
            Date d = new Date();
            //调用format方法,方法在SimpleDateFormatDemo类中,需要创建对象
            //创建对象先使用无参构造
           // SimpleDateFormat sdf = new SimpleDateFormat();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String s = sdf.format(d);
            System.out.println(s);
            System.out.println("-----------");
    
            //从String到Date
            String ss = "2023-04-14-09:57:56";
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
            Date dd = sdf2.parse(ss);
            System.out.println(dd);
        }
    }
    //结果
    2023年04月14日 10:01:21
    -----------
    Fri Apr 14 09:57:56 CST 2023

//Calendar类也被称为日历类,Calendar为某一时刻和一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法。Calendar提供了一个类方法getInstance用于获取Caledar对象,其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();

import java.util.Calendar;

public class CalendarDemo {
    public static void main(String[] args) {
        //获取对象
        Calendar c = Calendar.getInstance();//多态的形式
        System.out.println(c);
        //public int get (int field)
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
    }
}

//Calendar的常用方法

方法名说明
public int get (int field)返回给定日历字段的值
public abstract void add (int field, int amount)根据日历规则,将指定的时间量添加或减去给定的日历字段
public final void set (int year ,int month, int date)设置当前日历的年月日

//求任意年的二月份天数

import java.util.Calendar;
import java.util.Scanner;
​
//键盘录入任意年份,设置3月,从零开始设置值为2,日设置为1,往前推一天就是2月份天数
public class CalendarTest {
    public static void main(String[] args) {
        //键盘录入任意年份
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year = sc.nextInt();
        //设置日历对象的年,月,日
        Calendar c = Calendar.getInstance();
        c.set(year,2,1);
        //3月1日往前推一天,就是2月的最后一天
        c.add(Calendar.DATE,-1);
        //获取这一天的输出
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年的2月份有" + date + "天");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值