JAVA API

API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数, 目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力, 而又无需访问源码,或理解内部工作机制的细节。

此篇只举例了一些常用类。


目录

 String类

案例 

判断

获取

转换

Math类

案例 

 Scanner类

案例 

 注意


String类

SN(序号)方法描述
1char charAt(int index)
返回指定索引处的 char 值。
2int compareTo(Object o)
把这个字符串和另一个对象比较。
3int compareTo(String anotherString)
按字典顺序比较两个字符串。
4int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写。
5String concat(String str)
将指定字符串连接到此字符串的结尾。
6boolean contentEquals(StringBuffer sb)
当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。
7static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。
8static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String。
9boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
10boolean equals(Object anObject)
将此字符串与指定的对象比较。
11boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。
12byte[] getBytes()
 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
13byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
14void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
15int hashCode()
返回此字符串的哈希码。
16int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
17int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
18int indexOf(String str)
 返回指定子字符串在此字符串中第一次出现处的索引。
19int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
20String intern()
 返回字符串对象的规范化表示形式。
21int lastIndexOf(int ch)
 返回指定字符在此字符串中最后一次出现处的索引。
22int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
23int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。
24int lastIndexOf(String str, int fromIndex)
 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
25int length()
返回此字符串的长度。
26boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
27boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
28boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
29String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
30String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
31String replaceFirst(String regex, String replacement)
 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
32String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
33String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。
34boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
35boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
36CharSequence subSequence(int beginIndex, int endIndex)
 返回一个新的字符序列,它是此序列的一个子序列。
37String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
38String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
39char[] toCharArray()
将此字符串转换为一个新的字符数组。
40String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
41String toLowerCase(Locale locale)
 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
42String toString()
 返回此对象本身(它已经是一个字符串!)。
43String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
44String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
45String trim()
返回字符串的副本,忽略前导空白和尾部空白。
46static String valueOf(primitive data type x)
返回给定data type类型x参数的字符串表示形式。
47contains(CharSequence chars)
判断是否包含指定的字符系列。
48isEmpty()
判断字符串是否为空。

案例 

判断
package string;
 
public class String_methods {
    public static void main(String[] args) {
        //1 —— boolean equals(Object anObject)
            String str = "rr-R-rr";
            System.out.println("rr-R-rr字符串与原字符串的内容相等吗?" + "rr-R-rr".equals(str_0));
 
        //2 —— boolean equalsIgnoreCase(String anotherString)
            System.out.println("rr-R-rr字符串与原字符串的内容相等吗?" + "rr-R-rr".equalsIgnoreCase(str_0));
 
        //3 —— boolean startWith(String prefix)
            System.out.println("字符串是否以\"rr\"开头?" + str.startsWith("rr"));
            System.out.println("字符串是否以\"RR\"开头?" + str.startsWith("RR"));
 
        //4 —— boolean isEmpty()
            System.out.println("字符串是否为空?" + str.isEmpty());
    }
}
获取
package string;
 
public class String_methods_2 {
    public static void main(String[] args) {
        //1 —— int length()
            String str = "We are the best.";
            System.out.println("当前字符串: " + str);
            int length = str.length();
            System.out.println("字符串的长度为:" + length);
 
        //2 —— char charAt(int index)
            char ch_0 = str.charAt(0);
            char ch_1 = str.charAt(5);
            System.out.println("字符串中,索引为0的字符是:" + ch_0);
            System.out.println("字符串中,索引为5的字符是:" + ch_1);
 
        //3 —— int indexOf(String str)
            int i_0 = str_0.indexOf('e');
            System.out.println("字符\'e\'在字符串str_0中首次出现时的索引为:" + i_0);
            int i_2 = str_0.indexOf("are");
            System.out.println("字符串\"are\"在字符串str_0中首次出现时的索引为:" + i_2);
 
        //4 —— int indexOf(String str, int t)
            int i_1 = str_0.indexOf('e', 7);
            System.out.println("从字符串str索引为7的地方开始找,字符\'e\'在字符串str中首次出现时的索引为:" + i_1);
 
        //5 —— int lastIndexOf(String str)
            int i_3 = str_0.lastIndexOf('t');
            System.out.println("字符\'t\'在字符串str中最后一次出现时的索引为:" + i_3);
 
        //6 —— int compareTo(String anotherString)
                //第一种情况 : 俩字符串相同
            int i_4 = str.compareTo("We are the best.");
            System.out.println("compareTo方法返回0说明俩字符串相等: " + i_4);
                //第二种情况 : 俩字符串长度相同,内容不同
            int i_5 = str.compareTo("You are so good.");  //W : 77, Y : 79.
            System.out.println("\'W\'和\'Y\'的ASCII码差值为:" + i_5);
                //第三种情况 : 俩字符串长度不相同,但长的字符串的前一部分与短的字符串相同。
            int i_6 = str.compareTo("We are the best.we are the king!");
            int i_7 = "We are the best.we are the king!".compareTo(str);
            System.out.println("原字符串与传入字符串的长度差值为:" + i_6);
            System.out.println("传入字符串与原字符串的长度差值为:" + i_7);
 
        //7 —— String substring(int beginIndex)
            String str_1 = str.substring(11); //"best."
            System.out.println("字符串中,从索引11开始到结束的字符串为:" + str_1);
 
        //8 —— String substring(int beginIndex, int endIndex)
            String str_2 = str.substring(7, 10);  //[7,10)——注意区间的格式。
            System.out.println("字符串中,从索引7到10(不包括10)的字符串为:" + str_2);
    }
}
转换
package string;
 
