黑马程序员 总结 常用API类中的方法及测试

来源:互联网 发布:crs自动拉起数据库 编辑:程序博客网 时间:2024/05/14 00:28

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

常用API类中的方法及测试

1:Object(重点)
  public String toString():为了让对象的显示有意义,一般重写该方法。
  public boolean equals(Object obj):默认比较的是地址值,一般重写该方法,按照自己的需求。


2:Math
    Math中常用的全部都是静态方法,用类名(Math)调用即可。

//public static double floor(double a):小于等于参数的最大整数。System.out.println(Math.floor(1.5));//1//public static double ceil(double a):大于等于参数的最小整数。System.out.println(Math.ceil(1.5));//2//public static int round(float a):四舍五入。+0.5System.out.println(Math.round(1.4));//1System.out.println(Math.round(1.5));//2//public static double random():随机数。[0.0,1.0) 包左不包右//打印1-10的随机数System.out.println((int)(Math.random()*10+1));//public static double pow(double a,double b):a的b次方System.out.println(Math.pow(2, 10));//1024//public static double sqrt(double a):平方根素数问题。System.out.println(Math.sqrt(9));//3




3:Random
//public int nextInt(int n):随机产生[0,n)Random r = new Random();System.out.print(r.nextInt(5));//随机产生0-5之间的数,包含0,不包含5



4:Scanner

//public int nextInt():获取int类型Scanner scan = new Scanner(System.in);System.out.println(scan.nextInt());//输入的什么数字,打印的就是什么数字//public String nextLine():获取String类型scan = new Scanner(System.in);System.out.println(scan.nextLine());//输入的是什么字符串,打印的就是什么字符串




