java基础7(常用类—API)
API:(Application Programming Interface)应用程序编程接口
-
是对java预先定义的类或接口功能和函数功能的说明文档,目的是提供给开发人员进行使用帮助说明.
Object类
-
介绍
是java所有类的根基类
所有类都直接或间接继承该类
-
toString方法
返回值是String类型;
可以根据需要在用户自定义类型中重写toString()方法;
重写toString方法示例:
public class Car { private String name; private float price; public Car(String name, float price) { this.name = name; this.price = price; } //重写toString方法 @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", price=" + price + '}'; } }
public class ObjectDemo1 { public static void main(String[] args) { Car car = new Car("宝马",300000); System.out.println(car); } }
运行结果
-
equals方法
-
public boolean equals(Object obj):指示其他某个对象是否与此对象“相等”。
-
==:如果来连接的是两个基本数据类型,比较值是否相同
-
如果来连接的是两个引用数据类型,比较地址址是否相同
原码:
public boolean equals(Object obj) { return (this == obj); }
代码示例:
public class Car { private String name; private float price; public Car(String name, float price) { this.name = name; this.price = price; } //重写toString方法 @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", price=" + price + '}'; } }
public class ObjectDemo1 { public static void main(String[] args) { // Car car = new Car("宝马", 300000); // System.out.println(car); Car c1=new Car("1",1); Car c2= new Car("1",1); System.out.println(c1.equals(c2)); String c = "汽车"; String d = "汽车"; System.out.println(c.equals(d)); } }
运行结果
-
Arrays类
-
介绍
java.util.Arrays类
用于操作数组工具类,里面定义了常见操作数组的静态方法。
-
equals方法
● 比较两个非同一数组是否相等,而数组本身的equals判断另一个数组是否它本身。
● 声明:public static boolean equals(type[ ]a,type[ ]a2)
● 参数的类型可以是原生数据类型和引用类型的任意一种类型。
● 返回:如果两个相等,则返回true,否则返回false
-
sort排序
● 作用于数组的所有元素
● public static void sort(type[] a)
● 作用于数组指定范围内的元素
public static void sort(type[] a, int fromIndex(包括), int toIndex(不包括)) 将指定的类型(除boolean以外的任意原生数据类型)数组所有元素(或指定范 围内的元素)按数字升序进行排序。
object型数组,根据元素的自然顺序,对指定对象数组进行升序排序。
(fromIndex==toIndex,则排序范围为空)
• 自定义对象排序
自定义类实现Comparable接口
重写compareTo方法
-
binarySearch — 使用二分搜索算法搜索指定数组
● 声明:
-
●public static int binarySearch(type[] a, type key)
-
●public static int binarySearch(long[] a,int fromIndex,int toIndex,long key)
● 描述:
-
●使用二分搜索算法搜索指定的type型数组,以获得指定的值。
● 参数:
-
●a - 要搜索的数组。
-
●key - 要搜索的值。
-
●fromIndex - 要排序的第一个元素的索引(包括)。
-
●toIndex - 要排序的最后一个元素的索引(不包括)。
-
●type -byte、double、float、object、long、int、short、char
● 如果key在数组中,则返回搜索值的索引;否则返回-1或者”-“(插入点)
-
-
toString方法
● 声明:public static String toString(type[ ] a)
● 描述:返回指定数组内容的字符串表示形式。
● 基本数组,字符串表示形式由数组的元素列表组成,括在[ ],相邻元素用“ , ”(逗号加空格)分隔。
String类
-
介绍
字符串是由多个字符组成的一串数据(字符序列)的字符串常量,java中所有字符串都是String类的实例
-
创建形式
● 第一种:
String s = "abc";
先在栈中创建一个对String类的对象引用变量s,然后去字符串常量池中查找有没有"abc", 如果没有则在常量池中添加”abc”, s引用变量指向常量池中的”abc”,如果常量池中有,则直接指向改地址即可,不用重新创建.
● 第二种:
一概在堆中创建新对象,值存储在堆内存的对象中。
String s = new String("abc");
-
构造方法
public String()
public String(String str)
public String(byte[ ] bytes)
public String(char[ ] value)
-
判断功能
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean isEmpty()
boolean startsWith(String prefix)
boolean endsWith(String suffix)
-
获取功能
-
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): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
-
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。含头不含尾
-
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串
public class StringDemo1 { public static void main(String[] args) { String s1="abcdeabcaa"; String s2=new String("fghijk"); System.out.println(s1.length());//10 System.out.println(s1.charAt(2));//c System.out.println(s1.charAt(1));//b System.out.println(s1.indexOf("b"));//1 System.out.println(s1.indexOf("a",4));//5 System.out.println(s1.substring(7));//caa System.out.println(s1.substring(2,5));//cde } }
-
-
转换功能
-
public byte[] getBytes(): 把字符串转换为字节数组。
-
public char[] toCharArray(): 把字符串转换为字符数组。
-
public static String valueOf(char[] chs): 把字符数组转成字符串。
-
public static String valueOf(int i): 把int类型的数据转成字符串。
-
public String toLowerCase(): 把字符串转成小写。
-
public String toUpperCase(): 把字符串转成大写。
-
public String concat(String str): 把字符串拼接。
-
String [] split(分隔符)
import java.util.Arrays; public class StringDemo2 { public static void main(String[] args) { String s1="abcde"; String s2="ABCDE"; System.out.println(s2.toLowerCase()); System.out.println(s1.toUpperCase()); System.out.println("----------"); String s3=String.valueOf(true); String s4=String.valueOf(12345); System.out.println(s3); System.out.println(s4); System.out.println("----------"); String s5="把"+"字符串"+"拼接好"; String s6="把".concat("字符串").concat("拼接好"); System.out.println(s5); System.out.println(s6); System.out.println("----------"); String s7 ="a,b,c,d,e"; String[] s8= s7.split(","); System.out.println(Arrays.toString(s8)); } }
-
-
-
替换功能
-
public String replace(char old,char new) 将指定字符进行互换
-
public String replace(String old,String new) 将指定字符串进行互换
-
-
去除字符串俩空格
-
String trim()
-