API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数, 目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力, 而又无需访问源码,或理解内部工作机制的细节。
此篇只举例了一些常用类。
目录
String类
案例
判断
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类
| 序号 | 方法与描述 |
|---|---|
| 1 | abs() 返回参数的绝对值。 |
| 2 | ceil() 返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型。 |
| 3 | floor() 返回小于等于(<=)给定参数的最大整数 。 |
| 4 | rint() 返回与参数最接近的整数。返回类型为double。 |
| 5 | round() 它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。 |
| 6 | min() 返回两个参数中的最小值。 |
| 7 | max() 返回两个参数中的最大值。 |
| 8 | exp() 返回自然数底数e的参数次方。 |
| 9 | log() 返回参数的自然数底数的对数值。 |
| 10 | pow() 返回第一个参数的第二个参数次方。 |
| 11 | sqrt() 求参数的算术平方根。 |
| 12 | sin() 求指定double类型参数的正弦值。 |
| 13 | cos() 求指定double类型参数的余弦值。 |
| 14 | tan() 求指定double类型参数的正切值。 |
| 15 | asin() 求指定double类型参数的反正弦值。 |
| 16 | acos() 求指定double类型参数的反余弦值。 |
| 17 | atan() 求指定double类型参数的反正切值。 |
| 18 | atan2() 将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。 |
| 19 | toDegrees() 将参数转化为角度。 |
| 20 | toRadians() 将角度转换为弧度。 |
| 21 | random() 返回一个随机数。 |
案例
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类
| 序号 | 方法与描述 |
|---|---|
| 1 | String nextLine() 接收输入的一行内容(以回车作为分隔符) |
| 2 | String next() 接收输入的一个单词(以空格作为分隔符) |
| 3 | int nextInt() 接收输入的一个整数 |
| 4 | double nextDouble() 接收输入的一个浮点数 |
| 5 | boolean hasNext() 检测是否还有单词输入 |
| 6 | boolean hasNextInt() 检测输入中是否整数 |
| 7 | boolean 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()关闭掉,不然会告警。

被折叠的 条评论
为什么被折叠?



