10个常用Java函数及其用法详解
1. String.split()
String.split()函数用来将字符串根据指定的分隔符拆分成一个数组。比如:"Hello,World" 这个字符串使用 "," 作为分隔符调用 split(),就会得到一个包含了 "Hello" 和 "World" 两个元素的数组。
2. Arrays.sort()
Arrays.sort()函数用来对数组进行排序。根据调用时传入的参数,可以进行升序或降序排列。例如,如果需要对一个整型数组按照从小到大排序,可以使用 Arrays.sort() 函数。
3. List.toArray()
List.toArray()函数用来将 List 转换为数组。当需要将一个 List 对象转换为数组时,就可以使用 toArray() 函数。
4. Integer.parseInt()
Integer.parseInt()函数用来将一个字符串转换成整型数字。比如,将字符串 "123" 转换成整型数字 123。
5. Math.pow()
Math.pow()函数用来计算一个数的指定次幂。比如,Math.pow(2,3)可以计算 2 的 3 次方,即 8。
6. String.substring()
String.substring()函数用来提取一个字符串的一个子字符串。比如,"Hello,World".substring(2, 7) 可以提取出子字符串 "llo,W"。
7. System.currentTimeMillis()
System.currentTimeMillis()函数用来获取当前系统时间的毫秒数。可以用来计算程序的运行时间等。
8. Date.getTime()
Date.getTime()函数用来获取一个 Date 对象表示的时间距离1970年1月1日0时0分0秒的毫秒数。
9. Calendar.getInstance()
Calendar.getInstance()函数会返回一个 Calendar 对象,该对象表示当前系统时间。
10. Random.nextInt()
Random.nextInt()函数用来生成一个指定范围内的随机整数。例如,new Random().nextInt(10) 会生成一个 0 到 9 之间的随机整数。
