API常用类的各种方法
String类的构造方法:
public String() //空参构造。举例如下:
String str = new String(); //字符串中没有内容
public String(String original) //初始化一个新创建的String对象,使其表示一个与参数相同的字符序列。举例如下:
String str = new String(“abcdef”); //字符串内容为”abcdef”
public String(byte[] bytes) //通过使用平台的默认字符集解码指定的byte数组,构造一个新的 String,把字节数组转换成字符串。
public String(byte[] bytes, int offset, int length) //通过使用平台的默认字符集解码指定的byte子数组,构造一个新的String,把字节数组的一部分转成字符串,offset是转换的开始索引,length是转换的个数。
举例如下:
byte[] bys = new byte[]{97,98,99,100};
String str1 = new String(bys); //数组元素作为字符串的内容
String str2 = new String(bys, 1, 3); //字符串内容为”98,99,100”
public String(char[] value) //分配一个新的String,将字符数组转换为字符串。
public String(char[] value, int offset, int count) //将字符数组的一部分转换成字符串,offset是转换的开始索引,count是转换的个数(长度)。
举例如下:
char[] chs = new char[]{‘a’, ’b’, ’c’, ’d’, ’e’};
String str3 = new String(chs); //数组元素作为字符串的内容
String str4 = new String(chs, 0, 3); //字符串内容为”abc”
String类的普通方法:
int length() //返回字符串的长度
String substring(int beginIndex) //返回一个从指定索引到字符串结束的字符串
String substring(int beginIndex, int endIndex) //返回一个从指定索引开始到指定索引结束的字符串[beginIndex, endIndex)
boolean startsWith(String str) //判断该字符串是否以给定的字符串开头
boolean endsWith(String str) //判断该字符串是否以给定的字符串结尾
boolean contains(String str) //判断该字符串中是否包含给定的字符串
int indexOf(String str) //返回指定字符串的索引,如果有多个指定的字符串出现,返回的是第一个字符串的索引,如果没有指定字符串则返回-1
int indexOf(int ch) //返回指定字符在该字符串中第一次出现的位置,不存在则返回-1
char charAt(int index) //返回指定索引位置上的字符
byte[] getBytes() //把字符串转换成字节数组
char[] toCharArray() //把字符串转换成字符数组
//除了字符数组,其他所有类型数组打印数组名,打印出来的都是地址值;只有字符数组打印的不是地址值,而是数组里的元素。
boolean equals(Object anObject) //将此字符串与指定的对象作比较
boolean equalsIgnoreCase(String anotherString) //将两个字符串进行比较,忽略大小写
String toString() //获取该字符串对象中的内容(即此对象本身) [直接打印引用数据类型时,默认调用该类型进行重写后的toString方法]
boolean isEmpty() //当且仅当length()为0时返回true
String toUpperCase() //把该字符串所有字符转换成大写
String toLowerCase() //把该字符串所有字符转换成小写
String replace(char oldChar, char newChar) //将给定的旧字符,用新字符替换
String replace(String oldStr, String newStr) //将给定的旧字符串,用新字符串替换
String trim() //返回去除字符串两端空白后的字符串
StringBuffer类的构造方法:
public StringBuffer() //空参构造
StringBuffer类的最常用的两个方法:
StringBuffer append(任意类型 变量名) //向缓冲区追加数据
注意:(类型为StringBuffer的)返回值是当前对象自己,所以,StringBuffer可以改变字符串的长度和内容。
String toString() //将缓冲区内所有的内容转换成字符串类型
StringBuffer类的其他方法:
StringBuffer delete(int start, int end) //移除起始索引到结束索引位置的字符
StringBuffer deleteCharAt(int index) //删除指定索引位置的字符
StringBuffer insert(int offset, String str) //从索引offset的前面插入字符串
StringBuffer replace(int start, int end, String str) //使用给定字符串替换起始索引到结束索引之间的字符串
StringBuffer reverse() //返回此字符串的倒序字符串
注意:返回值(类型为StringBuffer)是当前对象自己,所以,StringBuffer可以改变字符串的长度和内容。
String substring(int start) //截取从指定位置到末尾结束的字符串缓冲区,返回新字符串
String substring(int start, int end) //截取从起始位置到结束位置的字符串缓冲区,返回新字符串
无论多少数据,无论数据是什么类型都不重要,只要最终变成字符串就可以使用StringBuffer这个容器。
StringBuffer类有的方法StringBuilder类都有。StringBuilder字符串缓冲区速度较快,但线程不安全。
Date类(java.util.Date):
构造方法:
public Date() //()里是当前日期
public Date(long time) //指定毫秒的日期
普通方法:
long getTime() //获取毫秒值
void setTime(毫秒值) //指定时间
DateFormat(java.text.DateFormat)类:
DateFormat是抽象类,不能创建对象,可以创建其子类SimpleDateFormat(java.text.SimpleDateFormat)的对象。
构造方法:
public SimpleDateFormat() //默认格式转换
public SimpleDateFormat(String pattern) //给定格式转换
普通方法:
public final String format(Date date) //将日期转换成字符串
public Date parse(String source) throws ParseException //指定字符串转换为日期(转换时,该String要符合指定格式,否则不能转换)
Calendar(抽象)类(java.util.Calendar):
public static Calendar getInstance() //返回当前日历
public int get(int field) //获取给定日历字段的值
public abstract void add(int field,int amount) //为指定字段添加指定时间,field为指定字段,amount为添加的时间
public final void set(int field, int value) //为指定字段重新设置指定的时间
public final void set(int year, int month, int date) //直接设置年月日
Public final Date getTime() //获取该日历对象转换成的日期对象
三个获取毫秒值的方法:
通过Date类的getTime方法获取:
long time = new Date().getTime();
通过Calendar类的 getTimeInMillis方法获取:
long time = Calendar.getInstance().getTimeInMillis();
通过System类currentTimeMillis方法获取:
long time = System.currentTimeMillis();
将字符串转换成基本类型:
static byte parseByte(String s) //将字符串参数解析成有符号的十进制byte
static int parseInt(String s) //将字符串参数解析成有符号的十进制整数
static double parseDouble(String s) //返回一个新的double值,该值被初始化为用指定String表示的值
static boolean parseBoolean(String s) //将字符串参数解析成boolean值
【还有short、long、float类型类似于上述操作】
parseXXX(String s); 其中XXX表示基本类型,参数为可以转成基本类型的字符串,如果字符串无法转成基本类型,将会发生数字转换问题NumberFormatException
将基本数值转成字符串有3种方式:
(1)基本类型直接与“”相连接即可,eg: 34 + ””;
(2)调用String的valueOf方法,eg: String.valueOf(34);
valueOf的参数类型可以是boolean、char、double、float、int、long、Object等类型。
(3)调用包装类中的toString方法,eg: Integer.toString(34);
String toString(); //返回表示此XXX数据类型值的String对象
String toString(byte b); //返回指定byte的一个新String对象
括号内的参数类型也可以是short、int、long、float、double、char、boolean
基本类型和对象转换:
使用int类型与Integer对象进行演示,其他基本类型与此相同
基本类型------->包装对象
Integer(int value) //构造一个新分配的Integer对象,它表示指定的int值 eg: Integer i = new Integer(4);
Integer(String s) //构造一个新分配的Integer对象,它表示String参数所指示的int值 eg: Integer ii = new Integer(“4”);
static Integer valueOf(int i) //普通方法,返回一个表示指定的int值的Integer实例 eg: Integer i = Integer.valueOf(4);
static Integer valueOf(String s) //普通方法,返回保存指定的String的值的Integer对象 eg: Integer ii = Integer.valueOf(“4”);
包装对象------->基本类型
static int intValue() //普通方法,以int类型返回该Integer的值
自动装箱:基本数值转换成对象。Integer i = 4;
自动拆箱:对象转换成基本数值;Integer i = 4; ---> i = i + 5;
System类:
static long currentTimeMillis() //返回以毫秒为单位的当前时间
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) //复制数组,src源数组,srcPos开始索引,dest目的数组,destPos目的数组的开始索引,length要复制的数组元素个数
static void exit(int status) //终止当前正在运行的Java虚拟机
status通常传入0记为正常退出状态,其他为异常状态。
static void gc() //垃圾回收器,暗示垃圾回收器进行垃圾回收
static String getProperty(String key)获取指定键指示的系统属性
Math类:
static double abs(double a) //返回double值的绝对值
static double ceil(double a) //向上取整,取得值大于a
static double floor(double a) //向下取整,取得值小于a
static double max(double a, double b) //返回最大值
static double min(double a, double b) //返回最小值
static double pow(double a, double b) //求a的b次方
static double random() //返回在[0,1)范围内的值
static long round(double a) //返回最接近参数a的long值(四舍五入)
static int round(float a) //返回最接近参数a的int值(四舍五入)
Arrays类:
static int binarySearch(int[] a, int key) //二分查找法查找key值的索引
static void sort(int[] a) //对指定的int型数组按数字升序进行排序
static String toString(int[] a) //返回指定数组内容的字符串表示形式