当前位置: 代码迷 >> 综合 >> JavaSE学习笔记(Day7)
  详细解决方案

JavaSE学习笔记(Day7)

热度:63   发布时间:2023-11-25 01:45:30.0

Day7

Java常用类_API

概述

API(Application Programming Interface)应用程序编程接口

  • Java程序员在开发Java程序时,只需要安装有Jdk,就可以在程序中使用import关键字导入Java API中指定的包并在自己的程序中使用这些包中定义的各种类和接口。

基本类型包装类

  • 基本数据类型不是面向过程的,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类。
  • 包装类(IntegerDouble) 封装了基本的数据类型的值,并提供了一系列的操作方法。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U9GzdRKY-1638463402848)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1638457216111.png)]

Object类

  • 是所有类的根类
  • 所有类都直接或者间接地继承该类

Object类的toString()方法

  • 返回该对象的字符串表示
  • 它的值等于 :
    • getClass().getName() + '@' + Integer.toHexString(hashCode())
  • 由于默认情况下的数据没有意义,通常重写该方法、
//直接调用toString方法public class MyTest {
    public static void main(String[] args) {
    Object obj = new Object();//toString() 方法或对象的地址值,以字符串形式返回。String s = obj.toString();System.out.println(s);//当你打印对象名的时候,其实后面调用了toString()方法,只是我们经常省略不写。System.out.println(obj);}
}
  • toString()方法的重写
    //一般我们自定义的类,重写toString()方法时,是喜欢输出成员变量的值//(此处省略了set get方法 :)@Overridepublic String toString() {
    return "Student{" +"name='" + name + '\'' +", age=" + age +'}';public class MyTest {
    public static void main(String[] args) {
    Student student = new Student();student.setName("张三");student.setAge(23);student.show();System.out.println(student.toString());}
}
输出结果为:
Student{
    name='张三', age=23}

Object类的equals()方法

  • equals()方法 用来判断某个对象是否与此对象 “相等”
  • 默认情况下比较对象的引用是否相同
  • 由于比较对象的引用没有意义,一般情况下重写该方法,用于比较成员变量的值是否相等
