Java EE基础班课程(第八天)
1.API使用
API(Application Programming Interface ,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。 用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节.
Java API :指的就是 JDK 中提供的各种功能的 Java 类这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用.
1.1Scanner使用
public class Demo01Scanner {
public static void main ( String [ ] args) {
InputStream xx = System . in;
Scanner sc = new Scanner ( xx) ;
System . out. println ( "请输入一个字符串:" ) ;
String s2 = sc. nextLine ( ) ;
System . out. println ( s2) ;
}
}
1.2Random使用
import java. util. Random ;
public class Demo02Random {
public static void main ( String [ ] args) {
Random random = new Random ( ) ;
for ( int i = 0 ; i < 100 ; i++ ) {
int num = random. nextInt ( ) ;
System . out. println ( num) ;
}
System . out. println ( "--------------------" ) ;
for ( int i = 0 ; i < 100 ; i++ ) {
int num = random. nextInt ( 100 ) ;
System . out. println ( num) ;
}
}
}
2.String类的学习
1. 打开API 搜索String 类
String 类代表字符串。 Java 程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
2. 查看包结构和类结构
java. lang. String
public final class String extends Object implements Serializable , Comparable < String > , CharSequence
3. 查看构造方法 学习常用的构造方法
String ( ) 初始化新创建的 String 对象,使其表示空字符序列。
String ( byte [ ] bytes) 通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。
String ( byte [ ] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
String ( char [ ] value) 分配一个新的 String ,以便它表示当前包含在字符数组参数中的字符序列。
String ( char [ ] value, int offset, int count) 分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。
String ( String original) 初始化新创建的String 对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。
4. 常用方法:
查看String 底层原码,发现 private final byte [ ] value; 说明String 底层是使用字节数组来存储数据的。
可以知道字符串有索引:索引是从0 开始到字符长度减一
char charAt ( int index) 返回 char 指定索引处的值。
int length ( ) 返回此字符串的长度。
boolean equals ( Object anObject) 将此字符串与指定对象进行比较。
String trim ( ) 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
boolean equalsIgnoreCase ( String anotherString) 将此 String 与其他 String 比较,忽略案例注意事项。
String toUpperCase ( ) 将所有在此字符 String 使用默认语言环境的规则大写。
String toLowerCase ( ) 将所有在此字符 String 使用默认语言环境的规则,以小写。
String replaceAll ( String regex, String replacement) 用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串。
String replace ( CharSequence target, CharSequence replacement) 将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。
String [ ] split ( String regex) 将此字符串分割为给定的 regular expression的匹配。
String substring ( int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
String substring ( int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。
char [ ] toCharArray ( ) 将此字符串转换为新的字符数组。
boolean isEmpty ( ) 返回 true 如果,且仅当 length ( ) 为 0 。
boolean startsWith ( String prefix) 测试此字符串是否以指定的前缀开头。
boolean endsWith ( String suffix) 测试此字符串是否以指定的后缀结尾。
2.1构造方法
public class Demo01String 构造方法 {
public static void main ( String [ ] args) {
String s1 = new String ( ) ;
System . out. println ( s1) ;
byte [ ] bytes = { 97 , 98 , 99 , 100 , 101 , 102 , 103 } ;
String s2 = new String ( bytes) ;
System . out. println ( s2) ;
String s3 = new String ( bytes, 0 , 3 ) ;
System . out. println ( s3) ;
char [ ] chars = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' } ;
String s4 = new String ( chars) ;
System . out. println ( s4) ;
String s5 = new String ( chars, 3 , 3 ) ;
System . out. println ( s5) ;
String s6 = new String ( "abcdefg" ) ;
System . out. println ( s6) ;
String s7 = "abcdefg" ;
System . out. println ( s7) ;
}
}
2.2常用方法char charAt(int index) ,int length()
public class Demo02String 常用方法 {
public static void main ( String [ ] args) {
String s1 = "abcdefg" ;
char c1 = s1. charAt ( 0 ) ;
System . out. println ( c1) ;
char c2 = s1. charAt ( 1 ) ;
System . out. println ( c2) ;
for ( int i = 0 ; i < 7 ; i++ ) {
char c = s1. charAt ( i) ;
System . out. println ( c) ;
}
int length = s1. length ( ) ;
System . out. println ( length) ;
for ( int i = 0 ; i < s1. length ( ) ; i++ ) {
char c = s1. charAt ( i) ;
System . out. println ( c) ;
}
}
}
2.3 常用方法equals
public class Demo02String 常用方法equals {
public static void main ( String [ ] args) {
String s1 = "abc" ;
String s2 = new String ( "abc" ) ;
boolean b = s1. equals ( s2) ;
System . out. println ( b) ;
int a = 10 ;
int c = 20 ;
c = 10 ;
System . out. println ( a== c) ;
System . out. println ( s1== s2) ;
}
}
2.4用户登录
import java. util. Scanner ;
public class Demo03 用户登录 {
public static void main ( String [ ] args) {
String userName = "admin" ;
String passWord = "123 456" ;
Scanner sc = new Scanner ( System . in) ;
for ( int i = 0 ; i < 3 ; i++ ) {
System . out. println ( "请输入用户名" ) ;
String user = sc. nextLine ( ) ;
System . out. println ( "请输入密码" ) ;
String pwd = sc. nextLine ( ) ;
if ( userName. equals ( user) && passWord. equals ( pwd) ) {
System . out. println ( "登录成功" ) ;
break ;
} else {
if ( 2 - i== 0 ) {
System . out. println ( "您的账户已锁定,请联系管理员!!!" ) ;
} else {
System . out. println ( "登录失败,还有" + ( 2 - i) + "次机会!!" ) ;
}
}
}
}
}
2.5用户登录案例优化版
import java. util. Scanner ;
public class Demo03 用户登录 {
public static void main ( String [ ] args) {
String userName = "admin" ;
String passWord = "123456" ;
String yanZhengMa = "Abc12D" ;
Scanner sc = new Scanner ( System . in) ;
for ( int i = 0 ; i < 3 ; i++ ) {
System . out. println ( "请输入用户名" ) ;
String user = sc. nextLine ( ) ;
String newUser = user. trim ( ) ;
System . out. println ( "请输入密码" ) ;
String pwd = sc. nextLine ( ) ;
System . out. println ( "请输入验证码Abc12D:" ) ;
String yzm = sc. nextLine ( ) ;
if ( userName. equals ( newUser) && passWord. equals ( pwd) && yanZhengMa. equalsIgnoreCase ( yzm) ) {
System . out. println ( "登录成功" ) ;
break ;
} else {
if ( 2 - i== 0 ) {
System . out. println ( "您的账户已锁定,请联系管理员!!!" ) ;
} else {
System . out. println ( "登录失败,还有" + ( 2 - i) + "次机会!!" ) ;
}
}
}
}
}
2.6 toUpperCase()和 toLowerCase()方法的使用
public class Demo03 用户登录 {
public static void main ( String [ ] args) {
String userName = "admin" ;
String passWord = "123456" ;
String yanZhengMa = "Abc12D" ;
Scanner sc = new Scanner ( System . in) ;
for ( int i = 0 ; i < 3 ; i++ ) {
System . out. println ( "请输入用户名" ) ;
String user = sc. nextLine ( ) ;
String newUser = user. trim ( ) ;
System . out. println ( "请输入密码" ) ;
String pwd = sc. nextLine ( ) ;
System . out. println ( "请输入验证码Abc12D:" ) ;
String yzm = sc. nextLine ( ) . toLowerCase ( ) ;
if ( userName. equals ( newUser) && passWord. equals ( pwd) && yanZhengMa. toLowerCase ( ) . equals ( yzm) ) {
System . out. println ( "登录成功" ) ;
break ;
} else {
if ( 2 - i== 0 ) {
System . out. println ( "您的账户已锁定,请联系管理员!!!" ) ;
} else {
System . out. println ( "登录失败,还有" + ( 2 - i) + "次机会!!" ) ;
}
}
}
}
}
2.7遍历字符串
import java. util. Scanner ;
public class Demo04 遍历字符串 {
public static void main ( String [ ] args) {
printString02 ( ) ;
}
public static void printString02 ( ) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
char [ ] chars = line. toCharArray ( ) ;
for ( int i = 0 ; i < chars. length; i++ ) {
System . out. println ( chars[ i] ) ;
}
}
public static void printString01 ( ) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
for ( int i = 0 ; i < line. length ( ) ; i++ ) {
char c = line. charAt ( i) ;
System . out. println ( c) ;
}
}
}
2.8 统计字符次数
public class Demo06 手机号屏蔽 {
public static void main ( String [ ] args) {
String phoneNum = "13917295832" ;
String lastStr = phoneNum. substring ( 7 ) ;
System . out. println ( lastStr) ;
String firstStr = phoneNum. substring ( 0 , 3 ) ;
System . out. println ( firstStr) ;
String newPhoneNum = firstStr+ "****" + lastStr;
System . out. println ( newPhoneNum) ;
}
}
2.9 敏感词替换
import java. util. Scanner ;
public class Demo07 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
String newString = line. replace ( "TMD" , "***" ) ;
System . out. println ( newString) ;
}
}
2.10字符串切割
import java. util. Arrays ;
import java. util. Scanner ;
public class Demo08 切割字符串 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入学生信息用','隔开:" ) ;
String line = sc. nextLine ( ) ;
String [ ] stuInfo = line. split ( "," ) ;
System . out. println ( Arrays . toString ( stuInfo) ) ;
Student stu = new Student ( ) ;
stu. setName ( stuInfo[ 0 ] ) ;
stu. setAge ( stuInfo[ 1 ] ) ;
System . out. println ( stu. getName ( ) ) ;
System . out. println ( stu. getAge ( ) ) ;
}
}
public class Student {
private String name;
private String age;
public Student ( ) {
}
public Student ( String name, String age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getAge ( ) {
return age;
}
public void setAge ( String age) {
this . age = age;
}
}
2.11字符串切割 封装对象 了解
public class Demo08 切割字符串 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入学生信息用','隔开:" ) ;
String line = sc. nextLine ( ) ;
String [ ] stuInfo = line. split ( "," ) ;
System . out. println ( Arrays . toString ( stuInfo) ) ;
Student stu = new Student ( ) ;
stu. setName ( stuInfo[ 0 ] ) ;
String age = stuInfo[ 1 ] ;
stu. setAge ( Integer . valueOf ( age) ) ;
System . out. println ( stu. getName ( ) ) ;
System . out. println ( stu. getAge ( ) ) ;
}
}
public class Student {
private String name;
private int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
}
2.12 String 常用方法摘要
boolean isEmpty ( ) 返回 true 如果,且仅当 length ( ) 为 0 。
boolean startsWith ( String prefix) 测试此字符串是否以指定的前缀开头。
boolean endsWith ( String suffix) 测试此字符串是否以指定的后缀结尾。
public class Demo09 {
public static void main ( String [ ] args) {
String str = "" ;
boolean b = str. isEmpty ( ) ;
System . out. println ( b) ;
str = "张三" ;
boolean b1 = str. startsWith ( "张" ) ;
System . out. println ( b1) ;
str = "100ml" ;
boolean b2 = str. endsWith ( "ml" ) ;
System . out. println ( b2) ;
}
}
3.Stringbuilder类学习
java. lang. StringBuilder
public final class StringBuilder
extends Object
implements Serializable , CharSequence
3.1学习构造方法
StringBuilder ( ) 构造一个没有字符的字符串构建器,初始容量为16 个字符。
StringBuilder ( int capacity) 构造一个没有字符的字符串构建器,由 capacity参数指定的初始容量。
StringBuilder ( String str) 构造一个初始化为指定字符串内容的字符串构建器。
public class Demo01 构造方法 {
public static void main ( String [ ] args) {
StringBuilder ss = new StringBuilder ( ) ;
System . out. println ( ss) ;
int capacity = ss. capacity ( ) ;
System . out. println ( capacity) ;
StringBuilder ss2 = new StringBuilder ( 32 ) ;
int capacity1 = ss2. capacity ( ) ;
System . out. println ( capacity1) ;
String str = "abc" ;
StringBuilder builder = new StringBuilder ( str) ;
System . out. println ( builder) ;
}
}
3.2学习常用方法
int capacity ( ) 返回当前容量。
StringBuilder append ( Object obj) 追加 Object 参数的字符串 Object 形式。
public class Demo02 {
public static void main ( String [ ] args) {
StringBuilder ss = new StringBuilder ( ) ;
StringBuilder s1 = ss. append ( 100 ) ;
StringBuilder s2 = ss. append ( 'a' ) ;
StringBuilder s3 = ss. append ( true ) ;
System . out. println ( ss) ;
System . out. println ( ss == s1) ;
System . out. println ( s2 == s1) ;
System . out. println ( s2 == s3) ;
StringBuilder sss = new StringBuilder ( ) ;
sss. append ( 100 ) . append ( 'a' ) . append ( true ) ;
System . out. println ( sss) ;
}
}
import java. util. Scanner ;
public class Demo03 {
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串" ) ;
String line = scanner. nextLine ( ) ;
String newStr = "" ;
StringBuilder builder = new StringBuilder ( line) ;
newStr = new StringBuilder ( line) . reverse ( ) . toString ( ) ;
System . out. println ( newStr) ;
}
}
3.3StringBuilder和String的区别
String :内容是不可变的
StringBuilder :内容是可变的
3.4练习
public class Demo04 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
String s = new StringBuilder ( line) . reverse ( ) . toString ( ) ;
if ( s. equals ( line) ) {
System . out. println ( "回文数" ) ;
} else {
System . out. println ( "不是回文数" ) ;
}
}
}
public class Demo05 拼接字符串 {
public static void main ( String [ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
String ss = printArray ( arr) ;
System . out. println ( ss) ;
}
public static String printArray ( int [ ] arr) {
StringBuilder str = new StringBuilder ( ) ;
str. append ( "[" ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( i == arr. length- 1 ) {
str. append ( arr[ i] ) ;
} else {
str. append ( arr[ i] ) . append ( ", " ) ;
}
}
str. append ( "]" ) ;
return str. toString ( ) ;
}
}
3.5StringBuilder 和 String 相互转化
1. StringBuilder 转换为 String
public String toString ( ) :通过 toString ( ) 就可以实现把 StringBuilder 转换为 String
2. String 转换为 StringBuilder
public StringBuilder ( String s) :通过构造方法就可以实现把 String 转换为 StringBuilder