常见对象的常用方法总结

常见对象的常用方法总结


1.Object类
A:常用方法:
a:public String toString():默认返回该对象的字符串表示形式,一般需重写
b:public boolean equals(Object obj):该对象与obj对象是否相同,相同则返回true,不同则返回false
c:public int hashCode():返回该对象的一个哈希码值,可理解为地址值。
d:public final Class<?> getClass():返回字节码文件对象。
e:protected void finalize():用于垃圾回收,时间不确定
f:protected Object clone():返回一个实例的副本。


2.Scanner类
A:构造方法:
InputStream is = System.in;
B:常用格式:
Scanner sc = new Scanner(System.in);
C:常用方法:
a:public String next(String pattern):返回下一标记
b:public boolean hasNext(String pattern):匹配成功则返回true
c:public int nextInt():从输入信息扫描的 int
d:public String nextLine():从输入信息扫描的 String

3.String类
A:常用构造方法
a:public String():表示一个空字符序列
b:public String(byte[] bytes,int offset,int length):根据byte数组创建新字符串
c:public String(char[] value,int offset,int count):根据char数组创建新字符串
d:public String(String original):新创建的字符串是该参数字符串的副本
B:常用方法:
a:判断功能
boolean equals(Object obj):给定对象表示的 String 与此 String 相等,则返回 true;否则返回 false。
boolean equalsIgnoreCase(String str):将此 String 与另一个 String 比较,不考虑大小写
boolean contains(String str):如果此字符串包含str,则返回 true,否则返回 false 
boolean startsWith(String str):判断此字符串是否以指定的前缀开始。
boolean endsWith(String str):判断此字符串是否以指定的后缀结束。
boolean isEmpty():如果 length()为 0则返回true;否则返回 false。
b:获取功能
int length():此对象表示的字符序列的长度。
char charAt(int index):此字符串指定索引处的 char 值
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
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):截取字符串,包前不包后。
c:转换功能
byte[] getBytes():将此字符串转换为一个新的byte数组。
char[] toCharArray():将此字符串转换为一个新的字符数组。
static String valueOf(char[] chs):返回 char 数组参数的字符串表示形式。
static String valueOf(int i): 返回 int 参数的字符串表示形式。
String toLowerCase():所有字符都转换为小写
String toUpperCase():所有字符都转换为大写
String concat(String str):将指定字符串连接到此字符串的结尾。
d:替换功能
String replace(char old,char new):用新字符替换字符串中所有的旧字符,返回新字符串
String replace(String old,String new):用新子字符串替换旧子字符串,返回结果。
e:去空格功能
String trim():返回去除前后空格的字符串
f:按字典比较功能
int compareTo(String str):按字典顺序比较两个字符串。
int compareToIgnoreCase(String str):按字典顺序比较两个字符串,不考虑大小写。


4.StringBuffer类
A:构造方法
a:StringBuffer(): 构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
b:StringBuffer(int size):构造一个不带字符,但具有指定初始容量的字符串缓冲区。
c:StringBuffer(String str): 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
B:常见功能
a:追加添加功能
StringBuffer append(char[] str, int offset, int len):将 char 数组参数的子数组的字符串表示形式追加到此序列
StringBuffer append(String str):将指定的字符串追加到此字符序列。
StringBuffer insert(int index, char[] str, int offset, int len): 将数组参数 str 的子数组的字符串表示形式插入此序列中。 
StringBuffer insert(int offset, String str):将字符串插入此字符序列中。
b:删除功能
StringBuffer delete(int start, int end):移除此序列的子字符串中的字符。
c:替换功能
StringBuffer replace(int start, int end, String str):使用给定 String 中的字符替换此序列的子字符串中的字符。
d:反转功能
StringBuffer reverse():将此字符序列用其反转形式取代。
e:截取功能
CharSequence subSequence(int start, int end):返回一个新的字符序列,该字符序列是此序列的子序列。

5.Arrays类
A:常见方法
a:排序
static void sort(int[] a) 
b:把指定数组转成字符串
static String toString(char[] a) 
c:二分查找
static int binarySearch(int[] a, int fromIndex, int toIndex, int key) 
注意:使用二分查找前必须先排序

