Java面向对象 学习笔记 (三) 常用API

1.常用API(String)

  • 字符串的比较不适合用"=="比较。
    String s1,s2,s;
  • s1.equals(s2); 比较s1 与 s2 是否相等,如果相等 为 true 否则为 false
  • s1.equalsIgnoreCase(s2); 比较s1 与 s2 是否相等忽略大小写,如果相等 为 true 否则为 false
  • s.length(); 获取字符串长度
  • s.charAt(i); 获取i索引位置的字符
  • s.toCharArray(); 把字符串转换成字符数组
  • s.substring(0,9); 截取字符串0到8的字符(包前不包后)
  • s.replace(“XXX”,“YYY”); 替换 把字符串中的XXX 替换为YYY
  • s.contains(“xxx”); 判断xxx是否在字符串s内
  • s.startWith(“xxx”); 是否以xxx 开始
  • s.split(“,”); 按照某个内容把字符串分割成字符串数组返回
//例如
String name = "张三,李四,王五";
String[] names = name.split(",");
name 字符串被分割为
    names[0] = "张三";
	names[1] = "李四";
	names[2] = "王五";
的数组;

2.常用API(ArrayList)

  • list.add(“Java程序员”); 往list集合中添加数据
  • list.get(i); 获取i位置的值
  • list.size(); 获取元素个数
  • 集合的遍历
  for(int i=0; i<list.size();i++){
  		System.out.println(list.get(i));
  }
  • list.remove(i); 删除i位置上的元素
  • list.remove(“xxx”); 删除list中xxx这个值(如果xxx重复只删除前面一个) 如果成功返回true 否则返回false
  • list.set(i,“xxx”); 把i位置的值修改为xxx
//泛型
List<String> List = new ArrayList<>();//只能存储String类型的值
List<Double> List = new ArrayList<>();//只能存储浮点类型的值
List<Integer> List = new ArrayList<>();//只能存储整数类型的值
List<T> List = new  ArrayList<>();//支持泛型 自己封装的类型也可以

3.常用API(StringBuilder)

  • public StringBuilder() 创建一个空白的可变的字符串对象,不包含任何内容
  • public StringBuilder(String str) 创建一个指定字符串内容的可变字符串对象
  • public StringBuilder append(任意类型) 添加数据并返回StringBuilder对象本身//支持链式编程
  • public StringBuilder reverse() 将对象的内容反转
  • public int length() 返回对象内容长度
  • public String toString() 通过toString()就可以实现把StringBuilder转换为String
StringBuilder 是一个可变的字符串类,我们可以把它当成一个对象容器
    //作用:提高字符串的操作效率,如拼接,修改等。
//StringBuilder构造器
         名称                                                说明
public StringBuilder()                    创建一个空白的可变的字符串对象,不包含任何内容
public StringBuilder(String str)          创建一个指定字符串内容的可变字符串对象
    
//StringBuilder常用方法
		方法名称                                             说明
public StringBuilder append(任意类型)            添加数据并返回StringBuilder对象本身//支持链式编程
public StringBuilder reverse()                            将对象的内容反转
public int length()                                       返回对象内容长度
public String toString()                    通过toString()就可以实现把StringBuilder转换为String
    
/**    
1、为什么拼接、反转字符串建议使用StringBuilder?
string :内容是不可变的、拼接字符串性能差。
StringBuilder:内容是可变的、拼接字符串性能好、代码优雅。
定义字符串使用String
拼接、修改等操作字符串使用StringBuilder
*/
//StringBuilder的使用案例
public class StringBuilderTest {
    public static void main(String[] args) {
        int[] arr = {9,8,7,6,5,4,3,2,1,0};

        System.out.println(toString(arr));
    }
    /**
     * 定义方法接收任意类型数组,返回数组类型格式
     */
    public static String toString(int[] arr) {
        if(arr==null){
            return null;
        }else{
            StringBuilder sb = new StringBuilder("]");
            for (int i = 0; i < arr.length; i++) {
                //链式编程
                sb.append(arr[i]).append(i== arr.length-1?"":",");
            }
            sb.append("[");//拼接
            sb.reverse();//反转
            return sb.toString();
        }

    }

    /**
     * 运行结果
     * D:\JavaJdk\bin\java.exe
     * [0,1,2,3,4,5,6,7,8,9]
     *
     * 进程已结束,退出代码为 0
     */
}