5:String(重点)
//A:判断功能//boolean equals(Object obj)//判断字符串的内容是否相同,区分大小写。String s = "HelloWorld";System.out.println(s.equals("HelloWorld"));//trueSystem.out.println(s.equals("helloworld"));//falseSystem.out.println("1-------------------");//boolean equalsIgnoreCase(String str)//判断字符串的内容是否相同,不区分大小写。System.out.println(s.equalsIgnoreCase("HelloWorld"));//trueSystem.out.println(s.equalsIgnoreCase("helloworld"));//trueSystem.out.println("2-------------------");//boolean contains(String str)//判断字符串对象是否包含给定的字符串。System.out.println(s.contains("or"));//trueSystem.out.println(s.contains("ak"));//falseSystem.out.println("3-------------------");//boolean startsWith(String str)//判断字符串对象是否以给定的字符串开始。System.out.println(s.startsWith("Hel"));//trueSystem.out.println(s.startsWith("hello"));//falseSystem.out.println("4-------------------");//boolean endsWith(String str)//判断字符串对象是否以给定的字符串结束。System.out.println(s.endsWith("World"));//trueSystem.out.println(s.endsWith("world"));//falseSystem.out.println("5-------------------");//boolean isEmpty()//判断字符串对象是否为空。数据是否为空。为空返回trueSystem.out.println(s.isEmpty());//falseString s2 = "";System.out.println(s2.isEmpty());//true//B:获取功能//int length()//获取字符串的长度String s = "helloworld";System.out.println(s.length());//10System.out.println("1--------");//char charAt(int index)//返回字符串中给定索引处的字符System.out.println(s.charAt(2));//lSystem.out.println("2--------");//int indexOf(int ch)//返回指定字符在此字符串中第一次出现处的索引。System.out.println(s.indexOf('l'));//2System.out.println("3--------");//int indexOf(String str)//返回指定子字符串在此字符串中第一次出现处的索引。System.out.println(s.indexOf("world"));//5System.out.println("4--------");//int indexOf(int ch,int fromIndex)//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。System.out.println(s.indexOf('l', 4));//8System.out.println("5--------");//int indexOf(String str,int fromIndex)//返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。String s1 =  "hellohello";System.out.println(s1.indexOf("llo",3));//7System.out.println("6--------");//String substring(int start)//截取字符串。返回从指定位置开始截取后的字符串。System.out.println(s.substring(4));//oworld//String substring(int start,int end)//截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。System.out.println(s.substring(4, 8));//owor//C:转换功能//byte[] getBytes()// 把字符串转换成字节数组。String s = "HelloWorld";byte[] bys = s.getBytes();for (int x = 0; x < bys.length; x++) {System.out.print(bys[x]+"  ");}System.out.println();System.out.println("1----------------");//72  101  108  108  111  87  111  114  108  100 //char[] toCharArray()//把字符串转换成字符数组。char[] chs = s.toCharArray();for (int x = 0; x < chs.length; x++) {System.out.print(chs[x]);}System.out.println();System.out.println("2----------------");//HelloWorld//static String copyValueOf(char[] chs)//把字符数组转换成字符串。char[] chs2 = { 'a', 'b', 'c', '中', '国' };String s2 = String.copyValueOf(chs2);System.out.println(s2);System.out.println("3----------------");//abc中国//static String valueOf(char[] chs)//把字符数组转换成字符串。String s3 = String.valueOf(chs2);System.out.println(s3);System.out.println("4----------------");//abc中国//static String valueOf(int i)// 基本类型:把int(基本类型)转换成字符串。int i = 100;String s4 = String.valueOf(i);System.out.println(s4);System.out.println("5----------------");//100//String toLowerCase()// 把字符串变成小写System.out.println(s.toLowerCase());System.out.println("6----------------");//helloworld//String toUpperCase()// 把字符串变成大写System.out.println(s.toUpperCase());System.out.println("7----------------");//HELLOWORLD//String concat(String str)//拼接字符串。String s5 = "hello";String s6 = s5 + "world";String s7 = s5.concat("world");System.out.println(s6);System.out.println(s7);//helloworld//helloworld//D:其他功能a:替换功能//String replace(char oldChar,char newChar)//用新的字符去替换指定的旧字符String s = "helloworld";System.out.println(s.replace('l', 'p'));//heppoworpd//String replace(String oldString,String newString)//用新的字符串去替换指定的旧字符串System.out.println(s.replace("ll", "ak47"));//heak47oworldb:切割功能String[] split(String regex) 根据匹配给定的正则表达式来拆分此字符串。String ages = "20-30";String[] strArray = ages.split("-");for (int x = 0; x < strArray.length; x++) {System.out.print(strArray[x]+" ");}System.out.println();//20 30c:去除两端空格功能//String trim()//返回字符串的副本,忽略前导空白和尾部空白。String name = "  admin hello      ";System.out.println("***" + name.trim() + "***");//***admin hello***d:字典顺序比较功能//int compareTo(String str)//按字典顺序比较两个字符串String s1 = "hello";String s2 = "Hello";System.out.println(s1.compareTo(s2));// 32//int compareToIgnoreCase(String str) //按字典顺序比较两个字符串,不考虑大小写。System.out.println(s1.compareToIgnoreCase(s2));//0




6:StringBuffer(重点)
//public StringBuffer append(char[] str):添加  参数重载,可以添加很多类型,这里举例一种StringBuffer sb = new StringBuffer();sb.append(100).append("hello").append(true).append(12.5);System.out.println(sb);//100hellotrue12.5//public StringBuffer insert(int index,int i):在指定位置添加  参数重载,可以添加很多类型,这里举例一种sb.insert(8, "world");System.out.println(sb);//100helloworldtrue12.5//public StringBuffer reverse():反转StringBuffer sb1 = new StringBuffer();sb1.append("hello").append("world");System.out.println(sb1);//dlrowolleh




7:System
//public static void exit(int status):终止当前正在运行的 Java 虚拟机。System.exit(100);//这里退出了,下面不会执行System.out.println("hello");




8:Arrays
//public static String toString(int[] a):把整型数组转变成字符串。int[] arr = { 23, 84, 51, 72, 69 };String str = Arrays.toString(arr);System.out.println(str);//[23, 84, 51, 72, 69]//public static void sort(int[] a):排序Arrays.sort(arr);System.out.println(Arrays.toString(arr));//[23, 51, 69, 72, 84]//public static int binarySearch(int[] a,int key):二分查找System.out.println(Arrays.binarySearch(arr, 51));//1




9:Integer(重点)
//public static int parseInt(String s):把String -- intString s = "100";int number = Integer.parseInt(s);System.out.println(number);//100



---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

0 0
原创粉丝点击