对于我来说目前比较不怎么常用的函数
字符串和数字转换
数字转字符串
1 2 3 4 5 6 7 8 9 10
| public class test{ public static void main(String[] args){ int i=5; String str = String.valueOf(i); Integer it = i; String str2 = it.toString(); } }
|
字符串转数字
1 2 3 4 5 6 7 8 9 10
| public class test{ public static void main(String[] args){ String str = "123"; int i = Integer.paeseInt(str); int i2 = Integer.valueOf(str); System.out.println(i); } }
|
浮点型(不能有字母)
1 2 3 4 5 6 7 8 9 10
| public static void main(String[] args){ double d = 3.14; String s = String.valueOf(d); String str = "3.14"; double d1 = Double.parseDouble(str); String str2 = "3.1a4"; double d2 = Double.parseDouble(str2); }
|
数学
四舍五入,随机数,开方,次方,π,自然常数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class test{ public static void main(String[] args){ float f1 = 5.4f; float f2 = 5.5f; System.out.println(Math.round(f1)); System.out.println(Math.round(f2)); System.out.println(Math.random()); System.out.println((int)(Math.random()*10)); System.out.println(Math.PI); System.out.println(Math.E); } }
|
求一千万以内的质数个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public static int Prime(int n) { int i, j, k, count = 0; for (i = 2; i < n; i++) { k = (int) Math.sqrt(i); for (j = 2; j <= k; j++) { if (0 == i % j) break; } if (j > k) { System.out.println(i); count++; } } return count; } public static void main(String[] args) { System.out.println("一千万的素数的个数为" + Prime(10000 * 1000)); }
|
字符串控制
charAt (int index) 获取指定位置的字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号"; char c = sentence.charAt(0); System.out.println(c); } }
|
toCharArray () 获取对应的字符数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号"; char[] cs = sentence.toCharArray(); System.out.println(sentence.length() == cs.length); } }
|
subString 截取子字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号"; String subString1 = sentence.substring(3); System.out.println(subString1); String subString2 = sentence.substring(3,5); System.out.println(subString2); } }
|
split 根据分隔符进行分隔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号"; String subSentences[] = sentence.split(","); for (String sub : subSentences) { System.out.println(sub); } } }
|
trim 去掉首尾空格
1 2 3 4 5 6 7 8 9 10 11 12 13
| package character; public class TestString { public static void main(String[] args) { String sentence = " 盖伦,在进行了连续8次击杀后,获得了 超神 的称号 "; System.out.println(sentence); System.out.println(sentence.trim()); } }
|
toLowerCase 全部变成小写
toUpperCase 全部变成大写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package character; public class TestString { public static void main(String[] args) { String sentence = "Garen"; System.out.println(sentence.toLowerCase()); System.out.println(sentence.toUpperCase()); } }
|
定位
indexOf 判断字符或者子字符串出现的位置
contains 是否包含子字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号"; System.out.println(sentence.indexOf('8')); System.out.println(sentence.indexOf("超神")); System.out.println(sentence.lastIndexOf("了")); System.out.println(sentence.indexOf(',',5)); System.out.println(sentence.contains("击杀")); } }
|
异常
finally
finally里一般拿来做一些善后清理工作,try块里出现错误的话,会立即跳出try块,找到匹配的错误,执行catch块里的语句此时,可能在try块里打开的文件没关闭,连接的网络没断开,对这些浪费的内存就不能及时释放回收。如果有finally块的话,不管有没有出错,都会执行finally块里的内容。
无论是否出现异常,finally 中的代码都会被执行代码比较复制代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestException { public static void main(String[] args) { File f= new File("d:/LOL.exe"); try{ System.out.println("试图打开 d:/LOL.exe"); new FileInputStream(f); System.out.println("成功打开"); } catch(FileNotFoundException e){ System.out.println("d:/LOL.exe不存在"); e.printStackTrace(); } finally{ System.out.println("无论文件是否存在, 都会执行的代码"); } } }
|