欢迎访问宙启技术站
智能推送

Java函数库:必须知道的15个函数

发布时间:2023-05-29 03:40:21

Java是一种流行的编程语言,它提供了许多强大的函数库,可以帮助开发人员轻松地解决许多问题。在本文中,我们将介绍15个必须知道的Java函数,这些函数非常实用,可以帮助您提高Java编程技能。

1. String类的substring()函数

substring()函数是Java String类中的一个非常常用的函数,它用于从字符串中截取一个子串。该函数采用两个参数:起始和结束索引。其中,起始索引是要截取子串的 个字符的位置,结束索引是子串的最后一个字符的位置加一。下面是一个例子:

String str = "Hello World";

String substr = str.substring(6, 11);

System.out.println(substr); //输出 World

2. String类的charAt()函数

charAt()函数是另一个与Java String相关的常用函数,它返回指定索引处的字符。例如:

String str = "Hello World";

char ch = str.charAt(6);

System.out.println(ch); //输出 W

3. Math类的min()和max()函数

Math类提供了许多实用的数学函数,其中min()和max()函数用于获取一组数字中的最小值和最大值。例如:

int x = 10;

int y = 20;

int z = 30;

int min = Math.min(Math.min(x, y), z);

int max = Math.max(Math.max(x, y), z);

System.out.println("Min: " + min);

System.out.println("Max: " + max);

4. Math类的random()函数

random()函数用于生成一个随机数,它返回一个double类型的值,该值在0.0到1.0之间。例如:

double randomNum = Math.random();

System.out.println(randomNum);

5. Arrays类的sort()函数

sort()函数用于对数组进行排序。该函数采用一个数组作为参数,并使用Java的默认排序算法对其进行排序。例如:

int[] arr = {10, 5, 8, 3, 2};

Arrays.sort(arr);

System.out.println(Arrays.toString(arr)); //输出 [2,3,5,8,10]

6. System类的currentTimeMillis()函数

currentTimeMillis()函数用于获取当前时间的毫秒数。例如:

long timeInMilliseconds = System.currentTimeMillis();

System.out.println(timeInMilliseconds);

7. Scanner类的nextInt()函数

Scanner类是用于从标准输入流中读取数据的Java类。nextInt()函数用于读取下一个整数。例如:

Scanner scanner = new Scanner(System.in);

int num = scanner.nextInt();

System.out.println(num);

8. Scanner类的nextLine()函数

nextLine()函数用于读取下一行文本。例如:

Scanner scanner = new Scanner(System.in);

String line = scanner.nextLine();

System.out.println(line);

9. File类的exists()函数

exists()函数用于检查文件是否存在。例如:

File file = new File("C:\\Users\\test.txt");

if(file.exists()) {

    System.out.println("File exists");

} else {

    System.out.println("File does not exist");

}

10. File类的createNewFile()函数

createNewFile()函数用于创建一个新文件。例如:

File file = new File("C:\\Users\\test.txt");

if(file.createNewFile()) {

    System.out.println("File created");

} else {

    System.out.println("File failed to create");

}

11. Calendar类的getInstance()函数

getInstance()函数用于获取一个Calendar对象,该对象可用于操作日期和时间。例如:

Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);

int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(year + "-" + month + "-" + day);

12. SimpleDateFormat类的format()函数

format()函数用于将日期格式化为一个字符串。例如:

Date date = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

String formattedDate = formatter.format(date);

System.out.println(formattedDate);

13. Integer类的parseInt()函数

parseInt()函数用于将字符串转换为整数。例如:

String str = "100";

int num = Integer.parseInt(str);

System.out.println(num);

14. Double类的parseDouble()函数

parseDouble()函数用于将字符串转换为double类型。例如:

String str = "3.1415";

double num = Double.parseDouble(str);

System.out.println(num);

15. ArrayList类的add()函数

add()函数用于向ArrayList对象中添加一个元素。例如:

ArrayList<String> list = new ArrayList<String>();

list.add("Hello");

list.add("World");

System.out.println(list); //输出 [Hello, World]

总结

以上就是15个Java函数库中的常用函数。这些函数覆盖了许多Java编程中经常遇到的场景,掌握它们可以让编程变得更加轻松和高效。在实际开发中,这些函数也是必不可少的。希望这篇文章对您有所帮助,让您在Java编程中少走弯路。