黑马程序员-Java基础:常用API
来源:互联网 发布:中国工控网软件下载 编辑:程序博客网 时间:2024/06/13 02:58
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
常用API总结
一、Object类
Object类可以说是所有类的”上帝”,所谓上帝的含义就是造万物,Java中所有的类都直接或间接继承自Object类,那么了解Object类中的方法有助于理解Java中所有类的共性。
重点方法介绍:
(1)boolean equals(Object obj)
Java中所有的对象都应该具备比较性,这个方法默认是通过比较对象的引用是否相同来确定对象是否相等的。
源码如下:
public boolean equals(Object obj) { return (this == obj); }(2)public int hashCode()
获取对象的哈希值,Java中不同的对象有不同的哈希值,这个值类似于对象的地址值。因此可以通过哈希值是否相同来判断是否是
同一个对象。自定义的类如果需要以自己的方式比较对象,则建议重写hashCode()和equals()方法
(3)public String toString()
获取对象的字符串表示形式,常用于打印对象中的成员以及成员的值,JavaAPI中大部分类都重写了此方法
Object类中toString()的源码:
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode());}
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
注意:
a:toString()重写.
b:字符串常量,一旦创建就不能改变 .
c:字符串等效于一个 字符数组
1、String构造方法
String s = new String(); 无参构造
public class StringDemo1 {public static void main(String[] args) throws UnsupportedEncodingException {//将字符串转化为数组char[] ch = {'a','c','中','国'};String string = new String(ch);System.out.println(string);String string2 = new String(ch,1,3); //1 表示角标偏移量, 2 长度System.out.println(string.length());System.out.println(string2);String string3 = new String("abcd");System.out.println(string3);}}
2、字符串的equals()方法
判断两个字符串地址值是否相等用“==”
判断两个字符串地址值是否相等用equals
public class StringDemo2 {public static void main(String[] args) {String s1 = "hello";String s2 = "world";String s3 = "helloworld";String s5 = s1+ s2;System.out.println(s3 == s5 ); // 比较地址值falseSystem.out.println(s3.equals((s5)));//比较内容true}}
3、获取字符串信息
(1)获取字符串长度
str.length();返回字符串str的长度值
(2)字符串查找
str.indexOf(substr);获取指定字符串substr在字符串str中的首次出现索引位置,找不到,则返回-1
(3)获取指定索引位置的字符。
str.charAt(int index);index:用于指定要返回字符的下标。
public class Test {public static void main(String[] args) {String str="helloWorld";//求字符串的长度System.out.println(str.length());//求字符“l”在str中首次出现的位置System.out.println(str.indexOf("l"));//获取str中索引5位置的字符System.out.println(str.charAt(5));}}
4、字符串替换
replace方法可将字符串中指定的字符或字符串换成新字符或字符串
str.replace(char oldChar ,char newChar)
public class Test {public static void main(String[] args) {String str="helloWorld";//str中的world替换成javaSystem.out.println(str.replace("World", "java"));}}输出结果:hellojava
5、字符串转换功能
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
public class Test {public static void main(String[] args) {String str="helloWorld";String str1="JAVA";//转换成大写 System.out.println(str.toUpperCase()); //转换成小写 System.out.println(str1.toLowerCase()); //字符拼接 System.out.println(str.concat(str1));}}输出结果:
HELLOWORLD
java
helloWorldJAVA
三、StringBuffer类概述
1、StirngBuffer类概述及其构造方法
StringBuffer类概述:
a:我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
b:线程安全的可变字符序列
构造方法
public StringBuffer() 无参构造方法
public StringBuffer(int capacity) 指定容量的字符串缓冲区对象
public StringBuffer(String str) 指定字符串内容的字符串缓冲区对象
public class StringBufferDemo {public static void main(String[] args) {StringBuffer sb = new StringBuffer();//16字节System.out.println(sb.capacity());System.out.println(sb.length());//长度System.out.println("-----------------------------");StringBuffer sb2 = new StringBuffer(10000);System.out.println(sb2.capacity());//容量System.out.println(sb2.length());//长度System.out.println("-----------------------------");String str = "abc";StringBuffer sb3 = new StringBuffer(str);StringBuffer reverse = sb3.reverse();System.out.println(reverse);System.out.println("-----------------------------");//new StringBuffer(new StringBuffer());}}
2、StirngBuffer的添加功能
public StringBuffer append(String str):
可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):
在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
public class StringBufferDemo2 {public static void main(String[] args) {StringBuffer sb = new StringBuffer("[");//每次添加到缓冲区后面StringBuffer append = sb.append("aaa").append(",").append("bbb").append("]");System.out.println(append);//在索引2出添加xStringBuffer insert = append.insert(2, "x");System.out.println(insert);}}
3、StirngBuffer的添加功能
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身[2,4)
ublic class StringBufferDemo3 {public static void main(String[] args) {StringBuffer sb = new StringBuffer("abcdefg");StringBuffer sb2 = sb.deleteCharAt(3);System.out.println(sb2);StringBuffer sb3 = sb2.delete(2, 4); //包含左边,不包含右边 System.out.println(sb3);StringBuffer sb4 = sb3.delete(0, sb3.length());System.out.println(sb4);System.out.println("---------------");}}输出结果:
abcefg
abfg
4、StirngBuffer的替换功能
A:StringBuffer的替换功能
public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
B:StringBuffer的反转功能public StringBuffer reverse():字符串反转
public class StringBufferDemo4 {public static void main(String[] args) {StringBuffer sb = new StringBuffer("itcast");StringBuffer replace = sb.replace(2,4, "xx");System.out.println(replace);String replace2 = "itcast".replace("ca", "xxxxx");System.out.println(replace2);StringBuffer reverse = replace.reverse();System.out.println(reverse);}}
5、StirngBuffer的截取功能
A:StringBuffer的截取功能
public String substring(int start):从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
B:注意事项
注意:返回值类型不再是StringBuffer本身public class StringBufferDemo5 {public static void main(String[] args) {StringBuffer sb = new StringBuffer("aaaaabbbbbccccc");//String substring = sb.substring(4);String substring = sb.substring(4, 11);//返回的对象是StringSystem.out.println(substring);System.out.println(sb);System.out.println("----------");String ss = "aaaaabbbbbccccc";String substring2 = ss.substring(4);System.out.println(ss);System.out.println(substring2);System.out.println("-----------------------------------");StringBuffer stringBuffer = new StringBuffer(substring);System.out.println(stringBuffer.reverse());}}<strong></strong>
6、StirngBuffer和String的转换
A:String --> StringBuffer
a:通过构造方法
b:通过append()方法
B:StringBuffer -- String
a:通过截取方法
b:通过toString()方法
c:通过String的构造方法
public class StringBufferDemo6 {public static void main(String[] args) {//String --> StringBuffer String s = "aaa";StringBuffer sb = new StringBuffer(s); //推荐 StringBuffer append = new StringBuffer().append(s);//StringBuffer --> String StringBuffer sb2 = new StringBuffer("bbb");String string = sb2.toString(); //推荐 String string2 = new String(sb2); }}
四、正则表达式
1、字符类
A:字符类 --验证单个字符""
[0-9] 0到9的字符都包括
[abc] a、b 或 c(简单类) -- [] 代表单个字符 ,a,b,c 中来一个就ok
"a","b","c","d","ab","ac"
[^abc] 任何字符,除了 a、b 或 c(否定)-- ^表示取反
"a","99"
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
"ab","1"
public class RegexDemo2 {public static void main(String[] args) {// 定义规则 fun1(); fun2(); fun3();//使用正则表达式 String qq = "";String regex2 = "[1-9][0-9]{4,14}";boolean matches = qq.matches(regex2);System.out.println(matches);//-------------------------------------------------String regex = "[a-zA-Z]";System.out.println("a".matches(regex));System.out.println("0".matches(regex));System.out.println("c".matches(regex));System.out.println("A".matches(regex));System.out.println("M".matches(regex));System.out.println("_".matches(regex));}private static void fun3() {String regex = "[^abc]";System.out.println("a".matches(regex));System.out.println("b".matches(regex));System.out.println("c".matches(regex));System.out.println("d".matches(regex));System.out.println("0".matches(regex));System.out.println("ab".matches(regex));// System.out.println("bc".matches(regex));System.out.println("".matches(regex));}private static void fun2() {String regex = "[abc]";System.out.println("a".matches(regex));System.out.println("b".matches(regex));System.out.println("c".matches(regex));System.out.println("d".matches(regex));System.out.println("ab".matches(regex));System.out.println("bc".matches(regex));System.out.println("".matches(regex));}private static void fun1() {String regex = "[0-9]";System.out.println("0".matches(regex));System.out.println("9".matches(regex));System.out.println("5".matches(regex));System.out.println("10".matches(regex));}}
2、预定义字符
B:预定义字符类
. 任何字符。一个. 一个字符 ".."两个字符
"a", "ab","%"
思考: 怎么表示呢? .
\d 数字:[0-9]
"10","0","9"
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
public class RegexDemo3 {public static void main(String[] args) {// 定义规则//fun1();//fun2();String regex ="\\w"; System.out.println("0".matches(regex));System.out.println("a".matches(regex));System.out.println("10".matches(regex));//0-9System.out.println("_".matches(regex));System.out.println("A".matches(regex));System.out.println("$".matches(regex));}private static void fun2() {String regex ="\\d"; // \ 是转义的 ,\\ 才能表示 \ System.out.println("0".matches(regex));System.out.println("a".matches(regex));System.out.println("10".matches(regex));}private static void fun1() {String regex ="..";System.out.println("a".matches(regex));System.out.println("00".matches(regex));System.out.println("*".matches(regex));System.out.println("%%".matches(regex));System.out.println(".".matches(regex));}}
3、Greedy 数量词
X? X,一次或一次也没有regex = "[abc]?"-- a,b,c <=1,不能有其他的. 要么啥也没有,要么 abc 有一个
"a","d","","ab"
X* X,零次或多次 -- a,b,c>=0 不能有其他的. 要么啥也没有,要么 abc 有多个
X+ X,一次或多次 -- a,b,c>=1 不能有其他的. 要么 abc 有一个或者多个
X{n} X,恰好 n 次 --=n次
X{n,} X,至少 n 次 -- >=n次
X{n,m} X,至少 n 次,但是不超过 m 次 -- >=n && <=m
public class RegexDemo4 {public static void main(String[] args) {//fun1();fun2();//fun3();System.out.println("----------------------------------");String regex ="[abc]{5}"; System.out.println("aaaaaaaaaaaaaaaaaabbbbb".matches(regex));System.out.println("bc".matches(regex));System.out.println("ac".matches(regex));System.out.println("abcab".matches(regex));System.out.println("abcdb".matches(regex));System.out.println("ad".matches(regex));System.out.println("----------------");System.out.println("".matches(regex));System.out.println("a".matches(regex));System.out.println(" ".matches(regex));System.out.println("d".matches(regex));}private static void fun3() {String regex ="[abc]+";System.out.println("aaaaaaaaaaaaaaaaaabbbbb".matches(regex));System.out.println("bc".matches(regex));System.out.println("ac".matches(regex));System.out.println("abcabc".matches(regex));System.out.println("ad".matches(regex));System.out.println("----------------");System.out.println("".matches(regex));System.out.println("a".matches(regex));System.out.println(" ".matches(regex));System.out.println("d".matches(regex));}private static void fun2() {String regex ="[abc]*";System.out.println("aaaaaaaaaaaaaaaaaabbbbb".matches(regex));//trueSystem.out.println("bc".matches(regex));//trueSystem.out.println("ac".matches(regex));//trueSystem.out.println("abcabc".matches(regex));//trueSystem.out.println("ad".matches(regex));//falseSystem.out.println("----------------");System.out.println("".matches(regex));//trueSystem.out.println("a".matches(regex));//trueSystem.out.println(" ".matches(regex));//falseSystem.out.println("d".matches(regex));//false}private static void fun1() {String regex ="[abc]?";System.out.println("aa".matches(regex));//falseSystem.out.println("b".matches(regex));//trueSystem.out.println("b".matches(regex));//trueSystem.out.println("d".matches(regex));//trueSystem.out.println("".matches(regex));//trueSystem.out.println(" ".matches(regex));//false}}
- 黑马程序员 JAVA基础---API常用类
- 黑马程序员----JAVA基础----常用对象API
- 黑马程序员-Java基础:常用API
- 黑马程序员-Java基础:常用API
- 黑马程序员 JAVA基础 ---API常用类(二)
- 黑马程序员 Java基础--API常用类(三)
- 黑马程序员——Java基础---API常用类
- 黑马程序员—java基础—常用API
- 黑马程序员 Java常用API
- 黑马程序员_Java基础常用API
- 《黑马程序员》java笔记->常用API
- 黑马程序员-----JAVA常用API、集合
- 黑马程序员——java常用api
- 黑马程序员Java之常用API
- 黑马程序员——Java常用API
- 黑马程序员-常用Java API介绍
- 黑马程序员---java基础---08常见api
- 黑马程序员--Java基础之API
- 腾讯面试(三)
- Android 控件之六:RadioGroup and RadioButton 单选组和单选钮
- Linux线程同步
- 演示定时刷新
- JAVA基础学习(五)---面向对象一类,对象、封装
- 黑马程序员-Java基础:常用API
- 新的开始
- CSS选择符及优先级计算
- Lowmemorykiller笔记
- byte[]和InputStream的相互转换
- Android ADB命令大全(通过ADB命令查看wifi密码、MAC地址、设备信息、操作文件、查看文件、日志信息、卸载、启动和安装APK等)
- 下载
- 基于1.3.3版本tooltip的datagrid单元格tip实现
- Cocos2d-JS 创建node节点示例