//equals()方法的重写@Overridepublic boolean equals(Object obj) {
      // Object obj=s2 Student//对传入的参数做非空判断if(obj==null){
    return false;}if(this==obj){
    return true;if (!(obj instanceof Student)) {
     //判断obj这个引用 是不是Student类型的引用return false;}//向下转型Student s2= (Student) obj;// 23==24 && "李四"=="李四"// String 引用类型 ==号比较两个对象比较的是地址值 我们现在的意思是想要比较这两个字符串字面上的内容是否相同//我们要比较两个字符串,字面上的内容是否相同,要调用字符串中的 equals() 方法来比较return this.age == s2.age && this.name.equals(s2.name);}
}public class MyTest {
    public static void main(String[] args) {
    Object obj = new Object();Object obj2= new Object();//== 号可以判断两个对象的地址值,是否相同System.out.println(obj==obj2);/** public boolean equals(Object obj) {return (this == obj);}* */boolean b = obj.equals(obj2);System.out.println(b);Student student = new Student("q",23);Student student1 = new Student("q",23);System.out.println(student==student1);boolean equals = student.equals(student1);System.out.println(equals);}
}
  • JDK提供的一些类,如String,Date等,重写了Object的equals方法,调用这些类的equals方法,

    x.equals (y) ,当x和y所引用的对象是同一类对象且属性内容相等时(并不一定是相同对象),返回
    true 否则返回 false

Arrays类

  • java.util.Arrays 类

  • 用于操作数组工具的类,里面定义了常见的操作数组的方法

  • equals方法

    • 用于比较两个非同一数组是否相等,数组本身的equals判断另一个数组是否是它

    本身

    • 声明方式: public static boolean equals( type[]a,type []b );
    • 参数的类型可以是原生数据类型和引用类型的任意一种类型
    • 返回:如果两个相等 , 则返回ture 否则返回false
  • sort排序方法

    • 作用于数组的所有元素

    • public static void sort(type []a)

    • 作用于数组指定范围内的元素

      public static void sort(type[] a, int fromIndex(包括), int toIndex(不包括))
      将指定的类型(除boolean以外的任意原生数据类型)数组所有元素(或指定范
      围内的元素)按数字升序进行排序。
      object型数组,根据元素的自然顺序,对指定对象数组进行升序排序。
      (fromIndex==toIndex,则排序范围为空)

    • 自定义对象的排序需实现Comparable接口

  • binarySearch 方法 (使用二分搜索算法 搜索指定数组

    • 使用二分搜索算法搜索指定的type型数组,以获得指定的值
    • 声明:

    public static int binarySearch(type[] a, type key)
    public static int binarySearch(long[] a,int fromIndex,int
    toIndex,long key

    • 参数:

      a - 要搜索的数组。
      key - 要搜索的值。
      fromIndex - 要排序的第一个元素的索引(包括)。
      toIndex - 要排序的最后一个元素的索引(不包括)。
      type -byte、double、float、object、long、int、short、char

  • toString方法

    • 返回指定数组的字符串表示形式
    • 字符串表示有数组的元素列表组成,括在[ ]中 相邻元素用“,” 分隔、

String类

  • 字符串是由多个字符组成的一串数据(字符序列)的字符串常量,java中所有字
    符串都是String类的实例。

  • String的两种创建方式:

    • String s = “abc”; //简易方法
    • 先在栈中创建一个对String类的对象引用变量s,然后去字符串常量池中查找
      有没有"abc", 如果没有则在常量池中添加”abc”, s引用变量指向常量池中
      的”abc”,如果常量池中有,则直接指向改地址即可,不用重新创建.
    • String s = new String(“abc”);
      • 一概在堆中创建新对象,值存储在堆内存的对象中
  • String类的构造方法:

    ? public String()

    ? public String(String original) //把字符串常量值转为字符串

    ? public String(byte[] bytes) //把字节数组转成字符串

    ? public String(char[] value):把字符数组转成字符串

  • String的特点: 一旦创建不能改变,因为字符串的值在堆内存池中划分了内存,分配了地址。

  • String类的功能

    • String类的获取功能

    • public int length(): 获取字符串的长度。
      public char charAt(int index): 获取指定索引位置的字符

      public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
      public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
      public int indexOf(int ch,int fromIndex): 返回指定字符在此字符串中从指定位置后第一次出现处的索引。
      public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

lastIndexOf: 返回最后一次出现的索引public class MyTest4 {
    public static void main(String[] args) {
    //获取某个字符在原字符中第一次出现的索引位置,如果没检索到,返回 -1String str = "我好困睡觉我要睡觉我好困睡觉我要睡觉哈哈哈";int a = str.indexOf("好");System.out.println(a);int b = str.indexOf("睡觉");System.out.println(b);//从指定索引处开始查找int c = str.indexOf("睡觉", 5);System.out.println(c);int d = str.indexOf("睡觉", str.indexOf("睡觉")+1);System.out.println(d);//lastIndexOf从后往前查找int lastIndexOf = str.lastIndexOf("睡觉",13);System.out.println(lastIndexOf);}
}

public String substring(int start): 从指定位置开始截取字符串,默认到末尾。含头不含尾
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串

public class MyTest5 {
    public static void main(String[] args) {
    String str = "我好困睡觉我要睡觉我好困睡觉我要睡觉哈哈哈";//根据起始索引和终止索引,从原字符串中,截取一部分字符串,返回。 含头不含尾String s = str.substring(0,5);System.out.println(s);//从给定索引截取到末尾String s1 = str.substring(10);System.out.println(s1);}
}
  • String类的转换功能

    public byte[] getBytes(): 把字符串转换为字节数组。

    
    public class Mytest {
          public static void main(String[] args) {
          //把字符串转换成字节数组String str = "asdfghjkl";byte[] bytes = str.getBytes();for (int i = 0; i < bytes.length; i++) {
          System.out.println(bytes[i]);}String s = new String(bytes);System.out.println(s);//UTF-8 一般汉字会占 3个字节 GBK 一个汉字占两个字节str="我";byte[] bytes1 = str.getBytes();for (int i = 0; i < bytes1.length; i++) {
          System.out.println(bytes1[i]);}byte[] bytes2={
          -26,-120,-111};String s1 = new String(bytes2);System.out.println(s1);}
    }
    

    public char[] toCharArray(): 把字符串转换为字符数组。

    
    public class Mytest1 {
          public static void main(String[] args) {
          //把字符串转换成字符数组String str = "我好困我要睡觉";char[] chars = str.toCharArray();for (int i = 0; i < chars.length; i++) {
          System.out.println(chars[i]);}//把字符数组转换成字符串String s = new String(chars);System.out.println(s);}
    }
    

    public static String valueOf(char[] chs): 把字符数组转成字符串。
    public static String valueOf(int i): 把int类型的数据转成字符串。
    public String toLowerCase(): 把字符串转成小写。
    public String toUpperCase(): 把字符串转成大写。
    public String concat(String str): 把字符串拼接。

    public class MyTest3 {
          public static void main(String[] args) {
          int num=100;double num2=3.14;boolean f=false;String s=num+"";//"100"String s2=num2+"";//"3.14"String s3=f+"";//"false"String s1 = String.valueOf(num);System.out.println(s1);//"100"String s4 = String.valueOf(true); //"true"System.out.println(s4);String s5 = String.valueOf(3.5);  //"3.5"System.out.println(s5);String s6 = String.valueOf(new char[]{
          'a', 'b', 'c'});String s7 = new String(new char[]{
          'a', 'b', 'c'});System.out.println(s6);System.out.println(s7);System.out.println("============");String s9="abc";String s8 = s9.toUpperCase();System.out.println(s8);String s10="EFG";String s11 = s10.toLowerCase();System.out.println(s11);//拼接字符串String str2="aaa"+"bbb"+"ccc";String sss = "AAA".concat("BBB");System.out.println(sss);String concat = "AAA".concat("BBB").concat("CCC").concat(str2);System.out.println(concat);}
    }
    

    String [] split(分隔符)

    • String的替换功能

      public String replace(char old,char new) 将指定字符进行互换
      public String replace(String old,String new) 将指定字符串进行互换

public class Mytest3 {
    public static void main(String[] args) {
    String str = "我好困我要睡觉";String s1 = str.replace("困", "饿");System.out.println(s1);String s2 = str.replace("睡觉", "吃饭");System.out.println(s2);String s3 = str.replace("饿", "累").replace("吃饭", "休息");System.out.println(s3);}
}//运行结果:
/*我好饿我要睡觉我好困我要吃饭我好困我要睡觉 / 
  • 去除字符串两空格:

    String trim()

    
    public class MyTest4 {public static void main(String[] args) {String str2="    a  b  c     ";//去除字符串左右两端的空格String trim = str2.trim();System.out.println(trim);}
    }