Java 中一些不常用的函数

tsvico Lv5

对于我来说目前比较不怎么常用的函数

字符串和数字转换

数字转字符串

1
2
3
4
5
6
7
8
9
10
public class test{
public static void main(String[] args){
int i=5;
//方法1
String str = String.valueOf(i);
//方法2
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";
//方法1
int i = Integer.paeseInt(str);
//方法2
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;
//5.4四舍五入
System.out.println(Math.round(f1));
//5.5四舍五入
System.out.println(Math.round(f2));

//得到0-1之间的随机数,小于1
System.out.println(Math.random());
//得到0-10之间的随机数,小于10
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次击杀后,获得了 超神 的称号";

//截取从第3个开始的字符串 (基0)
String subString1 = sentence.substring(3);

System.out.println(subString1);

//截取从第3个开始的字符串 (基0)
//到5-1的位置的字符串
//左闭右开
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次击杀后,获得了 超神 的称号";

//根据,进行分割,得到3个子字符串
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)); //从位置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("无论文件是否存在, 都会执行的代码");
}
}
}
  • 标题: Java 中一些不常用的函数
  • 作者: tsvico
  • 创建于 : 2018-07-26 22:49:00
  • 更新于 : 2021-03-06 20:21:07
  • 链接: https://blog.tbox.fun/2018/8defd4c4.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论