JavaSE 第八章 Java常用类


活动地址:CSDN21天学习挑战赛

9.2 JDK8之前的日期时间API

9.2.1 java.util.Data类

表示特定的瞬间,精确到毫秒

  • 构造器:

    • Date():使用无参构造创建的对象可以获取本地当前时间

    • Date(long date):分配 Date对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数

  • 常用方法

    • getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象 表示的毫秒数。

    • toString():把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中:dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat),zzz是时间标准。

    • void setTime(long millis) : 设置当前时间对象指向的时间,先把时间归零,再加上传入的时间毫秒所定位的瞬间
    • 其它很多方法都过时了。
mport java.util.Date;

public class Demo2 {
    public static void main(String[] args) {
        Date date = new Date() ;
        System.out.println("date = " + date);   // date = Mon Aug 08 08:08:15 CST 2022

        Date date1 = new Date(122111111111L) ;  
        System.out.println("date1 = " + date1); // date1 = Wed Nov 14 15:45:11 CST 1973

        System.out.println(date1.getTime());    // 122111111111
        System.out.println(date1.toString());   // Wed Nov 14 15:45:11 CST 1973
    }
}

9.2.2 java.text.SimpleDateFromat类

  • Date类的API不易于国际化,大部分被废弃了。java.text.SimpleDateFormat 类是一个不与语言环境有关的方式来格式化和解析日期的具体类

  • 它允许进行格式化:日期-->文本解析:文本-->日期

  • 格式化:

    • SimpleDateFormat() :默认的模式和语言环境创建对象

    • public SimpleDateFormat(String pattern):该构造方法可以用参数pattern 指定的格式创建一个对象,该对象调用:

    • public String format(Date date):格式化时间对象date

  • 解析:

  • public Date parse(String source):从给定字符串的开始解析文本,以生成 一个日期。

在这里插入图片描述

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo3 {
    public static void main(String[] args) throws ParseException {
        Date date = new Date() ;
        SimpleDateFormat sdf = new SimpleDateFormat() ; // 无参,默认格式
        String format = sdf.format(date);
        System.out.println("format = " + format);   // format = 22-8-8 上午8:23

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd : hh:mm:ss sss") ; // 有参构造,格式化为指定格式
        String format1 = sdf1.format(date);
        System.out.println("format1 = " + format1);     // format1 = 2022-08-08 : 08:23:48 048

        String s = "2022-8-8 : 8:8:8 888" ;
        System.out.println(sdf1.parse(s));      // Mon Aug 08 08:22:48 CST 2022
    }
}

9.2.3 java.util.Calendar类

  • Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。

  • 获取Calendar实例的方法

    • 使用Calendar.getInstance()方法

    • 调用它的子类GregorianCalendar的构造器。

  • 一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想 要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、 MINUTE、SECOND

    • public void set(int field,int value)

    • public void add(int field,int amount)

    • public final Date getTime()

    • public final void setTime(Date date)

  • 注意:

    • 获取月份时:一月是0,二月是1,以此类推,12月是11

    • 获取星期时:周日是1,周二是2 , …周六是7

Calendar : 日历类 
	//时间文化差异 : 一周的第一天是周日 , 月份的索引 0 - 11
	
//创建日历类对象
Calendar calendar = Calendar.getInstance(); //多态 : 时间指向现在

成员方法 :
	获取时间信息 : int get(int field)
        //int field : 日历字段
	设置时间信息 : void set(int field,int value)
        void set(,,,,,)
        
转换方法:
	Calendar --> Date 
		Date getTime()
    Date --> Calendar
    	void setTime(Date date)
        
    Calendar --> long
    	long getTimeInMillis() 
 	long --> Date
 		void setTimeInMillis(long millis) 
import java.util.Calendar;
import java.util.Date;

public class Demo4 {
    public static void main(String[] args) {
        // Calendar是一个抽象类,不能创建对象,获取到的是子类对象
        Calendar cal = Calendar.getInstance() ;
        System.out.println("cal = " + cal);

        // 获取时间信息 int get(int field)
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println("dayOfWeek = " + dayOfWeek);
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        System.out.println("hourOfDay = " + hourOfDay);

        // 设置时间信息 void set(int field , int value)
        cal.set(Calendar.DAY_OF_WEEK , 5);
        cal.set(Calendar.HOUR_OF_DAY , 21);
        dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        System.out.println("dayOfWeek = " + dayOfWeek);
        System.out.println("hourOfDay = " + hourOfDay);
        // 转换方法
        // Calendar转Date  Date getTime()
        Date time = cal.getTime();
        System.out.println("time = " + time);

        // Date转Calendar  void setTime(Date date)
        cal.setTime(time);
        System.out.println("cal = " + cal);

        // Calendar转long   long getTimeUnMillis()
        long timeInMillis = cal.getTimeInMillis();
        System.out.println("timeInMillis = " + timeInMillis);

        // long转Date  void setTimeInMillis()
        Date date = new Date() ;
        date.setTime(timeInMillis);
        System.out.println("date = " + date);
    }
}