6.Integer类
A:构造方法
Integer(int value):表示指定的int值的Integer对象
Integer(String s):表示 String 参数所指示的int值的Integer对象
B:常见方法
int compareTo(Integer anotherInteger):在数字上比较两个 Integer 对象
static int parseInt(String s):将字符串参数作为有符号的十进制整数进行解析。
static Integer valueOf(int i): 返回一个表示指定的 int 值的 Integer 实例。
C:String和int的相互装换
a:String --- int
Integer.parseInt("100");
b:int --- String
String.valueOf(100);

7.Character
A:构造方法
Character(char value):构造一个新分配的 Character 对象,用以表示指定的 char 值
B:常用方法
a:判断功能
static boolean isDigit(char ch):确定指定字符是否为数字
static boolean isLetter(char ch):确定指定字符是否为字母
static boolean isLowerCase(char ch):确定指定字符是否为小写字母
static boolean isUpperCase(char ch):确定指定字符是否为大写字母
b:转换功能:
static char toTitleCase(char ch):将字符参数转换为首字母大写
static char toUpperCase(char ch):将字符参数转换为首字母大写
static char toLowerCase(char ch):将字符参数转换为小写
 
8.Math类
A:常见方法
a:绝对值
static int abs(int a):返回 int 值的绝对值
b:向下取整
public static double ceil(double a)
c:向上取整
public static double floor(double a)
d:两书中的大值
static int max(int a, int b):返回两个 int 值中较大的一个
static int min(int a, int b):返回两个 int 值中较小的一个
 
e:a的b次幂
static double pow(double a, double b):返回第一个参数的第二个参数次幂的值
f:随机数
public static double random():返回带正号的 double 值,该值大于等于 0.0 且小于 1.0
g:四舍五入
public static int round(float a):返回最接近参数的 int
static long round(double a):返回最接近参数的 long
h:正平方根
public static double sqrt(double a):

9.System类
A:常用方法
a:获取当前时间的毫秒值
static long currentTimeMillis():返回以毫秒为单位的当前时间 
b:数组复制
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
c:退出JVM
static void exit(int status):终止当前正在运行的 Java 虚拟机
d:运行垃圾回收器
static void gc():运行垃圾回收器

10.BigInteger类
A:常用构造方法
BigInteger(String val)
BigInteger(byte[] val)
B:常用方法
a:加
public BigInteger add(BigInteger val)
b:减
public BigInteger subtract(BigInteger val)
c:乘
public BigInteger multiply(BigInteger val)
d:除
public BigInteger divide(BigInteger val)
e:商和余数的数组
public BigInteger[] divideAndRemainder(BigInteger val)

11.Date/DateFormat
(1):Date:表示特定的瞬间,精确到毫秒
A:构造方法
a:Date():根据当前的默认毫秒值创建日期对象
b:Date(long date):根据给定的毫秒值创建日期对象
B:常用方法
public long getTime():获取时间,以毫秒为单位
public void setTime(long time):设置时间
(2):DateFormat:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat
A:SimpleDateFormat的构造方法
SimpleDateFormat():默认模式
SimpleDateFormat(String pattern):给自定义模式
B:  Date -- String(格式化)
public final String format(Date date)
String -- Date(解析)
public Date parse(String source)

12.Calendar类
A:获取当前日历对象
Calendar c = Calendar.getInstance();
B:常用方法
a:返回给定日历字段的值
int get(int field) 
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Caldar.DATE);
b:设置日期
void set(int year, int month, int date) 
设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值
c:操作日历
public void add(int field,int amount)
根据给定的日历字段和对应的时间,来对当前的日历进行操作
c.add(Calendar.YEAR,-5);
c.add(Calendar.MONTH,10);

13.正则表达式
A:常用功能
a:判断功能
String类的public boolean matches(String regex)
b:分割功能
String类的public String[] split(String regex)
c:替换功能
String类的public String replaceAll(String regex,String replacement)
d:获取功能
Pattern类和Matcher类的:
find():查找存不存在
group():获取刚才查找过的数据
注意:必须先判断是否存在,存在则能获取
B:获取功能步奏
//定义正则表达式规则
String regex = "...."
//将规则编译成模式对象
Pattern p = Pattern.compile(regex);
//通过模式对象得到匹配器对象
Matcher m = p.matcher(String str); //str是被操作的字符串
//使用循环获取子字符串
while(m.find()){
System.out.println(m.group());
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值