Java——常用类(数组、常用类)

常用类

数组方法

length(数组长度)

例:

public class Test {
    public static void main(String[] args) {
        //String类型使用length()得到字符长度
        String a="继续码代码";
        System.out.println(a.length());//5
        //数组使用length得到数组元素的长度
        int[] b={1,2,3,4,5,6};
        System.out.println(b.length);//6
    }
}

equals()与Arrays.equals()

数组.equals(数组)
Arrays.equals(数组,数组)
例:

public class Test002 {
    public static void main(String[] args) {
        int[] a=new int[]{1,2,3,4};
        int[] b=new int[]{1,2,3,4};
        //使用==比较返回false
        boolean c=(a==b);
        System.out.println(c);//false
        //使用equals比较返回false
        System.out.println(a.equals(b));//false
        //使用Arrays.equals返回true
        System.out.println(Arrays.equals(a, b));//true
    }
}

Arrays.sort() 排序

例:

public class Test003 {
    public static void main(String[] args) {
        int[] a={5,3,4,8,55,8,6,5,255,55};
        //排序
        Arrays.sort(a);
        //增强for遍历
        for (int b:a){
            System.out.print(b);//34556885555255
        }
    }
}

Arrays.binarySearch(数组,元素):二分法查找

例:

public class Test004 {
    public static void main(String[] args) {
        int[] arr={1,2,35,8,8,35,6,22,8,82,66,55};
        //数组排序
        Arrays.sort(arr);
        //打印排好顺序的数组
        System.out.println(Arrays.toString(arr));
        //[1, 2, 6, 8, 8, 8, 22, 35, 35, 55, 66, 82]
        //查找排序后的数组中的元素
        int b=Arrays.binarySearch(arr,6);
        System.out.println(b);//2
    }
}

数组拷贝

Arrays. copyOf(源数组,长度)
例:

public class Test005 {
    public static void main(String[] args) {
        int[] arr={5,3,5,6,5,4,8,7};
        //复制一个指定长度的数组
        int[] arr1= Arrays.copyOf(arr,4);
        System.out.println(Arrays.toString(arr1));
        //运行结果[5, 3, 5, 6]
    }
}

System.arraycopy(源数组,源起始索引,目标数组,目标起始索引,长度)
例:

public class Test006 {
    public static void main(String[] args) {
        int[] arr={5,3,5,6,5,4,8,7};
        //把一指定起始位置指定长度的原数组中的元素复制到一个指定起始位置的新数组中
        int[] arr1=new int[10];
        System.arraycopy(arr,2,arr1,3,4);
        System.out.println(Arrays.toString(arr1));
        //运行结果为[0, 0, 0, 5, 6, 5, 4, 0, 0, 0]
    }
}

字符串相关类

①String
不可变的字符序列
②StringBuffer
可变字符序列,并且线程安全,但是效率低
③StringBuilder
可变字符序列,线程 不安全,但是效率高 (一般用它!)

String

可通过" "直接创建

常用方法

例:

public class Test007 {
    public static void main(String[] args) {
        String name="    向前冲出,那头熊出来聊聊";
        String name1="向前冲出,那头熊出来聊聊";
        String name2="a";
        String name3="A";
        //返回(char)字符串中第四个字符
        System.out.println(name.charAt(4));//向
        //返回(int)字符串的长度
        System.out.println(name.length());//16
        //返回(int)出现“出”的第一个位置
        System.out.println(name1.indexOf("出"));//3
        //返回(int)从第四个位置开始出现“出”的第一个位置
        System.out.println(name1.indexOf("出", 4));//8
        //返回(boolean)比较两个字符串是否一样(忽略大小写)
        System.out.println(name.equalsIgnoreCase(name1));//false
        //返回(String)将“前”替换成“后”
        System.out.println(name.replace("前", "后"));//    向后冲出,那头熊出来聊聊
        //返回(boolean)判断“向”是否与字符串第一个字符一致
        System.out.println(name.startsWith("向"));//false
        //返回(boolean)判断“聊”是否与字符串最后一个字符一致
        System.out.println(name.endsWith("聊"));//true
        //返回(String)将该字符串的大写形式
        System.out.println(name3.toUpperCase());//A
        //返回(String)将该字符串的小写形式
        System.out.println(name2.toLowerCase());//a
        //返回(String)第五个字符串开始到结尾的字符串
        System.out.println(name.substring(5));//前冲出,那头熊出来聊聊
        //返回(String)[3,7)的字符串
        System.out.println(name1.substring(3, 7));//出,那头
        //返回去掉开头结尾空格的字符串
        System.out.println(name.trim());//向前冲出,那头熊出来聊聊
        //返回(String[])字符串分割
        String[] a=name.split("熊", 3);
        System.out.println(Arrays.toString(a));//[    向前冲出,那头, 出来聊聊]
    }
}