public class String_methods_3 {
    public static void main(String[] args) {
        //1 —— byte[] getBytes()
            String str = "ABCD";  //A~D对应ASCII码值 = 65~68
            byte[] bytes = str_0.getBytes();
            for (int i = 0; i < bytes.length; i++) {
                System.out.println("字节数组的第" + (i + 1) + "个元素 = " + bytes[i]);
            }
 
        //2 —— char[] toCharArray()
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                System.out.println("字符数组的第" + (i + 1) + "个元素 = " + chars[i]);
            }
 
        //3 —— static String valueOf(...)
            String str_1 = String.valueOf(1111);
            String str_2 = String.valueOf('h');
            String str_3 = String.valueOf(123.6666);
            System.out.println("int类型转String类型:" + str_1);
            System.out.println("char类型转String类型:" + str_2);
            System.out.println("double类型转String类型:" + str_3);
 
        //4 —— String replace(String old, String new)
            String str_4 = "You_are_so_beautiful!";
            String str_5 = str_4.replace('_', ' ');    //以' '替换掉'_'
            System.out.println("原字符串:" + str_4);
            System.out.println("新字符串:" + str_5);
 
        //5 —— String[] split(String regex)
            String str_6 = "What hahaa hahacoincidence haha!";
            String[] strings = str_6.split("haha");     //以"haha"来切割字符串。
            for (String string : strings) {
                System.out.print(string);
            }
 
        //6 —— String trim()
            String str_7 = "      hey,girl!    ";
            String str_8 = str_7.trim();
            System.out.println("去掉两端空白字符前的字符串:" + str_7);
            System.out.println("去掉两端空白字符后的字符串:" + str_8);
 
        //7 —— String concat(String str)
            String str_9 = "I ";
            String str_10 = str_9.concat("love ").concat("programming!");
            System.out.println("拼接后的字符串str_10 = " + str_10);
 
        //8 —— String toUpperCase()
            String str_11 = "i love you!";
            System.out.println("大写之前的字符串:" + str_11);
            System.out.println("大写之后的字符串:" + str_11.toUpperCase());
 
        //9 —— String toLowerCase()
            System.out.println("小写之前的字符串:" + str_11.toUpperCase());
            System.out.println("小写之后的字符串:" + str_11.toUpperCase().toLowerCase());
 
        //10 —— static String format(String format, Object... args)
            String name = "R";
            int age = 20;
            String hobby = "sleeping";
            String sss = String.format("My name is %s, and I'm %d years old, my hobby is %s", name, age, hobby);
            System.out.println(sss);
    }
}

Math类

序号方法与描述
1abs()
返回参数的绝对值。
2ceil()
返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型。
3floor()
返回小于等于(<=)给定参数的最大整数 。
4rint()
返回与参数最接近的整数。返回类型为double。
5round()
它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
6min()
返回两个参数中的最小值。
7max()
返回两个参数中的最大值。
8exp()
返回自然数底数e的参数次方。
9log()
返回参数的自然数底数的对数值。
10pow()
返回第一个参数的第二个参数次方。
11sqrt()
求参数的算术平方根。
12sin()
求指定double类型参数的正弦值。
13cos()
求指定double类型参数的余弦值。
14tan()
求指定double类型参数的正切值。
15asin()
求指定double类型参数的反正弦值。
16acos()
求指定double类型参数的反余弦值。
17atan()
求指定double类型参数的反正切值。
18atan2()
将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。
19toDegrees()
将参数转化为角度。
20toRadians()
将角度转换为弧度。
21random()
返回一个随机数。

案例 

 
package mmath;
 
