目录
一、API概述
- API(Application Programming Interface)
- 应用程序编程接口
- 编写一个机器人程序去控制机器人踢足球,程序就需要向机器人发出向前跑、向后跑、射门、抢球等各种命令,没有编过程序的人很难想象这样的程序如何编写。但是对于有经验的开发人员来说,知道机器人厂商一定会提供一些用于控制机器人的Java类,这些类中定义好了操作机器人各种动作的方法。其实,这些Java类就是机器人厂商提供给应用程序编程的接口,大家把这些类称为Xxx Robot API。本章涉及的Java API指的就是JDK中提供的各种功能的Java类。
- 学习汉语和学习编程的异同点
二、常用类
1.Object类
1.1 Object类概述及其构造方法
- Object 类概述
- 构造方法
- public Object()
- 回想面向对象中为什么说:
1.2Object类的成员方法
- public int hashCode() 返回对象的哈希码值
- public final Class getClass() 表示 类对象的运行时类的Class对象
- public String toString() 返回对象的字符串表示形式
- public boolean equals(Object obj) 指示一些其他对象是否等于此
- protected void finalize() 用于垃圾回收的
- protected Object clone() 返回的是这个实例的一个克隆,被Object类接收
2.Scanner类
2.1 Scanner类概述及其构造方法
- Scanner类概述
- 构造方法
- public Scanner(InputStream source)
2.2 Scanner类的成员方法
- 基本格式
- hasNextXxx() 判断是否还有下一个输入项,其中Xxx可以是Int,Double等。如果需要判断是否包含下一个字符串,则可以省略Xxx
- nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同
- 默认情况下,Scanner使用空格,回车等作为分隔符
- 常用方法
- public int nextInt()
- public String nextLine()
3.String类
3.1 String类概述及其构造方法
- String类概述
- 字符串是由多个字符组成的一串数据(字符序列)
- 字符串可以看成是字符数组
- 构造方法
- public String()
- public String(byte[] bytes)
- public String(byte[] bytes,int offset,int length)
- public String(char[] value)
- public String(char[] value,int offset,int count)
- public String(String original)
public class StringDemo1 {
public static void main(String[] args) {
String s = new String();
System.out.println("s: " + s);
System.out.println("字符串s的长度:" + s.length());
System.out.println("*********************************");
//public String(byte[] bytes) 将一个字节数组转成一个字符串
byte[] b = {97, 98, 99, 100, 101};
String s1 = new String(b);
System.out.println("s1: " + s1);
System.out.println("字符串s1的长度为:" + s1.length());
System.out.println("*********************************");
//public String(byte[] bytes,int index,int length)
//将字节数组中的一部分截取出来变成一个字符串
String s2 = new String(b, 1, 3);
System.out.println("s2: " + s2);
System.out.println("字符串s2的长度为:" + s2.length());
System.out.println("*********************************");
//public String(char[] value)
//将一个字符数组转化成一个字符串
char[] c = {'a', 'b', 'c', 'd', '我', '爱', '冯', '提', '莫'};
String s3 = new String(c);
System.out.println("s3: " + s3);
System.out.println("字符串s3的长度为:" + s3.length());
System.out.println("*********************************");
//public String(char[] value,int index,int count)
//将字符数组中的一部分截取出来变成一个字符串
String s4 = new String(c, 4, 5);
System.out.println("s4: " + s4);
System.out.println("字符串s4长度:" + s4.length());
System.out.println("*********************************");
//public String(String original)
String s5 = "qwerdf";
String s6 = new String(s5);
System.out.println("s6: " + s6);
System.out.println("字符串s6的长度为:" + s6.length());
}
}
3.2 String类的特点及其面试题
- 字符串是常量,它的值在创建之后不能更改
- String s = “hello”; s += “world”; 问s的结果是多少?
- 面试题
- String s = new String(“hello”)和String s = “hello”;的区别?
package com.shujia.java.sx.day14;
/*
String s = new String(“hello”)和String s = “hello”;的区别?
1、==比较引用数据类型比较的是地址值
2、equals默认比较的是地址值,但是由于String类中重写了该方法,所以比较的是内容
3、String s = new String(“hello”) 会在堆内存中创建对象
*/
public class StringDemo3 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); //true
}
}
3.3 String类的判断功能
- boolean equals(Object obj)
- boolean equalsIgnoreCase(String str)
- boolean contains(String str)
- boolean startsWith(String str)
- boolean endsWith(String str)
- boolean isEmpty()
package com.shujia.java.sx.day14;
/*
String 类的判断功能:
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean startsWith(String str)
boolean endsWith(String str)
boolean isEmpty()
*/
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//boolean equals(Object obj) 比较字符串中的内容是否相同区分大小写
System.out.println(s1.equals(s2));//false
System.out.println(s1.equals(s3));//false
System.out.println("********************");
//boolean equals IgnoreCase(String str)
//比较字符串中的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));//true
System.out.println(s1.equalsIgnoreCase(s3));//true
System.out.println("********************");
//boolean contains(String str)
//判断大的字符串中是否包含小的字符串,如果包含,返回的是true,否则就是false
System.out.println(s1.contains("Hello"));//false
System.out.println(s1.contains("leo"));//false
System.out.println(s1.contains("hello"));//true
System.out.println("*******************");
//boolean startsWith(String str)测试此字符串是否以指定的前缀开头。区分大小写
System.out.println(s1.startsWith("hel"));//true
System.out.println(s1.startsWith("H"));//false
System.out.println(s1.startsWith("h"));//true
System.out.println(s1.startsWith("he"));//true
System.out.println(s1.startsWith("he23"));//false
System.out.println("***************************");
//boolean endsWith(String str)//测试此字符串是否以指定的后缀结尾。
// 区分大小写
System.out.println(s1.endsWith("orld"));//true
System.out.println(s1.endsWith("orlD"));//false
System.out.println("*******************");
//boolean isEmpty()//判断元素是否为空,如果为空返回true,否则返回false
System.out.println(s1.isEmpty());//false
System.out.println("*******************");
String s4 = "";
String s5 = null;
System.out.println(s4==s5);//false
System.out.println(s4.isEmpty());//true
//由于s5的对象都没有,所以不能够调用方法,报错,空指针异常
// System.out.println(s5.isEmpty());//NullPointerException
String s6 = "bigdata";
String s7 = null;
// System.out.println(s6.equals(s7));
// System.out.println(s7.equals(s6));
//需求:在不知道s6和s7的前提下,想要与字符串"hadoop"进行内容比较
// System.out.println(s6.equals("hadoop"));
// System.out.println(s7.equals("hadoop"));
//在进行字符串内容比较的时候,为了防止出现空指针异常,将变量放后面
System.out.println("hadoop".equals(s6));//false
System.out.println("hadoop".equals(s7));//false
}
}
3.4 String类的获取功能
- int length()
- char charAt(int index)
- 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)
package com.shujia.java.sx.day14;
/*
String类的获取功能:
int length()
char charAt(int index)
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)
*/
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int length() 获取字符串的长度
System.out.println(s.length());
System.out.println("***************************");
//public char charAt(int index)返回char指定索引处的值
//从0开始到length()-1
System.out.println(s.charAt(7));//r
System.out.println(s.charAt(0));//h
// System.out.println(s.charAt(11));//StringIndexOutOfBoundsException
System.out.println("***************************");
//public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引。
//从0开始到length()-1
//传入的是ASCLL码值
System.out.println(s.indexOf(108)); //2
System.out.println(s.indexOf('l')); //2
System.out.println("***************************");
//public int indexOf(String str)
// 返回指定子字符串第一次出现的字符串内的索引。
//字符串第一个字符索引
System.out.println(s.indexOf("owo")); //4
//当子字符串不存在的时候,返回的是-1
System.out.println(s.indexOf("poi")); //-1
System.out.println("***************************");
// public int indexOf(int ch,int fromIndex)
// 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
System.out.println(s.indexOf('l',4)); //8
System.out.println(s.indexOf('p',4)); //-1
System.out.println(s.indexOf('l',40)); //-1
System.out.println(s.indexOf('p',40)); //-1
System.out.println("***************************");
//int indexOf(String str,int fromIndex)
//返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索。
System.out.println(s.indexOf("ll",1));
System.out.println("***************************");
//public String substring(int beginIndex)
// 返回一个字符串,该字符串是此字符串的子字符串。
// 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾。
System.out.println(s.substring(3)); //loworld
// System.out.println(s.substring(20)); //StringIndexOutOfBoundsException
System.out.println("***************************");
//public String substring(int beginIndex,int endIndex)
// 返回一个字符串,该字符串是此字符串的子字符串。
// 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。
//左闭右开 [ , )
System.out.println(s.substring(4,7)); //owo
System.out.println(s.substring(1,20)); //StringIndexOutOfBoundsException
}
}
3.5 String类的转换功能
- byte[] getBytes()
- char[] toCharArray()
- static String valueOf(char[] chs)
- static String valueOf(int i)
- String toLowerCase()
- String toUpperCase()
- String concat(String str)
package com.shujia.java.sx.day14;
/*
String类的转换功能:
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
*/
public class StringDemo8 {
public static void main(String[] args) {
String s = "HelloWorLD";
//public byte[] getBytes()
// 使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
byte[] b1 = s.getBytes();
// System.out.println(b1); //[B@4554617c 数组没有重写toString()方法 打印的是地址值
for(int i=0;i<b1.length;i++){
System.out.println(b1[i]);
}
System.out.println("*********************");
//char[] toCharArray()将此字符串转换为新的字符数组。
// 字符串 ---> 字符数组
char[] chars = s.toCharArray();
for(int i = 0;i<chars.length;i++){
System.out.print(chars[i]);
}
System.out.println();
//增强for循环
// for (char aChar : chars) {
// System.out.println(aChar);
// }
System.out.println("*********************");
//字符数组 ----> 字符串
//static String valueOf(char[] chs)
System.out.println(chars);
System.out.println("*********************");
//static String valueOf(int i)
//将int类型的数据转化成字符串类型
String s1 = String.valueOf(100); // "100" --> 100
System.out.println(s1);
System.out.println("*********************");
//String toLowerCase()
//将字符串内容全部转化成小写
String s2 = s.toLowerCase(); //HelloWorLD
System.out.println(s2); //helloworld
System.out.println("*********************");
// String toUpperCase()
//将字符串内容全部转大写
String s3 = s.toUpperCase();//HelloWorLD
System.out.println(s3);//HELLOWORLD
System.out.println("*********************");
//public String concat(String str)将指定的字符串连接到该字符串的末尾。
//将小括号里面的字符串拼接到调用方法的字符串后面
String s4 = "Hello";
String s5 = "World";
String s6 = s4+s5;
String s7 = s5.concat(s6);
System.out.println("s4: "+s4); //Hello
System.out.println("s5: "+s5); //World
System.out.println("s6: "+s6); //HelloWorld
System.out.println("s7: "+s7); //WorldHelloWorld
// String s8 = "";
String s8 = null;
// System.out.println(s8.concat(s5));//NullPointerException
// System.out.println(s5.concat(s8));//NullPointerException
}
}
3.6 String类的其他功能
- 替换功能
- String replace(char old,char new)
- String replace(String old,String new)
- 去除字符串两空格
- 按字典顺序比较两个字符串
- int compareTo(String str)
- int compareToIgnoreCase(String str)
package com.shujia.java.sx.day15;
/*
替换功能
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格 ******
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
*/
public class StringDemo11 {
public static void main(String[] args) {
String s = "helloworld";
//String replace(char old,char new)
//将字符串中所有的l替换成a,返回一个新的字符串
String replaceString = s.replace('l', 'a');
System.out.println(replaceString);
System.out.println(s);
//String replace(String old,String new)
String replaceString2 = s.replace("owo", "wow");
System.out.println(replaceString2);
String replaceString3 = s.replace("owo", "qwerdf");
System.out.println(replaceString3);
//如果被替换的字符串不存在,返回原来的字符串
String replaceString4 = s.replace("opi", "qwe");
System.out.println(replaceString4);
//String trim()去除字符串两边的空格,不能去除字符串中间的空格
String s1 = " hello world ";
String trimString = s1.trim();
System.out.println(s1);
System.out.println(trimString);
//int compareTo(String str)
String s2 = "hello"; //h的ASCLL码值:104
String s3 = "hello";
String s4 = "abc"; // a的ASCLL码值:97
String s5 = "qwe"; // q的ASCLL码值:113
System.out.println(s2.compareTo(s3)); // 0
System.out.println(s2.compareTo(s4)); // 7
System.out.println(s2.compareTo(s5)); // -9
}
}
3.7 String练习
package com.shujia.java.sx.day15;
/*
把数组中的数据按照指定个格式拼接成一个字符串
举例:int[] arr = {1,2,3}; 输出结果:[1, 2, 3]
分析:
1、定义一个空字符串
2、先在字符串前面拼接一个"["
3、遍历该int型数组,获取到每一个元素
4、判断是否读到了最后一个元素,如果是,在后面拼接上一个"]"
5、输出
*/
public class StringDemo13 {
public static void main(String[] args) {
int[] arr = {1,2,3};
//1、定义一个空字符串
String s = "";
//先在字符串前面拼接一个"["
s+="[";
//3、遍历该int型数组,获取到每一个元素
for(int i = 0;i<arr.length;i++){
//判断是否读到了最后一个元素,如果是,在后面拼接上一个"]"
if(i == arr.length-1){
s+=arr[i];
s+="]";
}else {
s+=arr[i];
s+=",";
}
}
//5、输出
System.out.println(s);
}
}
package com.shujia.java.sx.day15;
/*
字符串反转
举例:键盘录入”abc” 输出结果:”cba”
分析:
1、键盘录入一个字符串
2、定义一个空的字符串
3、将字符串转换成字符数组
4、倒着遍历字符数组,得到每一个字符
5、将获取到的每个字符拼接到新的字符换中
6、输出
*/
import java.util.Scanner;
public class StringDemo14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//1、键盘录入一个字符串
System.out.println("请输入一个字符串:");
String s = sc.next();
//2、定义一个空的字符串
String s1 = "";
//3、将字符串转换成字符数组
char[] chars = s.toCharArray();
//4、倒着遍历字符数组,得到每一个字符
for(int i =chars.length-1;i>=0;i--){
//5、将获取到的每个字符拼接到新的字符换中
s1+=chars[i];
}
//6、输出
System.out.println(s1);
}
}
package com.shujia.java.sx.day15;
/*
统计大串中小串出现的次数
举例:
在字符串” woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagundeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxin”
java 出现的次数
*/
public class StringDemo15 {
public static void main(String[] args) {
//定义一个字符串
String s = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagundeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxindeaijavawozhendehenaijavaxin";
//定义一个需要查找的小串
String minString = "owo";
//定义一个变量统计出现的次数
int count = 0;
//在大串中找到小串第一次出现的位置
int index = s.indexOf(minString);
// System.out.println(firstIndex);
//也可能找不到
if(index == -1){
System.out.println("大串中没有小串");
}else {
while (index !=-1){
count++;
//截取字符串
int startIndex = index + minString.length();
s = s.substring(startIndex);
index = s.indexOf(minString);
}
}
//输出
System.out.println("java出现的次数为:"+count);
}
}
4.StringBuffer类
4.1 StringBuffer类概述及其构造方法
- StringBuffer类概述
- 我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
- 线程安全的可变字符序列
- StringBuffer和String的区别?
- 前者的长度和内容都是可变的,后者都不可变
- 前者可以提前给出一个缓冲区,可以进行字符串的拼接,而不会重新开辟新的空间 后者,每创建新的字符串都会开辟新的空间
- 构造方法
- public StringBuffer()
- public StringBuffer(int capacity)
- public StringBuffer(String str)
public class StringBufferDemo1 {
public static void main(String[] args) {
// StringBuffer() 构造一个没有字符的字符串缓冲区,初始容量为16个字符。
StringBuffer sb = new StringBuffer();
System.out.println("sb: "+sb); //重写了toString()方法
int capacity = sb.capacity();//查看初始容量16
System.out.println(capacity);
System.out.println(sb.length());//0
//StringBuffer(int capacity) 构造一个没有字符的字符串缓冲区和指定的初始容量。
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2: "+sb2); //重写了toString()方法
int capacity1 = sb2.capacity();//查看初始容量
System.out.println(capacity1);
System.out.println(sb2.length());//0
//StringBuffer(String str) 构造一个初始化为指定字符串内容的字符串缓冲区。
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3: "+sb3); //重写了toString()方法
int capacity2 = sb3.capacity();//查看初始容量
System.out.println(capacity2); //21 16+5
System.out.println(sb3.length());//5
}
}
4.2 StringBuffer类的成员方法
- 添加功能
- public StringBuffer append(String str)
- public StringBuffer insert(int offset,String str)
package com.shujia.java.sx.day15;
/*
StringBuffer:
添加功能
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();
//public StringBuffer append(String str)
// StringBuffer stringBuffer = sb.append("hello");
// System.out.println(sb); //hello
// System.out.println(stringBuffer); //hello
// System.out.println(sb == stringBuffer); //true
// sb.append("hello");
// sb.append(true);
// sb.append(10);
// sb.append(12.34);
// System.out.println(sb);
// System.out.println(stringBuffer);
//链式编程:
sb.append("hello").append(true).append(10).append(12.34);
System.out.println(sb); //hellotrue1012.34
//public StringBuffer insert(int index,String str)
//在指定的索引上,插入字符串
sb.insert(5,"java");
System.out.println(sb); //hellojavatrue1012.34
}
}
- 删除功能
- public StringBuffer deleteCharAt(int index)
- public StringBuffer delete(int start,int end)
package com.shujia.java.sx.day15;
/*
删除功能
public StringBuffer deleteCharAt(int index)
public StringBuffer delete(int start,int end)
*/
public class StringBufferDemo3 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("hello").append("java").append("bigdata");
System.out.println(sb);
System.out.println("******************************");
//public StringBuffer deleteCharAt(int index)删除char在这个序列中的指定位置。
//返回的内容是本身
sb.deleteCharAt(5);
System.out.println(sb);//helloavabigdata
// sb.deleteCharAt(20); //StringIndexOutOfBoundsException
// System.out.println(sb);
//public StringBuffer delete(int start,int end)
// 删除此序列的子字符串中的字符。
// 子串开始于指定start并延伸到字符索引end - 1 ,
// 或如果没有这样的字符存在的序列的结束。 如果start等于end ,则不作任何更改。
sb.delete(5,8);
System.out.println(sb);//hellobigdata
//如何利用现有方式删除所有的字符
System.out.println("******************************");
sb.delete(0,sb.length());
System.out.println(sb);
}
}
- 替换功能
- public StringBuffer replace(int start,int end,String str)
package com.shujia.java.sx.day15;
/*
替换功能
public StringBuffer replace(int start,int end,String str)
用指定的String中的字符替换此序列的子字符串中的String 。
子串开始于指定start并延伸到字符索引end - 1 ,
或如果没有这样的字符存在的序列的结束。
第一子串中的字符被去除,然后指定String被插入在start 。 (
如果需要,此序列将被延长以容纳指定的字符串。)
*/
public class StringBufferDemo4 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("hello").append("hadoop").append("hive").append("spark");
System.out.println(sb);//hellohadoophivespark
//public StringBuffer replace(int start,int end,String str)
//这里的start和end表示的是原来的字符串需要替换的区间,含头不含尾
sb.replace(11,12,"真好");
System.out.println(sb);//hellohadoop真好ivespark
}
}
- 反转功能
- public StringBuffer reverse()
package com.shujia.java.sx.day15;
/*
反转功能 public StringBuffer reverse()导致该字符序列被序列的相反代替。
如果序列中包含任何替代对,则将它们视为单个字符进行反向操作。
*/
public class StringBufferDemo5 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("我爱莫提冯");
sb.reverse();
System.out.println(sb);
}
}
- 截取功能
- public String substring(int start)
- public String substring(int start,int end)
package com.shujia.java.sx.day15;
/*
截取功能
String substring(int start)
返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列。
String substring(int start, int end)
返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。
*/
public class StringBufferDemo6 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("java")
.append("hadoop")
.append("hive")
.append("spark")
.append("flink");
System.out.println(sb);
System.out.println("******************");
//String substring(int start)
//返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列
String substring = sb.substring(4);
System.out.println("截出来的字符串为:"+substring);
System.out.println("StringBuffer的内容为:"+sb);
//String substring(int start, int end)
//返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。
String substring1 = sb.substring(14, 19);
System.out.println("截出来的字符串为:"+substring1);
System.out.println("StringBuffer的内容为:"+sb);
}
}
4.3 StringBuffer类练习
package com.shujia.java.sx.day15;
/*
String和StringBuffer的相互转换
A--B,把A转化为B,为了是使用B的功能
B--A,再把B转化为A,可能是最终的结果是需要A类型,所以还得转化回来
*/
public class StringBufferDemo7 {
public static void main(String[] args) {
//String -- StringBuffer
String s = "hello";
//String类型不能直接赋值给StringBuffer类型
// StringBuffer sb1 = s;
// StringBuffer sb = "helloworld";
//方式1:通过构造方法转换
StringBuffer sb = new StringBuffer(s);
System.out.println(sb);
//方式2:通过StringBuffer的append()方法
StringBuffer sb2 = new StringBuffer();
sb2.append(s);
System.out.println(sb2);
//StringBuffer -- String
StringBuffer sb3 = new StringBuffer("bigdata");
//方式1:通过调用StringBuffer的toString()方法
String s1 = sb3.toString();
System.out.println(s1);
//方式2:通过String的构造方法,将StringBuffer作为参数传入
String string = new String(sb3);
System.out.println(string);
}
}
package com.shujia.java.sx.day15;
/*
把数组拼接成一个字符串(用StringBuffer实现)
*/
public class StringBufferDemo8 {
public static void main(String[] args) {
char[] chars = {'a','b','c','d'};
StringBuffer sb = new StringBuffer();
for(int i= 0;i<chars.length;i++){
sb.append(chars[i]);
}
String s = sb.toString();
System.out.println(s);
}
}
package com.shujia.java.sx.day15;
/*
把字符串反转,键盘录入字符串
*/
import java.util.Scanner;
public class StringBufferDemo9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.next();
//方式1:用String拼接
String s = "";
//将输入的字符串转化为字符数组
char[] chars = line.toCharArray();
for(int i=chars.length-1;i>=0;i--){
s += chars[i];
}
System.out.println("反转之后的字符串为:"+s);
System.out.println("********************************");
//方法2:利用StringBuffer的reverse()方法
StringBuffer sb = new StringBuffer(line);
StringBuffer reverse = sb.reverse();
String s1 = reverse.toString();
System.out.println("反转之后的字符串为:"+s1);
System.out.println("链式编程:");
System.out.println(new StringBuffer(line).reverse().toString());
}
}
package com.shujia.java.sx.day15;
/*
把字符串反转,键盘录入字符串
*/
import java.util.Scanner;
public class StringBufferDemo9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.next();
//方式1:用String拼接
String s = "";
//将输入的字符串转化为字符数组
char[] chars = line.toCharArray();
for(int i=chars.length-1;i>=0;i--){
s += chars[i];
}
System.out.println("反转之后的字符串为:"+s);
System.out.println("********************************");
//方法2:利用StringBuffer的reverse()方法
StringBuffer sb = new StringBuffer(line);
StringBuffer reverse = sb.reverse();
String s1 = reverse.toString();
System.out.println("反转之后的字符串为:"+s1);
System.out.println("链式编程:");
System.out.println(new StringBuffer(line).reverse().toString());
}
}
4.4 StringBuffer类面试题
- String,StringBuffer,StringBuilder的区别
- String的内容不可变,而StringBuffer和StringBuilder的内容是可变的
- StringBuffer是同步线程安全的,数据安全,效率低 StringBuilder是不同步的,线程不安全,数据不安全,效率高
- StringBuffer和数组的区别
- 它们两个都可以被看作是一个容器,装一些数据 但是呢,StringBuffer里面的数据都是字符串数据 数组可以存放不同数据类型的数据,但是同一个数组只允许存放同一类型的数据。
- 看程序写结果:
- String作为参数传递
- StringBuffer作为参数传递
- 只有当谁有操作的时候,那个StringBuffer才会变化
5.数组高级
5.1 排序和查找
- 排序
- 冒泡排序
- 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
- 选择排序
- 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
- 查找
5.2 数组高级练习题
package com.shujia.java.sx.day15;
/*
把字符串进行排序
举例:"gdsabcm"
结果:"abcdgms"
分析:
1、定义一个需要排序的字符串
2、将字符串转化为字符数组
3、把字符数组进行排序
4、把排序后的字符数组在转为字符串
5、输出
*/
public class ArrayTest {
public static void main(String[] args) {
//定义一个字符串
String s = "gdsabcm";
//将字符串转化为字符数组
char[] chars = s.toCharArray();
//把字符数组进行排序
//使用冒泡排序
bubblingSort(chars);
//把排序后的字符数组在转为字符串
String s1 = String.valueOf(chars);
System.out.println("排序后的字符串为:" + s1);
}
public static void bubblingSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}
6.Arrays类
6.1 Arrays类概述及其常用方法
- Arrays类概述
- 成员方法
- public static String toString(int[] a)
- public static void sort(int[] a)
- public static int binarySearch(int[] a,int key)
package com.shujia.java.sx.day15;
import java.util.Arrays;
/*
Arrays:针对数组进行操作的工具类。提供了排序,查找等功能。
public static String toString(int[] a)
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)
*/
public class ArraysDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {21, 32, 42, 1, 3, 65, 20};
//public static String toString(int[] a)
//将数组转成字符串
System.out.println("排序前:" + Arrays.toString(arr));
//public static void sort(int[] a)
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
//public static int binarySearch(int[] a,int key)
//二分查找 {21, 32, 42, 1, 3, 65, 20}
//二分查找的前提:元素必须是有序的
System.out.println("二分查找 65:" + Arrays.binarySearch(arr, 65));//6
System.out.println("二分查找 655:" + Arrays.binarySearch(arr, 655));//-8
}
}
6.2 Arrays类常用方法源码详细解释
- public static String toString(int[] a) 源码解析
- public static int binarySearch(int[] a,int key) 源码解析
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
7.基本类型包装类(Integer Character)
7.1 基本类型包装类概述
- 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
- 常用的操作之一:用于基本数据类型与字符串之间的转换。
- 基本类型和包装类的对应
- Byte,Short,Integer,Long,Float,Double Character,Boolean
7.2 Integer类概述及其构造方法
- Integer类概述
- Integer 类在对象中包装了一个基本类型 int 的值
- 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法
- 构造方法
- public Integer(int value)
- public Integer(String s)
7.3 Integer类成员方法
- public int intValue()
- public static int parseInt(String s)
- public static String toString(int i)
- public static Integer valueOf(int i)
- public static Integer valueOf(String s)
package com.shujia.java.sx.day16;
/*
int类型与String类型的相互转换
int --- String
public static String valueOf(int i)
String -- int -- Integer
public static int parseInt(String s)
*/
public class IntegerDemo {
public static void main(String[] args) {
//int -- String
int num = 100;
//方式1:
String s1 = ""+num;
System.out.println(s1);
//方法2:
String s2 = String.valueOf(100);
System.out.println(s2);
//方式3:
//int -- Integer -- String
Integer integer = new Integer(100);
System.out.println(integer);
//public String toString()
String s = integer.toString();
System.out.println(s);
//方式4:源码中有一个public static String toString(int i)
String s3 = Integer.toString(100);
System.out.println(s3);
System.out.println("********************************");
//String -- int
String string = "100";
//方式1:
//String -- Integer -- int
Integer i1 = new Integer(string);
System.out.println(i1);
//public int intValue()
int intI1 = i1.intValue();
System.out.println(intI1);
//方式2:
//public static int parseInt(String s)
int i = Integer.parseInt(string);
System.out.println(i);
// int i2 = Integer.parseInt("100abc"); //NumberFormatException
// System.out.println(i2);
}
}
- 常用的基本进制转换
- public static String toBinaryString(int i)
- public static String toOctalString(int i)
- public static String toHexString(int i)
- 十进制到其他进制
- public static String toString(int i,int radix)
- 其他进制到十进制
- public static int parseInt(String s,int radix)
package com.xuexi.java.zy.day16;
/*
常用的基本进制转换
public static String toBinaryString(int i)
public static String toOctalString(int i)
public static String toHexString(int i)
十进制到其他进制
public static String toString(int i,int radix)
由这个我们也看到了进制的范围:2-36
为什么呢?0.....9,a.....z;
其他进制到十进制
public static int parseInt(String s,int radix)
*/
public class IntegerDemo2 {
public static void main(String[] args) {
//十进制到二进制、八进制、十六进制
System.out.println(Integer.toBinaryString(100));
System.out.println(Integer.toOctalString(100));
System.out.println(Integer.toHexString(100));
System.out.println("*************************************");
//十进制到其他进制
System.out.println(Integer.toString(100,10));
System.out.println(Integer.toString(100,2));
System.out.println(Integer.toString(100,8));
System.out.println(Integer.toString(100,16));
System.out.println(Integer.toString(100,4));
System.out.println(Integer.toString(100,40));
System.out.println(Integer.toString(100,-4));
System.out.println("***************************************");
//其他进制到十进制
System.out.println(Integer.parseInt("100",10));
System.out.println(Integer.parseInt("100",2));
System.out.println(Integer.parseInt("100",8));
System.out.println(Integer.parseInt("100",16));
//NumberFormatException
// System.out.println(Integer.parseInt("123",2));
}
}
7.4JDK5的新特性
- JDK1.5以后,简化了定义方式
- Integer x = new Integer(4);可以直接写成。
- Integer x = 4;//自动装箱。
- x = x + 5;//自动拆箱。通过intValue方法。
- 需要注意:
- 在使用时,Integer x = null;上面的代码就会出现NullPointerException。
7.5 Character类概述及其构造方法
- Character类概述
- Character 类在对象中包装一个基本类型 char 的值
- 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然
- 构造方法
- public Character(char value)
7.6 Character类成员方法
- public static boolean isUpperCase(char ch)
- public static boolean isLowerCase(char ch)
- public static boolean isDigit(char ch)
- public static char toUpperCase(char ch)
- public static char toLowerCase(char ch)
8.正则表达式(Pattem,Matcher)
8.1 正则表达式的概述
- 正则表达式:是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
8.2 正则表达式的组成规则
- 规则字符在java.util.regex Pattern类中
- 常见组成规则
8.3 正则表达式的应用
- 判断功能
- public boolean matches(String regex)
- 分割功能
- public String[] split(String regex)
- 替换功能
- public String replaceAll(String regex,String replacement)
- 获取功能
9.Math类、Random类、System类
9.1 Math类概述
- Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
9.1 成员方法
- public static int abs(int a)
- public static double ceil(double a)
- public static double floor(double a)
- public static int max(int a,int b) min自学
- public static double pow(double a,double b)
- public static double random()
- public static int round(float a) 参数为double的自学
- public static double sqrt(double a)
9.2 Random类概述及其构造方法
9.2.1 Random类概述
- 此类用于产生随机数
- 如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
9.2.2 构造方法
- public Random()
- public Random(long seed)
9.2.3 Random成员方法
- public int nextInt()
- public int nextInt(int n)
9.3 System类概述及其成员方法
9.3.1 System类概述
- System 类包含一些有用的类字段和方法。它不能被实例化。
9.3.2 成员方法
- public static void gc()
- public static void exit(int status)
- public static long currentTimeMillis()
- public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
10.BigInteger类、BigDecimal类
10.1 BigInteger类概述
10.2 构造方法
- public BigInteger(String val)
10.3 BigInteger类成员方法
- public BigInteger add(BigInteger val)
- public BigInteger subtract(BigInteger val)
- public BigInteger multiply(BigInteger val)
- public BigInteger divide(BigInteger val)
- public BigInteger[] divideAndRemainder(BigInteger val)
10.4 BigDecimal类概述
10.5 构造方法
- public BigDecimal(String val)
10.6 BigDecimal类成员方法
- public BigDecimal add(BigDecimal augend)
- public BigDecimal subtract(BigDecimal subtrahend)
- public BigDecimal multiply(BigDecimal multiplicand)
- public BigDecimal divide(BigDecimal divisor)
- public BigDecimal divide(BigDecimal divisor,int scale, int roundingMode)
11.Date类、DateFormat类、Calendar类
11.1 Date类概述
11.2 构造方法
- public Date()
- public Date(long date)
11.3 成员方法
- public long getTime()
- public void setTime(long time)
11.4 DateFormat类概述及其方法
- DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
- 是抽象类,所以使用其子类SimpleDateFormat
- SimpleDateFormat构造方法
- public SimpleDateFormat()
- public SimpleDateFormat(String pattern)
- 成员方法
- public final String format(Date date)
- public Date parse(String source)
11.5 Calendar类的概述及其方法
- Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
- 成员方法
- public static Calendar getInstance()
- public int get(int field)
- public void add(int field,int amount)
- public final void set(int year,int month,int date)