在这里插入图片描述

9.3 JDK8之后的日期时间API

9.3.1 LocalDateTime/LocalDate/LocalTime

  • LocalDateTime/LocalDate/LocalTime : 本地年月日时分秒/本地年月日/本地时分秒

  • LocalDateTime 为例 : LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒。 时间表示为纳秒精度.

  • 创建对象 :
    • static LocalDateTime now() : 获取LocalDateTime对象,并把时间指向现在

    • static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) : 获取LocalDateTime对象,并把时间指向指定的时间,这里的Mouth用的是int类型

    • static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) :与上述方法的不同点就在于这个方法的Mouth使用的是枚举类型

import java.time.LocalDateTime;
import java.time.Month;

public class Demo4 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        LocalDateTime dateTime = LocalDateTime.of(2022, 8, 8, 8, 8, 8);
        LocalDateTime dateTime1 = LocalDateTime.of(2022, Month.AUGUST, 8, 8, 8, 8);

        System.out.println("now = " + now);
        System.out.println("dateTime = " + dateTime);
        System.out.println("dateTime1 = " + dateTime1);

        /*
        now = 2022-08-09T18:44:45.282
        dateTime = 2022-08-08T08:08:08
        dateTime1 = 2022-08-08T08:08:08
        */
    }
}

9.3.2 DateTimeFormatter

  • LocalDateTime时间和字符串时间之间的转换问题 : 格式化和解析

  • DateTimeFormatter : 只提供日期格式化和解析格式的类

  • 获取DateTimeFormatter类的对象 :

    • static DateTimeFormatter ofPattern(String pattern) :String pattern : 日期格式代码
  • 格式化和解析的方法都来自于LocalDateTime类:

    • 格式化 : LocalDateTime --> String String format(DateTimeFormatter formatter)

    • 解析 : String --> LocalDateTime
      static LocalDateTime parse(String strTime,DateTimeFormatter formatter)

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Demo5 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now() ;   // 获取当前日期时间
        System.out.println("now = " + now);

        // 定义格式化格式/解析格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss SSS");
        String format = now.format(formatter);  // 格式化为指定格式
        System.out.println("format = " + format);
        String time = "2008年08月08日 08:08:08 888" ;
        LocalDateTime parse = LocalDateTime.parse(time, formatter); // 解析为时间
        System.out.println("parse = " + parse);
    }
}

now = 2022-08-09T18:54:18.201
format = 2022年08月09日 18:54:18 201
parse = 2008-08-08T08:08:08.888

9.3.3 LocalDateTime的until()方法(获取时间间隔)

  • long until(Temporal endExclusive, TemporalUnit unit)

    // Temporal endExclusive : 是LocalDateTime的父接口
    // TemporalUnit unit : 是时间单位的父接口
    // ChronoUnit : 时间单位的枚举类型

    • 格式 : 起始时间LocalDateTime对象.until(结束时间LocalDateTime对象,时间单位对象)
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Demo6 {
    public static void main(String[] args) {
        String time = "1999年09月09日 09:09:09" ;  // 出生日期
        // 解析格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);

        // 出生到现在一共过了多少天
        long until = dateTime.until(now, ChronoUnit.DAYS);
        System.out.println("until = " + until);     // until = 8370
    }
}

9.3.4 LocalDateTime的get系列方法(获取值)

  • int getYear() :获取年信息

  • int getMonthValue() :获取月份信息(1-12)

  • int getDayOfMonth(): 获取天信息

  • int getHour() :获取小时信息

  • int getMinute():获取分钟信息

  • int getSecond(): 获取秒钟信息


  • DayOfWeek getDayOfWeek():获取星期信息
  • int getDayOfYear():获取年中天信息
  • Month getMonth(): 获取月份信息(枚举对象返回)
  • int getNano():获取纳秒信息
import java.time.LocalDateTime;