public class Math_ {
    public static void main(String[] args) {
        //1 —— static ... abs(...)
        System.out.println("-233的绝对值 = " + Math.abs(-233));
        System.out.println("6.666的绝对值 = " + Math.abs(6.666));
        System.out.println("6.666的绝对值 = " + Math.abs(-11.11));
        System.out.println("6.666的绝对值 = " + Math.abs(5));
 
        //2 —— static double pow(double a, double b)
        System.out.println("3 的 2次方 = " + Math.pow(3, 2));
        System.out.println("2 的 3次方 = " + Math.pow(2, 3));
        System.out.println("4 的 2次方 = " + Math.pow(4, 2));
        System.out.println("2 的 10次方 = " + Math.pow(2, 10));
 
        //3 —— static double ceil(double a)
        System.out.println("2.33向上取整 = " + Math.ceil(2.33));
        System.out.println("2.99向上取整 = " + Math.ceil(2.99));
        System.out.println("3.01向上取整 = " + Math.ceil(3.01));
        System.out.println("-3.01向上取整 = " + Math.ceil(-3.01));
 
        //4 —— static double floor(double a)
        System.out.println("2.33向下取整 = " + Math.floor(2.33));
        System.out.println("2.99向下取整 = " + Math.floor(2.99));
        System.out.println("4.01向下取整 = " + Math.floor(4.01));
        System.out.println("-4.01向下取整 = " + Math.floor(-4.01));
 
        //5 —— static ... round(...)
        System.out.println("2.499四舍五入 = " + Math.round(2.499));
        System.out.println("2.501四舍五入 = " + Math.round(2.501));
        System.out.println("-3.33四舍五入 = " + Math.round(-3.33));
        System.out.println("-6.88四舍五入 = " + Math.round(-6.88));
 
        //6 —— static double sqrt(double a)
        System.out.println("9开根号 = " + Math.sqrt(9));
        System.out.println("9.0开根号 = " + Math.sqrt(9.0));
        System.out.println("1.21开根号 = " + Math.sqrt(1.21));
        System.out.println("256开根号 = " + Math.sqrt(256));
 
        //7 —— static double random()
        System.out.println("返回[0.0, 1.0)区间内的一个随机数:" + Math.random());
        System.out.println("返回[0.0, 1.0)区间内的一个随机数:" + Math.random());
        System.out.println("返回一个2~11间的随机整数 = " + (int) (2 + Math.random() * (11 - 2 + 1)));
        System.out.println("返回一个2~11间的随机整数 = " + (int) (2 + Math.random() * (11 - 2 + 1)));
 
        //8 —— static ... max(..., ...) PS:支持double,float,int,long类型
        System.out.println("3.2 和 2.3中的最大值 = " + Math.max(3.2, 2.3));
        System.out.println("-2.01 和 -1.99中的最大值 = " + Math.max(-2.01, -1.99));
        System.out.println("2333 和 3333中的最大值 = " + Math.max(2333, 3333));
        System.out.println("-666 和 11中的最大值 = " + Math.max(-666, 11));
 
        //9 —— static ... min(..., ...) PS:支持double,float,int,long类型
        System.out.println("3.2 和 2.3中的最小值 = " + Math.min(3.2, 2.3));
        System.out.println("-2.01 和 -1.99中的最小值 = " + Math.min(-2.01, -1.99));
        System.out.println("2333 和 3333中的最小值 = " + Math.min(2333, 3333));
        System.out.println("-666 和 11中的最小值 = " + Math.min(-666, 11));
    }
}

 Scanner类

序号方法与描述
1String nextLine()
接收输入的一行内容(以回车作为分隔符)
2String next()
接收输入的一个单词(以空格作为分隔符)
3int nextInt()
接收输入的一个整数
4double nextDouble()
接收输入的一个浮点数
5boolean hasNext()
检测是否还有单词输入
6boolean hasNextInt()
检测输入中是否整数
7boolean hasNextDouble()
检测输入中是否浮点数

案例 

package sscanner;
import java.util.Scanner; //导入Scanner的包
public class Scanner_ {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);//构建一个Scanner对象
        System.out.print("输入数据:");
        String s1 = sc.next(); // 接收一个单词,空格分隔
        System.out.println("接收的单词s1:"+s1);

        String s2 = sc.nextLine(); // 接收一行字符串
        System.out.println("接收的一行字符串s2:"+s2);

        System.out.print("输入整数:");
        if (sc.hasNextInt()) {// 判断输入的是否是整数
            int i = sc.nextInt(); // 接收整数
            System.out.println("整数数据i:" + i);
        }
        else {// 如果输入的不是整数
            System.out.println("输入的不是整数!");
        }

        System.out.print("输入小数:");
        if (sc.hasNextDouble()) {// 判断输入的是否是小数
            Double f = sc.nextDouble(); // 接收小数
            System.out.println("小数数据f:" + f);
        }
        else {
            System.out.println("输入的不是小数!");
        }
        sc.close(); //关闭Scanner对象
    }
}

 注意

1. next()不接收带空格的字符串,输入字符串前面的空格next()会去掉,后面的空格作为分隔符或者结束符。

2. 当输入多个数据时,第一个next()接收到数据,下一个next()发现字节流里面有东西,就不会阻塞等待输入了,会直接接收。

如上例中,输入“This is Java code” ,s1用next()只接受一个单词“This”,s2用nextLine()接收了剩余的部分。

3. 要输入 int 或 float /double类型的数据,最好先使用 hasNextXxx() 方法进行验证

4. Scanner对象用完最后用close()关闭掉,不然会告警。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值