201711671105 《Java程序设计》第八章学习总结

教材学习内容总结

1、String类

1.1常用方法:

public int length()

public boolean equals(String s)

public boolean startsWith(String s)

public int compareTo(String s)

public boolean contains(String s) //String对象调用contains方法判断当前String对象的字符序列是否包含参数s的字符序列

public int indexOf (String str)

public String substring(int startpoint)  //字符串对象调用该方法获得一个新的String对象,新的String对象的字符序列是复制当前String对象的字符序列中的startpoint位置至最后位置上的字符所得到的字符序列

public String trim()   //得到一个新的String对象,这个新的String对象的字符序列是当前String对象的字符序列去掉前后空格后的字符序列

1.2字符串向基本类型转化:

public static int parseInt(String s) throws NumberFormatException

public static byte parseByte(String s) throws NumberFormatException 

public static short parseShort(String s) throws NumberFormatException

public static long parseLong(String s) throws NumberFormatException

public static float parseFloat(String s) throws NumberFormatException

public static double parseDouble(String s) throws NumberFormatException 

1.3基本类型向字符串转化:

public static String valueOf(byte n)

public static String valueOf(int n)

public static String valueOf(long n)  

public static String valueOf(float n)

1.4正则表达式及字符串的分解:

String对象调用public boolean matches(String regex)方法可以判断当前String对象的字符序列是否和参数regex指定的正则表达式匹配。

public String[] split(String regex)使用参数指定的正则表达式regex做为分隔标记分解出其中的单词,并将分解出的单词存放在字符串数组中。

2、StringTokenizer类

构造方法:

StringTokenizer(String s)//为String对象s构造一个分析器。使用默认的分隔标记,即空格符、换行符、回车符、Tab符、进纸符做分隔标记。

StringTokenizer(String s, String delim)  //为String对象s构造一个分析器。参数delim的字符序列中的字符的任意排列被作为分隔标记。

3、Scanner类

scanner将空格做为分隔标记来解析字符序列中的单词,具体解析操作:

scanner调用next()方法依次返回NBA中的单词,如果NBA最后一个单词已被next()方法返回,scanner调用hasNext()将返回false,否则返回true;

对于被解析的字符序列中的数字型的单词,比如618,168.98等,scanner可以用nextInt()或nextDouble()方法来代替next()方法,即scanner可以调用nextInt()或nextDouble()方法将数字型单词转化为int或double数据返回;

如果单词不是数字型单词,scanner调用nextInt()或nextDouble()方法将发生InputMismatchException异常,在处理异常时可以调用next()方法返回该非数字化单词;

useDelimiter(正则表达式); 方法将一个正则表达式作为分隔标记,即和正则表达式匹配的字符串都是分隔标记。 

4、StringBuffer类

StringBuffer类的对象的实体的内存空间可以自动地改变大小,便于存放一个可变的字符序列。

常用方法:

StringBuffer append(String s)

StringBuffer append(int n)

StringBuffer append(Object o)

public chat charAt(int n )

public void setCharAt(int n ,char ch)

StringBuffer insert(int index, String str) 

public StringBuffer reverse() 

StringBuffer delete(int startIndex, int endIndex)

StringBuffer replace(int startIndex,int endIndex,String str)


5、Math、BigInteger与Random类

5.1 Math类的常用类方法:

public static long abs(double a)  //返回a的绝对值。

public static double max(double a,double b)   //返回a、b的最大值。

public static double min(double a,double b)   //返回a、b的最小值。

public static double random()    //产生一个0到1之间的随机数(不包括0和1)。

public static double pow(double a,double b)   //返回a的b次幂。

public static double sqrt(double a)   //返回a的平方根。

public static double log(double a)    // 返回a的对数。

public static double sin(double a)   //返回正弦值。public static double asin(double a)  返回反正弦值。

5.2BigInteger类的常用类方法:

public BigInteger add(BigInteger val)  // 返回当前大整数对象与参数指定的大整数对象的和。

public BigInteger subtract(BigInteger val)  //返回当前大整数对象与参数指定的大整数对象的差。

public BigInteger multiply(BigInteger val)  //返回当前大整数对象与参数指定的大整数对象的积。

public BigInteger divide(BigInteger val)  //返回当前大整数对象与参数指定的大整数对象的商。

public BigInteger remainder(BigInteger val)  //返回当前大整数对象与参数指定的大整数对象的余。

public int compareTo(BigInteger val)  // 返回当前大整数对象与参数指定的大整数的比较结果,返回值是1、-1或0,分别表示当前大整数对象大于、小于或等于参数指定的大整数。

public BigInteger pow(int a)    //返回当前大整数对象的a次幂。

public String toString()    //返回当前大整数对象十进制的字符串表示。

public String toString(int p)    //返回当前大整数对象p进制的字符串表示。

5.3Random类的常用方法:

构造方法:

Random random=new Random();

random.nextInt();返回一个0至n之间(包括0,但不包括n)的随机数 

6、Class类与Console类

Class类:

使用Class的类方法得到一个和某类(参数className指定的类)相关的Class对象:

public static Class forName(String className) throws ClassNotFoundException

上述方法返回一个和参数className指定的类相关的Class对象。如果类在某个包中,className必须带有包名,例如, className="java.util.Date"。

Console类:

如果希望在键盘输入一行文本,但不想让该文本回显,即不在命令行显示,那么就需要使用java.io包中的Console类的对象来完成。首先使用System类调用console()方法返回一个Console类的一个对象,比如:

Console cons = System.console();

然后,cons调用readPassword()方法读取用户在键盘输入的一行文本,并将文本以一个char数组返回:

char[] passwd = cons.readPassword();

7、Pattern与Match类

建立模式对象:

使用正则表达式regex做参数得到一个称为模式的Pattern类的实例pattern:

例如:String regex = "good";

           Pattern pattern = Pattern.compile(regex); 

模式对象是对正则表达式的封装。Pattern类调用类方法compile(String regex)返回一个模式对象,其中的参数regex是一个正则表达式, 称为模式对象使用的模式。

得到匹配对象:

模式对象pattern调用matcher(CharSequence input)方法返回一个Matcher对象matcher,称为匹配对象             

Matcher matcher = pattern.matcher(input); 

Matcher对象matcher可以使用下列方法寻找字符串input中是否有和模式regex匹配的子序(regex是创建模式对象pattern时使用的正则表达式)。

public boolean find():寻找input和regex匹配的下一子序列,如果成功该方法返回true,否则返回false。

public boolean matches():matcher调用该方法判断input是否完全和regex匹配。

public boolean lookingAt():matcher调用该方法判断从input的开始位置是否有和regex匹配的子序列。

public boolean find(int start) : matcher调用该方法判断input从参数start指定位置开始是否有和regex匹配的子序列 。

public String replaceAll(String replacement) matcher调用该方法可以返回一个字符串,该字符串是通过把input中与模式regex匹配的子字符串全部替换为参数replacement指定的字符串得到的.

public String replaceFirst(String replacement) matcher调用该方法可以返回一个字符串,该字符串是通过把input中第1个与模式regex匹配的子字符串替换为参数replacement指定的字符串得到的。
 

猜你喜欢

转载自blog.csdn.net/zhifengdeng/article/details/84895951