public class Demo7 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        int year = now.getYear();
        System.out.println("year = " + year);
        int monthValue = now.getMonthValue();
        System.out.println("monthValue = " + monthValue);
        int dayOfMonth = now.getDayOfMonth();
        System.out.println("dayOfMonth = " + dayOfMonth);
        int hour = now.getHour();
        System.out.println("hour = " + hour);
        int minute = now.getMinute();
        System.out.println("minute = " + minute);
        int second = now.getSecond();
        System.out.println("second = " + second);

        System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek());
        System.out.println("now.getDayOfYear() = " + now.getDayOfYear());
        System.out.println("now.getMonth() = " + now.getMonth());
        System.out.println("now.getNano() = " + now.getNano());
        /*
        year = 2022
        monthValue = 8
        dayOfMonth = 9
        hour = 19
        minute = 18
        second = 34
        now.getDayOfWeek() = TUESDAY
        now.getDayOfYear() = 221
        now.getMonth() = AUGUST
        now.getNano() = 124000000
        */

    }
}

9.3.5 LocalDateTime的plus系列方法(加)

  • plus系列方法是对日期字段进行做加法的操作
  • LocalDateTime plusYears(long years) :加年

  • LocalDateTime plusMonths(long months) :加月份

  • LocalDateTime plusDays(long days) :加天数

  • LocalDateTime plusHours(long hours) :加小时数

  • LocalDateTime plusMinutes(long minutes) :加的分钟数

  • LocalDateTime plusSeconds(long seconds) :加秒数


  • LocalDateTime plusNanos(long nanos) :加毫秒数

  • LocalDateTime plusWeeks(long weeks) : 加星期数

  • 注意: 如果传入的是正数就是做加法,如果你传入的是负数就是做减法

import java.time.LocalDateTime;

public class Demo8 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        System.out.println("now.plusYears(10) = " + now.plusYears(10));
        System.out.println("now.plusMonths(10) = " + now.plusMonths(10));
        System.out.println("now.plusDays(10) = " + now.plusDays(10));
        System.out.println("now.plusHours(10) = " + now.plusHours(10));
        System.out.println("now.plusMinutes(10) = " + now.plusMinutes(10));
        System.out.println("now.plusSeconds(10) = " + now.plusSeconds(10));

    }
}

now = 2022-08-09T19:23:16.308
now.plusYears(10) = 2032-08-09T19:23:16.308
now.plusMonths(10) = 2023-06-09T19:23:16.308
now.plusDays(10) = 2022-08-19T19:23:16.308
now.plusHours(10) = 2022-08-10T05:23:16.308
now.plusMinutes(10) = 2022-08-09T19:33:16.308
now.plusSeconds(10) = 2022-08-09T19:23:26.308

9.3.6 LocalDateTime的minus系列方法(减)

  • minus系列方法是对日期字段进行做减法的操作

  • LocalDateTime minusYears(long years)

  • LocalDateTime minusMonths(long months)

  • LocalDateTime minusDays(long days)

  • LocalDateTime minusHours(long hours)

  • LocalDateTime minusMinutes(long minutes)

  • LocalDateTime minusSeconds(long seconds)


  • LocalDateTime minusNanos(long nanos)

  • LocalDateTime minusWeeks(long weeks)

  • 注意: 如果你传入的是正数就是做减法,如果你传入的是负数就是做加法

	@Test
    public void test1() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        System.out.println("now.minusYears(10) = " + now.minusYears(10));
        System.out.println("now.minusMonths(10) = " + now.minusMonths(10));
        System.out.println("now.minusDays(10) = " + now.minusDays(10));
        System.out.println("now.minusHours(10) = " + now.minusHours(10));
        System.out.println("now.minusMinutes(10) = " + now.minusMinutes(10));
        System.out.println("now.minusSeconds(10) = " + now.minusSeconds(10));

        /*
        now = 2022-08-09T19:26:59.652
        now.minusYears(10) = 2012-08-09T19:26:59.652
        now.minusMonths(10) = 2021-10-09T19:26:59.652
        now.minusDays(10) = 2022-07-30T19:26:59.652
        now.minusHours(10) = 2022-08-09T09:26:59.652
        now.minusMinutes(10) = 2022-08-09T19:16:59.652
        now.minusSeconds(10) = 2022-08-09T19:26:49.652
        */
    }