StringBuffer和StringBuilder(可变字符序列)

例:

public class Test008 {
    public static void main(String[] args) {
        //可变字符序列
        StringBuilder bui=new StringBuilder("快乐每一天");
        StringBuilder bui1=bui.append("快乐每一天");
        System.out.println(bui);//快乐每一天快乐每一天
        System.out.println(bui1);//快乐每一天快乐每一天
        System.out.println(bui == bui1);//true
        //删除[2,5)的字符
        bui.delete(2,5);
        System.out.println(bui);//快乐快乐每一天
        //反转
        bui.reverse();
        System.out.println(bui);//天一每乐快乐快
        //插入一个字符“C”
        bui.insert(3,"C");
        System.out.println(bui);//天一每C乐快乐快
        //不可变转可变
        String arr="Hello";
        StringBuilder arr1=new StringBuilder(arr);
        //可变转不可变
        arr=arr1.toString();
    }
}

此处展示StringBuilder;StringBuffer用法与之类似 。

包装类

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

例:

public class Test009 {
    public static void main(String[] args) {
        //基本类型变量
        int a=10;
        //创建一个包装类对象
        Integer aw=new Integer(10);
        //数字转字符串
        aw=Integer.valueOf(10);
        System.out.println(aw);//10
        //字符串转数字
        aw=Integer.valueOf("10");
        System.out.println(aw);//10
        //基本类型和引用类型之间的转换(自动装箱、自动拆箱)
        //基本类型转引用类型
        Integer aw1=10;
        System.out.println(aw1);//10
        //引用类型转基本类型
        int arr=aw1;
        System.out.println(arr);//10

        //引用类型的增强
        //Integer包装类型最大值(2147483647)
        System.out.println(Integer.MAX_VALUE);//2147483647
        //Integer包装类型最小值(-2147483648)
        System.out.println(Integer.MIN_VALUE);//-2147483648
        //其中两个元素取最大值
        System.out.println(Integer.max(10, 100));//100
        //其中两个元素取最小值
        System.out.println(Integer.min(10, 100));//10
        System.out.println(Integer.max(Integer.valueOf(10), Integer.valueOf("10000")));//10000
        Integer see=100;
        Integer see1=100;
        System.out.println(see == see1);//true
        System.out.println(see.equals(see1));//true
        System.out.println(Integer.valueOf("100", 3));//9
    }
}

日期类

常用日期相关类

在这里插入图片描述
例:

public class Test010 {
    public static void main(String[] args) throws Exception {
        //获取当前的计算机系统时间和GMT时间(格林威治时间)
        // 1970年1月1号0时0分0秒所差的毫秒数
        long s=System.currentTimeMillis();
        //Date 时间,时间点
        //直接创建对象,获取当前时间点
        Date date=new Date();
        System.out.println(date);//Wed Jun 17 17:48:51 CST 2020
        
        //储存一个指定的时间,减少了60秒
        date=new Date(System.currentTimeMillis()-60*1000);
        System.out.println(date);//Wed Jun 17 17:47:51 CST 2020
        
        //字符串转日期
        String s1="2020年02月22号 12:30:00";
        //创建一个"yyyy年MM月dd号 HH:mm:ss"模板
        SimpleDateFormat sfm=new SimpleDateFormat("yyyy年MM月dd号 HH:mm:ss");
        Date birthday=sfm.parse(s1);
        System.out.println(birthday);//Sat Feb 22 12:30:00 CST 2020
        
        //日期转字符串
        //创建一个"yyyy/MM/dd HH点mm分ss秒"模板
        sfm=new SimpleDateFormat("yyyy/MM/dd HH点mm分ss秒");
        s1=sfm.format(birthday);
        System.out.println(s1);//2020/02/22 12点30分00秒
    }
}
JDK8常用API类
日期类

在这里插入图片描述
例:

public class Test012 {
    public static void main(String[] args) {
        //获取当前年份
        Year y=Year.now();
        System.out.println(y);//2020
        //获取月份
        Month m=Month.of(4);
        System.out.println(m);//APRIL
        m=Month.valueOf("APRIL");
        System.out.println(m);//APRIL
        System.out.println(Month.APRIL);//APRIL
        //获取今天的日期
        LocalDate today=LocalDate.now();
        System.out.println("今天:"+today);//今天:2020-06-17
        //构建本时区的时间(年月日)
        LocalDate today1=LocalDate.of(1999,Month.APRIL,20);
        System.out.println(today1);//1999-04-20
        //构建本时区的时间(年月日时分秒)
        LocalDateTime time=LocalDateTime.of(1999,Month.APRIL,20,12,34,33);
        System.out.println(time);//1999-04-20T12:34:33
    }
}
格式化类

在这里插入图片描述
例:

public class Test013 {
    public static void main(String[] args) {
        //日期转换器
        //把当前时间转换成模板格式时间
        DateTimeFormatter formatter1=DateTimeFormatter.ofPattern("yyyy年MM月dd HH:mm:ss");
        //格式化日期字符串
        LocalDateTime now1=LocalDateTime.now();
        //把模板放入格式器
        String now2=now1.format(formatter1);
        System.out.println(now2);
        //
        String date= "2020年12月12 12:12:12";
        //把模板放入格式器
        LocalDateTime date1=LocalDateTime.parse(date,formatter1);
        System.out.println(date1);
    }
}
间隔类

在这里插入图片描述
例:

public class Test014 {
    public static void main(String[] args) {
        //获取当前时间
        LocalDate today=LocalDate.now();
        //构建一个本时区时间2000年6月17日
        LocalDate birthDate=LocalDate.of(2000,6,17);
        //时期间隔(年月日)
        //两个时间段间隔多少时间
        Period p=Period.between(birthDate,today);
        //打印当前时间与2000年6月17日间隔时间
        System.out.println(p.getYears()+"年"+p.getMonths()+"月"+p.getDays()+"日");
    }
}

Math类

例:

public class Test011 {
    public static void main(String[] args) {
        //取整    绝对值 大数  小数  幂   平方根
        //Math.ceil():将小数部分一律向整数部分进位
        System.out.println(Math.ceil(3.2));//4.0
        //Math.floor():舍去小数,仅取整数部分
        System.out.println(Math.floor(3));//3.0
        //取绝对值
        System.out.println(Math.abs(-1));//1
        //取最大数
        System.out.println(Math.max(1, 10));//10
        //取最小数
        System.out.println(Math.min(1, 10));//1
        //幂函数
        System.out.println(Math.pow(2, 3));//8.0
        //开平方根
        System.out.println(Math.sqrt(9));//3.0
        //三角函数
        System.out.println(Math.sin(0));//0.0
        System.out.println(Math.sin(Math.PI / 2));//1.0
    }
}

File类

boolean canRead() ——返回文件是否可读
boolean canWrite() ——返回文件是否可写
boolean exists() ——判断文件夹是否存在
boolean isDirectory()—— 判断当前的目录是否存在
boolean isFile()—— 判断当前的目录是否是一个文件
boolean isHidden()—— 判断当前路径是否是一隐藏文件
long lastModified()—— 以毫秒值返回最后一次修改时间
long length()—— 返回文件的字节数
String getName()—— 获取文件或文件夹的名称
String getPath()—— 获取File对象中封装的路径
boolean createNewFile()—— 指定路径不存在该文件时创建文件,返回true 否则false
例1:

public class Test016 {
    public static void main(String[] args) throws  Exception {
        File file=new File("a.txt");
        //判断内存对象表示的文件是否存在
        System.out.println(file.exists());
        //创建一个新的文件
        file.createNewFile();
        //查看文件是否存在
        System.out.println(file.exists());
        //查看文件是否可读
        System.out.println(file.canRead());
        //查看文件是否可写
        System.out.println(file.canWrite());
        //查看文件是否隐藏
        System.out.println(file.isHidden());
        //返回最后一次修改文件的时间
        System.out.println(new Date(file.lastModified()));
    }
}

例2:

public class Test017 {
    public static void main(String[] args) {
        //File 文件的抽象对象(文件|文件夹)
        File file1=new File("out");
        //判断文件夹是否存在
        System.out.println(file1.exists());
        //获取文件的绝对路径,并返回路径的字符串
        System.out.println(file1.getAbsolutePath());
        //判断当前的目录是否存在
        System.out.println(file1.isDirectory());
        //判断当前的目录是否是一个文件
        System.out.println(file1.isFile());
        //返回文件的字节大小
        System.out.println(file1.length());
        //获取文件|目录的名字
        System.out.println(file1.getName());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值