4.常用API(Math System BigDecimal)

//Math类
	1.包含执行基本数字运算的方法,Math类没有提供公开的构造器。
	2.类的成员都是静态的,直接通过类名就可以直接调用
Math 类的常用方法
         方法名                                                 说明
public static int abs(int a)                               获取参数绝对值
public static double ceil(double a)                           向上取整
public static double floor(double a)                          向下取整
public static int round(float a)                              四舍五入
public static int max(int a,int b)                     获取两个int值中的较大值
public static double pow(double a, double b)              返回a的b次幂的值
public static double random()                     返回值为double的随机值,范围[0.0,1.0)

//System类
	1.System的功能是通用的,都是直接用类名调用即可,所以System不能被实例化。
System类的常用方法
        方法名                                                                   说明
public static void exit(int status)                              终止当前运行的Java虚拟机,非零表示异常终止
public static long currentTimeMillis()                                 返回当前系统的时间毫秒值形式
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)      数组拷贝
                                                                    
//BigDecimal类
     1.用于解决浮点型运算精度失真问题
                                                                    
//使用步骤
创建对象BigDecimal封装浮点型数据(最好的方式是调用方法)
public static BigDecimal valueOf(double val):包装浮点数成为BigDecimal对象。
BigDecima常用API
//     方法名                                                          说明
public BigDecimal add(BigDecimal b)                                    加法
public BigDecimal subtract(BigDecimal b)                               减法
public BigDecimal multiply(BigDecimal b)                               乘法
public BigDecimal divide(BigDecimal b)                                 除法
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)       除法

5.常用API(Data)

//Date类概述
	1.Date类的对象在Java中代表的是当前所在系统的此刻日期时间。
Date的构造器
名称                                                    说明
public Date()                       创建一个Date对象,代表的是系统当前此刻日期时间。
        
Date的常用方法
名称                                                    说明
public long getTime()                            获取时间对象的毫秒值
        
时间毫秒值->日期对象
      构造器                                            说明
public Date(long time)                       把时间毫秒值转换成Date日期对象。
      Date方法                                          说明
public void setTime(long time)         设置日期对象的时间为当前时间毫秒值对应的时间

6.常用API(SimpleDateFormat)

//simpleDateFormat类作用
	1.可以对Date对象或时间毫秒值 格式化 成我们喜欢的时间形式。
	2.也可以把字符串的时间形式 解析 成日期对象。

//SimpleDateFormat的构造器
        
        构造器                                                   说明
public SimpleDateFormat()                        构造一个SimpleDateFormat,使用默认格式
public SimpleDateFormat(String pattern)          构造一个simpleDateFormat,使用指定的格式
        
//SimpleDateFormat的格式化方法
        
      格式化方法                                                 说明
public final String format(Date date)                将日期格式化成日期/时间字符串
public final String format(Object time)             将时间毫秒值式化成日期/时间字符串

//格式化这个日期对象      yyyy年MM月dd日   HH(时):mm(分):ss(秒) EEE(星期) a(上午下午)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

//形式必须与被解析时间的形式完全一样,否则解析报错
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date d3 = sdf3.parse(dateString);

7.常用API(Calendar)

//calendar常用方法
Calendar cal = calendar.getInstance();
//        方法名                                     说明
public int get(int field)                    取日期中的某个字段信息。
    
public void set(int field,int value)         修改日历的某个字段信息。
    
public void add(int field,int amount)      为某个字段增加/减少指定的值。
    
public final Date getTime()                    拿到此刻日期对象。
    
public long getTimeInMillis()                 拿到此刻时间毫秒值。
    
注意:calendar是可变日期对象,一旦修改后其对象本身表示的时间将产生变化。

8.常用API(Java8 开始,java.time 包提供了新的日期和时间API)

//从Java 8开始,java.time包提供了新的日期和时间API,主要涉及的类型有:

//JDK8新增日期类一

	1. LocalDate:不包含具体时间的日期。
	2. LocalTime:不含日期的时间。
	3. Loca1DateTime:包含了日期及时间。
	4. Instant:代表的是时间戳。
	5. DateTimeFormatter用于做时间的格式化和解析的Duration:用于计算两个“时间”间隔
	6. Period:用于计算两个“日期”间隔
        