9.3.7 LocalDateTime的with系列方法(设置值)

  • with系列方法是对日期字段进行设置值的操作

  • LocalDateTime withYears(long years) :范围-999999999 - 999999999

  • LocalDateTime withMonths(long months) :范围1-12

  • LocalDateTime withDays(long days) :范围1-28/31

  • LocalDateTime withHours(long hours) :0-23

  • LocalDateTime withMinutes(long minutes) :0-59

  • LocalDateTime withSeconds(long seconds) :0-59


  • LocalDateTime withNanos(long nanos) :0-999999999
	@Test
    public void test2() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        System.out.println("now.withYear(10) = " + now.withYear(10));
        System.out.println("now.withMonth(10) = " + now.withMonth(10));
        System.out.println("now.withDayOfYear(10) = " + now.withDayOfYear(10));
        System.out.println("now.withHour(10) = " + now.withHour(10));
        System.out.println("now.withMinute(10) = " + now.withMinute(10));
        System.out.println("now.withSecond(10) = " + now.withSecond(10));

        /*
        now = 2022-08-09T19:30:16.606
        now.withYear(10) = 0010-08-09T19:30:16.606
        now.withMonth(10) = 2022-10-09T19:30:16.606
        now.withDayOfYear(10) = 2022-01-10T19:30:16.606
        now.withHour(10) = 2022-08-09T10:30:16.606
        now.withMinute(10) = 2022-08-09T19:10:16.606
        now.withSecond(10) = 2022-08-09T19:30:10.606
        */
        
    }

9.3.8 Period类

  • Period 代表年月日的时间间隔类

  • 得到Period对象 static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)

	@Test
    public void test3() {
        LocalDate now = LocalDate.now();

        String time = "1999年09月09日" ;  // 出生日期
        // 解析格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate dateTime = LocalDate.parse(time, formatter);

        Period period = Period.between(dateTime , now);
        System.out.println("period = " + period);   // period = P22Y11M
    }

9.3.9 Duration类

  • Duration 代表时分秒的时间间隔类

  • 得到Duration对象 static Duration between(Temporal startInclusive, Temporal endExclusive) :Temporal : 是LocalDateTime/LocalDate/LocalTime的父接口,语法上参数都可以接受; 但是在Duration的between方法的逻辑上不可以接收LocalDate的;

  • to系列方法 : 计算时间间隔对象中表示的时间值

    • toDays() : 计算间隔了多少天
	@Test
    public void test4() {
        LocalDateTime now = LocalDateTime.now();

        String time = "1999年09月09日 09:09:09" ;  // 出生日期
        // 解析格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);

        Duration duration = Duration.between(dateTime , now);
        System.out.println("duration = " + duration);   // duration = PT200890H31M53.658S
        System.out.println("duration.toDays() = " + duration.toDays());     // duration.toDays() = 8370
    }

9.4 System类

  • System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。 该类位于java.lang包。

  • 由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实 例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便 的进行调用。

  • 成员变量

    • System类内部包含in、out和err三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。

在这里插入图片描述

  • 成员方法

    • native long currentTimeMillis(): 该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。

    • void exit(int status): 该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表 异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。

    • void gc(): 该方法的作用是请求系统进行垃圾回收。至于系统是否立刻回收,则 取决于系统中垃圾回收算法的实现以及系统执行时的情况。

    • String getProperty(String key): 该方法的作用是获得系统中属性名为key的属性对应的值。

系统中常见的属性名以及属性的作用如下表所示:
在这里插入图片描述

public class Demo2 {
    public static void main(String[] args) {
        String property = System.getProperty("java.version");
        System.out.println("java.version = " + property);   // java.version = 1.8.0_221

        String property1 = System.getProperty("java.home");
        System.out.println("java.home = " + property1);     // java.home = C:\Program Files\Java\jdk1.8.0_221\jre
    }
}

9.5 Math类

Math : 关于数学操作的工具类
常量 :
public static final double E = 2.7182818284590452354; : 自然对数的底数
public static final double PI = 3.14159265358979323846; : 圆周率
静态方法 :
    static int abs(int a) : 求a的绝对值
    static double cbrt(double a) : 求a的立方根
    static double ceil(double a) : 对a进行向上取整 -> 天花板
    static double floor(double a) : 对a进行向下取整 -> 地板
    static long round(double a) : 四舍五入
    static int max(int a, int b) : 求ab的最大值
    static int min(int a, int b) : 求ab的最小值
    static double pow(double a, double b) : 求a的b次幂
    static double random() : 求随机数的 -> [0.0,1.0)

9.6 BigInteger与BigDecimal

  • Integer类作为int的包装类,能存储的最大整型值为2 31-1,Long类也是有限的, 最大为2 63-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。
  • java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供 所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。
  • 构造器

    • BigInteger(String val):根据字符串构建BigInteger对象
  • 常用方法
    • public BigInteger abs():返回此 BigInteger 的绝对值的BigInteger。

    • BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger

    • BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger

    • BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger

    • BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。

    • BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。

    • BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val)后跟(this % val) 的两个 BigInteger 的数组。

    • BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

  • 一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中, 要求数字精度比较高,故用到java.math.BigDecimal类。

  • BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

  • 构造器

    • public BigDecimal(double val)
    • public BigDecimal(String val) 推荐使用
  • 常用方法
    • public BigDecimal add(BigDecimal augend) :加

    • public BigDecimal subtract(BigDecimal subtrahend) :减

    • public BigDecimal multiply(BigDecimal multiplicand) :乘

    • public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) : 除

