---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
常用的API方法是开发中经常要用到的,因此我们不但要会使用各个方法,而且要做到熟练使用,最好能做到了然于心,能记下来。
1、Object
Object类: 它是所有类的超类
需要掌握的方法:
public String toString():返回该对象的字符串表示
public boolean equals(Object obj): 指示其他某个对象是否与此对象“相等”。
(了解)protected void finalize(): 用来释放 对象垃圾
(了解)public final Class<?> getClass():返回此 Object 的运行时类。
(了解)public int hashCode():返回该对象的哈希码值 ,它是一个逻辑值。 它不是真正的内存地址
2、Scanner类
在JDK1.5时候,它是一个获取键盘输入的数据的封装类
通过使用Scanner类中的方法,可以得到各种类型的数据
需要掌握的方法:
public int nextInt() 获取int数据类型的数据
public String nextLine() 获取字符串数据
3、String类
概念:
将多个字符 串联起来,这就是字符串 "hello",其实字符串就相当于是一个字符数组
String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
字符串是常量;它们的值在创建之后不能更改。
构造函数:
public String(): 空参数构造函数
public String(byte[] bytes): 把给定的字节数组 转换成字符串
public String(byte[] bytes,int startIndex,int length): 把给定的字节数组的一部分 转换成字符串 (包含startIndex位置的元素)
public String(char[] value): 把 给定的字符数组 转换成字符串
public String(char[] value,int startIndex,int length): 把给定的字符数组的一部分 转换成字符串(包含startIndex位置的元素)
public String(String original):把字符串 转换成 字符串
判断功能:
boolean equals(Object obj): 比较两个字符串内容是否相同
boolean equalsIgnoreCase(String str): 比较两个字符串内容是否相同(忽略大小写)
boolean contains(String str): 判断该字符串是否包含给定的字符串
boolean startsWith(String str): 判断该字符串是否以 给定的字符串开头
boolean endsWith(String str): 判断该字符串是否以给定的字符串结尾
boolean isEmpty(): 判断该字符串是否为空 ""
获取功能:
int length(): 获取字符串的长度
char charAt(int index): //把该字符串中给定位置的字符返回
int indexOf(int ch): //byte short char int 返回给定字符在字符串中第一次出现的位置
int indexOf(String str);//返回给定的字符串在该字符串中第一次出现的位置
int indexOf(int ch,int fromIndex): 在指定位置开始,返回给定字符在字符串中第一次出现的位置
int indexOf(String str,int fromIndex)在指定位置开始,返回给定的字符串在该字符串中第一次出现的位置
String substring(int start): 在指定位置开始,截取该字符串,返回一个新字符串
String substring(int start,int end)在指定位置开始,到指定位置结束,截取该字符串,范返回一个新字符串(包左不包右)
转换功能:
byte[] getBytes(): 把该字符串 转换成 字节数组
char[] toCharArray(): 把该字符串 转换成字符数组
static String copyValueOf(char[] chs): 把给定的字符数组 转换为 字符串
static String valueOf(char[] chs): 把给定的字符数组 转换为 字符串
static String valueOf(int i)基本类型: 把给定的int数据 转换成字符串
String toLowerCase(): 把该字符串转换成小写字母
String toUpperCase(): 把该字符串转换成大写字母
String concat(String str): 字符串连接
替换功能
String replace(char old,char new): 在该字符串中,用指定的新字符 替换 指定老字符
String replace(String old,String new): 在该字符串中, 用指定的新字符串 替换 指定的老字符串
切割
String[] split(String regex): 使用给定的分隔符,将该字符串分解成多个字符串
去除字符串两空格
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
下面就一道题目来解释 == 与 equals 的区别:
题目内容:
/*
* 写出以下代码执行结果,分析为什么?(没有分析结果不得分)
* public class Test {
* public static void main(String[] args) {
* String s = "abc";
* s.substring(1);
* s.replace("bc", "xyz");
* System.out.println(s);
* String value = new String ("abc");
* System.out.println(s == value);
* }
* }
*
*/
下面附上我本人的理解:
/*
* 执行结果为
* abc
* false
*
* 解释:
* String s = "abc";
* 1,在栈内存中开辟空间存放字符串s的地址值
* 2,abc 存放在常量池
* 3,引用s只想常量池中的 abc
* abc:
* 1, s.substring(1); 引用String类里的功能,表示从 1 索引出开始直到字符串 s 的结尾
* 1>.在栈中开辟一片空间,存放常量池中 abc
* 2>.引用功能改变栈中的字符串为 abc 为bc
* 3>.功能调用完毕,弹栈
* 4>.常量池中的字符串并没有改变
*
* 2, s.replace("bc", "xyz"); 引用String类里的功能,表示用新的字符串"xyz"代替老的字符串"bc"
* 1>.在栈中开辟一篇空间,存放常量池中 abc
* 2>.引用功能改变栈中的字符串为 abc 为 axyz
* 3>.功能调用完毕,弹栈
* 4>.常量池中的字符串并没有改变
*
* 3, 所以输出结果还是字符串s
*
* false:
* String value = new String ("abc");
* 1>.栈中开辟一片空间存放 value 的地址值
* 2>.在堆中开辟一片空间存放一个新建的String对象"abc"
* 3>.value指向堆中的新建的String对象"abc"
* s == value
* 1>. == 比较的是地址值,
* 2>.value所指的对象地址为堆中地址,而常量 s 所指向地址在常量池中,因此输出为false
*
*/
4、Arrays类:
public static String toString(int[] a): 把数组转换成字符串
public static void sort(int[] a): 把数组排序
public static int binarySearch(int[] a,int key): 二分查找法
5、StringBuffer:
构造函数:
public StringBuffer(): 容量为 16 个字符
public StringBuffer(int capacity): 容量为指定的大小
public StringBuffer(String str): 容量为16字符 + 字符串的长度
需要掌握的方法:
public int capacity() 理想使用情况
public int length() 实际使用情况
添加元素相关的方法:
public StringBuffer append(String str) 在该字符串后面 追加给定的字符串,返回此对象的一个引用。其实返回的对象引用就是该对象本身
public StringBuffer insert(int offset,String str): 在给定位置,插入给定的字符串到原字符串中
删除元素的方法:
public StringBuffer delete(int start,int end): 从指定位置开始到指定位置结束的数据在该字符串中删除 ,返回此对象
public StringBuffer deleteCharAt(int index): 把指定位置的字符在字符串中删除, 返回此对象
替换元素
public StringBuffer replace(int start,int end, String str)从指定的开始位置,到指定的结束位置,把原来的数据 替换为 新的字符串数组
截取子串
public String substring(int start)从给定位置开始,截取该字符串,返回截取后一个新字符串
public String substring(int start,int end)从给定位置开始,到给定的位置结束,截取该字符串,返回截取后的一个新字符串
反转
public StringBuffer reverse() // 将原来字符串从后往前依次排列,如:abc --> cba
6、Integer:
字段: 成员变量
public static final int MAX_VALUE 最大值 2^31 -1
public static final int MIN_VALUE 最小值 -(2^31)
Integer类的构造函数
public Integer(int value): 把指定的int数据 转换成 Integer对象
public Integer(String s):把指定的字符串 转换成 Integer对象,使用要求:字符串只能由数字字符所组成“12345”
方法:
进制转换
public static String toBinaryString(int i): 将给定的10进制数 转换为 2进制,以字符串的形式显示
public static String toHexString(int i): 将给定的10进制数 转换为16进制,以字符串的形式显示
public static String toOctalString(int i): 将给定的10进制数 转换为8进制,以字符串的形式显示
public static int parseInt(String s): 将字符串 转换为 int数据
public static int parseInt(String s,int radix):把给定进制的字符串 转换为 10进制
7、Random:
构造函数:
public Random() : 使用默认种子来生成随机数对象.默认种子指的是当前时间的毫秒值
public Random(long l): 使用给定的种子来生成 随机数对象, 注意:当种子相同时,随机数也是一样的
方法:
nextInt(); 获取int数据范围内的随机数
nextInt(int n); 获取[0,n)(包左不包右)这个范围内的随机数
8、Date:
构造函数:
public Date(): 当前日期对象
public Date(long l): 给定毫秒数 对应的日期对象 (日期是从1970年1月1月 00:00:00开始计算的)
方法:
日期与毫秒数转换
public long getTime() 用来得到当前时间对象 对应的毫秒数
9、DateFormat
构造函数
public SimpleDateFormat():采用默认模式与默认语言区域生成日期格式化对象
public SimpleDateFormat(Stirng format): 采用给定的模式与默认语言区域生成日期格式化对象
方法:
把日期 格式化成 字符串
public final String format(Date date)
按照指定的格式,将字符串解析成日期对象
public Date parse(String source)
10、Calendar:
方法:
public static Calendar getInstance(): 获取一个Calendar类对象
public abstract void add(int field, int amount): 将当期日期对象中的给定字段进行修改 amount
public int get(int field): 获取当前日期对象中 给定的字段值
public final void set(int year,int month, int date): 设置当期日期对象中 年月日等
11、System:
方法:
public static void gc(): 运行垃圾回收器
public static void exit(int status) 终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止
public static long currentTimeMillis(): 返回以毫秒为单位的当前时间
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
以上API各个类的方法都是开发中想对来说比较常用的,整理归纳出来,可以方便记忆。
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
详细请查看:www.itheima.com