文章目录
001.Math类
java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。
import java.lang.Math;
double abs(); // 取绝对值
double ceil(); //向上取整
double floor(); //向下取整
long round(double num); //四舍五入
002.数组工具类Arrays
java.utils.Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。
import java.util.Arrays;
static String toString(数组); //将参数数组变成字符串(按照默认格式:[1, 2, 3])
static void sort(数组); // 按照默认升序对数组的元素进行排序
003. ArrayList
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
public boolean add(E e); //将指定的元素添加到此集合的尾部。
public E remove(int index); //移除此集合中指定位置上的元素。返回被删除的元素。
public E get(int index); //返回此集合中指定位置上的元素。返回获取的元素。
public int size(); //返回此集合中的元素数。
004.Calendar
import java.tuil.Calendar;
/*
public int get(int field): //返回给定日历字段的值
参数:传递指定的日历字段(YEAR, MONTH)
返回值:日历字段代表的具体的值
int Year = 1;年
int MONTH = 2;月
int DATA = 5; 月中的某一天
int DAY_OF_MONTH = 5; 月中的某一天
int HOUR = 10; 时
int MINUTE = 12; 分
int SECOND = 13; 秒
注意:西方的月份0-11, 东方的月份1-12。
*/
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR)
int month = c.get(Calendar.MONTH)
/*
public int set(int field, int value): //将给定的日历字段设置为给定值
int Year = 1;年
int MONTH = 2;月
int DATA = 5; 月中的某一天
*/
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2099)
005.Scanner类
一个可以解析基本类型和字符串的简单文本扫描器。
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); // 接收一个键盘录入的整数
006.Random类
import java.util.Random;
Random r = new Random(); // 创建一个随机数生成器
public int nextInt(int n); //返回一个伪随机数,范围在0和n (不包括)之间的 int 值。
007.String类
构造方法:
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 97, 98, 99 };
String str3 = new String(bytes);
常用方法:
public boolean equals (Object anObject); //将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase (String anotherString); //将此字符串与指定对象进行比较,忽略大小 写。
public int length ();//返回此字符串的长度。
public String concat (String str);//将指定的字符串连接到该字符串的末尾。
public char charAt (int index); //返回指定索引处的 char值。
public int indexOf (String str); //返回指定子字符串第一次出现在该字符串内的索引。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。
public String substring (int beginIndex, int endIndex); //返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。
转换方法:
public char[] toCharArray (); //将此字符串转换为新的字符数组。
public byte[] getBytes (); //使用平台的默认字符集将该 String编码转换为新的字节数组。
public String replace (CharSequence target, CharSequence replacement); //将与target匹配的字符串使 用replacement字符串替换。
分割:
public String[] split(String regex); //将此字符串按照给定的regex(规则)拆分为字符串数组
008.System类
java.lang.System
类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有:
public static long currentTimeMillis()
:返回以毫秒为单位的当前时间。public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
:将数组中指定的数据拷贝到另一个数组中。
009.StringBuilder类
StringBuilder又称为可变字符序列,是一个字符串的缓冲区,即它是一个容器,容器中可以装很多字符串。并且能够对其中的字符串进行各种操作。
构造方法:
public StringBuilder(); //构造一个空的StringBuilder容器。
public StringBuilder(String str); //构造一个StringBuilder容器,并将字符串添加进去。
常用方法:
public StringBuilder append(); //添加任意类型数据的字符串形式,并返回当前对象自身。
public String toString(); //将当前StringBuilder对象转换为String对象。