9.7 Arrays类

Arrays : 关于数组操作的工具类

  • 静态方法:
    • static String toString(Object[] a) : 打印数组

    • static int binarySearch(int[] a, int key) : 查找key元素在a数组中出现的索引位置 //原理 : 二分法查找 -> 前提 : 先把数组调整有序

    • static int[] copyOf(int[] original, int newLength) : 把original数组中的元素复制到一个新的长度为newLength的数组中,并把新数组返回

    • static int[] copyOfRange(int[] original, int from, int to) : 把老数组的一部分元素复制 到新数组中

    • static boolean equals(int[] a, int[] a2) : 比较2个数组的内容是否相同

    • static void fill(int[] a, int val) : 把a数组中的元素全部填充为 val

    • static void sort(int[] a) : 对a数组进行排序 //原理 : 快速排序

    • static void sort(Object[] a) : 对a数组进行排序 //注意 : 如果往sort方法内填入的是引用数据类型数组,要求传入的数组元素类型必须提供 排序规则

import java.util.Arrays;

public class Demo1 {
    public static void main(String[] args) {

        char[] arr = {'a' , 'b' , 'c' , 'd' , 'e'} ;
        int[] arr1 = {1 , 5 , 9 , 77 , 100} ;
//        static String toString(Object[] a) : 漂亮的打印数组
        String s = Arrays.toString(arr);
        System.out.println("s = " + s);

//        static int binarySearch(int[] a, int key) : 查找key元素在a数组中出现的索引位置 //原理 : 二分法查找 -> 前提 : 先把数组调整有序
        System.out.println(Arrays.binarySearch(arr1, 77));

//        static int[] copyOf(int[] original, int newLength) : 把original数组中的元素复制到一个新 的长度为newLength的数组中,并把新数组返回
        char[] copyOf = Arrays.copyOf(arr, 10);
        String s1 = Arrays.toString(copyOf);
        System.out.println("s1 = " + s1);

//        static int[] copyOfRange(int[] original, int from, int to) : 把老数组的一部分元素复制 到新数组中
        int[] ints = Arrays.copyOfRange(arr1, 1, 3);
        String s2 = Arrays.toString(ints);
        System.out.println("s2 = " + s2);

//        static boolean equals(int[] a, int[] a2) : 比较2个数组的内容是否相同
        int[] arr2 = {1 , 5 , 9 , 77 , 100} ;
        boolean equals = Arrays.equals(arr1, arr2);
        System.out.println("equals = " + equals);

//        static void fill(int[] a, int val) : 把a数组中的元素全部填充为 val
        Arrays.fill(arr1 , 100) ;
        System.out.println("arr1 = " + Arrays.toString(arr1));

//        static void sort(int[] a) : 对a数组进行排序 //原理 : 快速排序
        int[] arr3 = {5 , 7 , 1 , 3 , 4 , 100} ;
        Arrays.sort(arr3);
        System.out.println("arr3 = " + Arrays.toString(arr3));
        
//        static void sort(Object[] a) : 对a数组进行排序 //注意 : 如果往sort方法内填入的是引用数据类型数组,要求传入的数组元素类型必须提供 排序规则
    }
}

/**

s = [a, b, c, d, e]
3
s1 = [a, b, c, d, e,  ,  ,  ,  ,  ]
s2 = [5, 9]
equals = true
arr1 = [100, 100, 100, 100, 100]
arr3 = [1, 3, 4, 5, 7, 100]

*/

9.8 Objects类

Objects : 关于对象操作的工具类型 , 喜欢做非空校验

静态方法 :

  • static boolean equals(Object a, Object b) : 对a进行非空校验,再调用a的equals方法
    源码 : return (a == b) || (a != null && a.equals(b));

  • static boolean isNull(Object obj) : 判断obj是否为null

  • static boolean nonNull(Object obj) : 判断obj是否不为null

  • static int hashCode(Object o) : 对o进行非空校验,再调用o的hashCode方法

  • static String toString(Object o) : 对o进行非空校验,再调用o的toString方法

  • static String toString(Object o, String nullDefault) :对o进行非空校验,再调用o的toString方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值