20175305张天钰《java程序设计》第七周学习总结

隐身守侯 提交于 2020-02-18 07:35:31

《java程序设计》第七周学习总结


第八章 常用实用类

1.String类

1.String类不可以有子类。
2.用户无法输出String对象的引用,输出的是字符序列
3.构造方法:String s = new String("We are students");
4.其他构造方法:String (char a[ ]) 和 String(char a[],int startIndex,int count)

2.字符串的并置

*1.String对象可以用“+”进行并置运算,即首尾相接得到一个新的String对象。

3.String类的常用方法

1.public int length( ) 用来获取一个String对象的字符序列的长度;
2.public boolean equals(String s) 用来比较当前String对象的字符序列是否与参数s指定的String对象的字符序列相同;
3.public boolean startsWith(String s)、public boolean endsWith(Sting s) 判断当前String对象的字符序列前缀是否是参数指定的String对象s的字符序列;
4.public boolean contains(String s) 用来判断当前String对象的字符序列是否包含参数s的字符序列;
5.public int indexOf (string s) 从当前String对象的字符序列的0索引位置开始检索首次出现s的字符序列位置并返回该位置,若没检索到,该方法的返回值为-1;
6.public int lastIndexOf(String s) 从当前String对象的字符序列的0索引位置开始检索最后一次出现s的字符序列的位置,并返回该位置,若没检索到,则返回-1;
*7.public String trim() 得到一个新的String对象,这个String对象的字符序列是当前String对象的字符序列去掉前后空格后的字符序列。

4.字符串与字符、字节数组

1.字符串与字符数组的方法:getChars//复制到参数c指定的数组中和public char[] toCharArray()
2.字符串与字节数组的方法:
String(byte[],int offset,int length)
public byte[] getBytes()
public byte[] getBytes(String charsetName)
*3.字符串的加密算法:P186

5.正则表达式:

*正则表达式是一个String对象的字符序列,该字符序列中含有具有特殊意义的字符,这些特殊字符称作正则表达式的元字符

元字符:

6.常用使用类

1)StringTokenizer类

1.StringTokenizer(String s)
2.StringTokenizer(String s, String delim)

2)Scanner类

1.next()方法用来一次返回被解析的字符序列中的单词
2.如果含有数字可以用nextInt()和nextDouble()来代替next()方法。

3)StringBuffer类

1.length():获取实体中存放的字符序列的长度。
2.capacity():获取当前实体的实际容量。
3.StringBuffer append(String s):将String对象s的字符序列追加到当前StringBuffer对象的字符序列中,并返回当前StringBuffer对象的引用。
4、public char charAt(int n)和public void setCharAt(int n,char ch):得到序列位置n上的字符和替换ch指定的字符。
5、StringBuffer insert(int index,String str):将参数str指定的字符序列插入到参数index指定的位置。
6、public StringBuffer reverse():将对象实体中的字符序列翻转。
7、StringBuffer delete(int startIndex,int endIndex):从当前StringBuffer对象的字符序列中删除一个子字符序列,并返回当前对象的引用。
8、StringBuffer replace(int startIndex,int endIndex,String str):将当前StringBuffer对象的字符序列的一个子字符序列用参数str指定的字符序列替换。

4)Date类与Calendar类

1.使用无参数构造方法:Date()
2.使用带参数的构造方法:Date(long time)
3.使用Calendar类的static方法getInstance()可以初始化一个日历对象:Calendar calendar = Calendar.getInstance()
Date date = new Date();
long dt=315532800;//十年前 单位是ms 1000ms=1s
date.setTime(System.currentTimeMillis()-dt
1000);
/formate方法/
/* %ty 年 %tm 月 %td 日 /
/
 %tY 4位年 */
System.out.println(String.format("%tY-%<tm-%<td",date));
System.out.println(String.format("%tm/%<td %<tY",date));
System.out.print(String.format(Locale.CHINA,"%tY年%<tm月%<td日 %<ta",date));

5)日期的格式化

1.format方法:format(格式化模式,日期列表)
2.format(Locale locale,格式化模式,日期列表);

6)Math类:用于静态方法、常量E和PI

1.public static long abs(double a)
2.public static double max(double a,double b)
*3.public static double min(double a,double b)

7)BigInteger类:用于处理特别大的整数

1.public BigInteger add(BigInteger val)
2.public BigInteger subtract(BigInteger val)
*3.public BigInteger multiply(BigInteger val)

8)Random类:用于产生随机数

7.托管代码

public static void main(String []args){
String mess="姓名:张三 出生时间:1989.10.16.个人网站:http://www.zhang.com 身高:185cm,体重:72kg";
int index= mess.indexOf(":"); //返回字符串中首次出现冒号的位置
String name=mess.substring(index+1);
if(name.startsWith("张")) System.out.println("简历中的姓名姓张");
index= mess.indexOf(":",index+1); //返回字符串中第2次出现冒号的位置
String date=mess.substring(index+1,index+11);
System.out.println(date);
index=mess.indexOf(":",index+1);
int heightPosition= mess.indexOf("身",index+1);//返回字符串中首次出现身高的位置
String personNet=mess.substring(index+1,heightPosition-1);
System.out.println(personNet);
index=mess.indexOf(":",heightPosition);//返回字符串中身高后面的冒号位置
int cmposition=mess.indexOf("cm");
String height=mess.substring(index+1,cmposition);
height=height.trim();
int h=Integer.parseInt(height);
if(h>=180){
System.out.println("简历中的身高"+height+"大于或等于180cm");
}
else {
System.out.println("简历中的身高"+height+"小于180cm");
}
index=mess.lastIndexOf(":");//返回字符串中最后一个冒号的位置
int kgPosition=mess.indexOf("kg");//转化为整形
String weight=mess.substring(index+1,kgPosition);
weight=weight.trim();
int w=Integer.parseInt(weight);
if(w>=75){
System.out.println("简历中的体重"+weight+"大于或等于75kg");
}
else{
System.out.println("简历中的体重"+weight+"小于75kg");
}
}

总结:本章所学的常用实用类感觉非常有用,使得java编程更加快捷方便,并且可以理解很多代码并学习
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!