/**
     新增的API严格区分了时刻、本地日期、本地时间,并且,对日期和时间进行运算更加方便。
	其次,新API的类型几乎全部是不变类型(和String的使用类似),可以放心使用不必担心被修改。
*/
//代码案例 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class TimeDemo {
    public static void main(String[] args) {
        //1.获取本地日期对象
        LocalDate nowDate = LocalDate.now();
        System.out.println("nowDate今天的日期:" + nowDate);//今天的日期
        //nowDate今天的日期:2022-04-15
        System.out.println("nowDate.getYear():" + nowDate.getYear());
        //nowDate.getYear():2022
        System.out.println("nowDate.getMonthValue():" + nowDate.getMonthValue());
        //nowDate.getMonthValue():4
        System.out.println("nowDate.getDayOfMonth():" + nowDate.getDayOfMonth());
        //nowDate.getDayOfMonth():15

        //当年的第几天
        System.out.println("nowDate.getDayOfYear():" + nowDate.getDayOfYear());
        //nowDate.getDayOfYear():105
        //星期
        System.out.println("nowDate.getDayOfWeek:" + nowDate.getDayOfWeek());
        //nowDate.getDayOfWeek:FRIDAY
        System.out.println("nowDate.getDayOfWeek().getValue():" + nowDate.getDayOfWeek().getValue());
        //nowDate.getDayOfWeek().getValue():5
        //月份
        System.out.println("nowDate.getMonth():" + nowDate.getMonth());
        //nowDate.getMonth():APRIL
        System.out.println("nowDate.getMonth().getValue():" + nowDate.getMonth().getValue());
        //nowDate.getMonth().getValue():4
        System.out.println("==================================");
        //==================================

        //2.获取本地时间对象
        LocalTime nowTime = LocalTime.now();
        System.out.println("nowTime:" + nowTime);
        //nowTime:00:12:01.999269800
        System.out.println("nowTime.getHour():" + nowTime.getHour());
        //nowTime.getHour():0
        System.out.println("nowTime.getMinute():" + nowTime.getMinute());
        //nowTime.getMinute():12
        System.out.println("nowTime.getSecond():" + nowTime.getSecond());
        //nowTime.getSecond():1
        System.out.println("nowTime.getNano():" + nowTime.getNano());//纳秒
        //nowTime.getNano():999269800

        System.out.println("==================================");
        //==================================

        //3.获取本地  时间  日期
        LocalDateTime nowDateTime = LocalDateTime.now();
        System.out.println("nowTime:" + nowDateTime);
        //nowTime:2022-04-15T00:19:55.330272800

        //日期
        System.out.println("nowTime.getYear():" + nowDateTime.getYear());
        //nowTime.getYear():2022
        System.out.println("nowTime.getMonthValue():" + nowDateTime.getMonthValue());
        //nowTime.getMonthValue():4
        System.out.println("nowTime.getDayOfMonth():" + nowDateTime.getDayOfMonth());
        //nowTime.getDayOfMonth():15

        //时间
        System.out.println("nowTime.getHour():" + nowDateTime.getHour());
        //nowTime.getHour():0
        System.out.println("nowTime.getMinute():" + nowDateTime.getMinute());
        //nowTime.getMinute():19
        System.out.println("nowTime.getSecond():" + nowDateTime.getSecond());
        //nowTime.getSecond():55
        System.out.println("nowTime.getNano():" + nowDateTime.getNano());//纳秒
        //nowTime.getNano():330272800
        System.out.println("==================================");
        //==================================
    }
}

9.常用API(Arrays)

  • Arrays是一个为数组提供的工具类,直接拿来使用即可。
//Arrays类概述
数组操作工具类,专门用于操作数组元素的。
Arrays类的常用API
方法名
public static String toString(Object[] a)                                    对数组进行排序
public static void sort(Object[] a)                                        对数组进行默认升序排序
public static <T> void sort(Object[] a, Comparator<? super T> c)          使用比较器对象自定义排序
Arrays.sort(arr, new Comparator<Integer>() {                            
         @Override
         public int compare(Integer o1, Integer o2) {
         //指定比较规则
         //return o2 - o1; //降序
         return o1 - o2; //升序
    }
});

public static int binarySearch(int[] a, int key)
二分搜索数组中的数据,存在返回索引,不存在返回-1-(应该插入的位置)

本篇为粗略版常用API,适合会的但是忘记的人看,初学者请找详细教程
本文是自己学习时做的总结,如有错误恳请指出,我会及时更改,谢谢!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

所恋皆洛尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值