String类
java.lang.String :此类不需要导入。
构造方法
public String()
:初始化新创建的 String对象,以使其表示空字符序列。public String(char[] value)
:通过当前参数中的字符数组来构造新的String。public String(byte[] bytes)
:通过使用平台的默认字符集解码当前参数中的字节数组来构造新的
//无参构造
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(规则)拆分为字符串数组。
public class String_Demo03 { public static void main(String[] args) { //创建字符串对象 String s = "aa|bb|cc"; String[] strArray = s.split("|"); // ["aa","bb","cc"] for(int x = 0; x < strArray.length; x++) { System.out.println(strArray[x]); // aa bb cc
}
}
}
Math类
java.lang.Math
类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。
基本运算的方法
public static double abs(double a)
:返回 double 值的绝对值。public static double ceil(double a)
:返回大于等于参数的小的整数。public static double floor(double a)
:返回小于等于参数大的整数。public static long round(double a)
:返回接近参数的 long。(相当于四舍五入方法)
Arrays类
java.util.Arrays
此类包含用来操作数组的各种方法,比如排序和搜索等。其所有方法均为静态方法,调用起来 非常简单。
操作数组的方法
public static String toString(int[] a)
:返回指定数组内容的字符串表示形式。public static void sort(int[] a)
:对指定的 int 型数组按数字升序进行排序。
Random类
java.util.Random
:该类需要 import导入使后使用。
此类的实例用于生成伪随机数。
Ranndom r=new Ranndom();
int i=r.nextInt();
查看构造方法 : public Random()
:创建一个新的随机数生成器。
查看成员方法: public int nextInt(int n)
:返回一个伪随机数,范围在 0 (包括)和 指定值 n (不包括)之间的 int 值