目录
一、String、StringTokenizer节:
能用正则表达式来分离字符串,能否统计一篇短文中单词?能否分离出C程序中的单词?
1. String分割字符串
public class Stringdemo {
public static void main(String[] args) {
//stringTokenizer();
string();
}
public static void string() {
String a = "I'm LiHua. I'm from China.";
String b = a.toLowerCase();
int countw = 0;
char fch ='a';
char ch;
System.out.println("字符总数:"+b.length());
for (int i = 0; i < b.length() ; i++) {
ch=b.charAt(i);
if(!(ch>='a'&&ch<='z')&&(fch>='a'&&fch<='z'))
countw++;
fch = ch;
}
System.out.println("单词总数"+countw);
}
}
2. StringTokenizer分割字符串
- StringTokenizer可以解析分隔符不是空格的情况
- java.util.StringTokenizer
import java.util.StringTokenizer;
public class Stringdemo {
public static void main(String[] args) {
String a = "I'm LiHua. I'm from China.";
StringTokenizer st_Mark = new StringTokenizer(a);
int count = st_Mark.countTokens();
for (int i = 0; i < count; i++) {
System.out.println(st_Mark.nextToken());
}
System.out.println("----------");
/**
* 用=或;吧a分隔开来,之后把结果放在StringTokenizer类型的st_Mark中
*/
a = "name=lisa;age=23;title=singer actor";
st_Mark = new StringTokenizer(a,"=;");
count = st_Mark.countTokens();
for (int i = 0; i < count; i++) {
System.out.println(st_Mark.nextToken());
}
}
}
二、String与StringBuffer的区别?
- String是不可变类,String对象一旦被创建,其值不能被改变,也就是String对象一旦产生后就不可以被修改,重新赋值其实是两个对象。
- 而StirngBuffer是可变类,当对象被创建后,仍然可以对其值进行修改,对StringBuffer对象进行字符串的操作时,不生成新的对象。
import static java.lang.System.identityHashCode;
public class demo {
public static void main(String[] args) {
/**
* 用identityHashCode()来获得字符串的地址
* 地址不相同说明创建了新的对象,相同则说明没有创建新的对象
*/
//String
String a ="abc";
String b="edf";
System.out.println(identityHashCode(a));
a=a+b;
System.out.println(identityHashCode(a));
//StringBuffer
StringBuffer c =new StringBuffer("abc");
System.out.println(identityHashCode(c));
c.append("edf");
System.out.println(identityHashCode(c));
}
}
三、Math类、BigInteger类和Random类
1. Math类
package edu.moth10.Classes;
/*
Math类的用法通常是:Math.内容
*/
public class demo {
public static void main(String[] args) {
//private Math() {}
/*源代码中private的构造方法是私有的,故不可以用
Math m = new math();的方式来创建Math对象,提高了安全性
*/
//public static final double E = 2.7182818284590452354;
System.out.println("e="+Math.E);//自然对数的底数e的double值
// public static final double PI = 3.14159265358979323846;
System.out.println("PI="+Math.PI);//Π的值
/**
* 三角函数
* 其中Math.atan2(y,x)是求向量(x,y)与x轴夹角
*/
System.out.println("sin(0.5*Π)="+Math.sin(0.5*Math.PI));
System.out.println("cos(2Π)="+Math.cos(2*Math.PI));
System.out.println("tan(0.25*Π)="+Math.tan(0.25*Math.PI));
System.out.println("arcsin(0.5)="+Math.asin(0.5));
System.out.println("arccos(0.5)="+Math.acos(0.5));
System.out.println("arctan(1)="+Math.atan(1));
System.out.println(""+Math.atan2(1.0,0));
//求绝对值
System.out.println("|-10|="+Math.abs(10));
//开平方,开立方
System.out.println("4开根号"+Math.sqrt(4));
System.out.println("8开立方"+Math.cbrt(8));
//log
System.out.println("log10(100)="+Math.log10(100));//以10为底
System.out.println("log(e)="+Math.log(Math.E));//以e为底
System.out.println("log()="+Math.log1p(Math.E-1));//以e为底,求loglp(m)中m-1的对数值
//求sqrt(x*x+y*y)
System.out.println("sqrt(3*3+4*4)="+Math.hypot(3,4));
//指数幂值
System.out.println("e^3="+Math.exp(3));//e为底数
System.out.println("2^5"+Math.pow(2,5));//2为底数
//rint(n)求最接近n的整数值
System.out.println("最接近8.87的整数:"+Math.rint(8.87));
//nextUp(n)比n大一点的的浮点数
System.out.println("比1.0大一点点的浮点数:"+Math.nextUp(1.0));
//nextDown(n)比n小一点的的浮点数
System.out.println("比1.0小一点的的浮点数:"+Math.nextDown(1.0));
//next(n,m)返回m与n两个数之间与n十分接近的一个浮点数
System.out.println("next(1.2,0)返回1.2与0两个数之间与1.2十分接近的一个浮点数:"+Math.nextAfter(1.2,0));
//ceil(a) 返回大于a的第一个整数所对应的浮点数
System.out.println("返回大于1.32321的第一个整数所对应的浮点数"+Math.ceil(1.32321));//输出2.0
//floor(a) 返回小于a的第一个整数所对应的浮点数
System.out.println("返回大于1.567的第一个整数所对应的浮点数"+Math.floor(1.567));//输出1.0
}
}
2. BigInteger类
package edu.moth10.Classes;
import java.math.BigInteger;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
BigInteger result = new BigInteger("0"),
one = new BigInteger("123456789"),
two = new BigInteger("987654321");
result = one.add(two);
System.out.println("和:"+ result);
result = two.subtract(one);
System.out.println("差:"+result);
result = one.multiply(two);
System.out.println("积: "+result);
result = one.divide(two);
System.out.println("商:"+result);
result = one.remainder(two);
System.out.println("余:"+result);
int a = two.compareTo(one);
System.out.println("one与two比较的结果:"+a);
result =one.abs();
System.out.println("绝对值:"+result);
String result1 = one.toString();
System.out.println("one的十进制字符串:"+result1);
result1 = two.toString(4);
System.out.println("tow的四进制:"+result1);
}
}
3. Random类
导包:java.util.Random
,创建:Random r = new Ramdom
import java.util.Random;
public class demo {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 100; i++) {
int num = r.nextInt(10);//随机数的范围是0~9
System.out.printf("%d\t",num);
if((i+1)%20==0) System.out.println();
}
for (int i = 0; i < 7; i++) {
int num = r.nextInt();//随机数的范围是int类型的所有范围
System.out.printf("%d\t",num);
}
}
}
nextInt():返回下一个随机数据,范围是int所有范围
nextInt():返回下一个随机数,范围是[0,n-1)
四、Class类:
重点p210例20的反射机制创建对象
接口
package edu.moth10.Classdemo;
public interface MeiTuan {
abstract void payOnline();
}
类:ALiPay
package edu.moth10.Classdemo;
public class ALiPay implements MeiTuan {
public ALiPay() {
//super();
System.out.println("成功调用支付宝的构造器");
}
@Override
public void payOnline() {
System.out.println("选择用支付宝进行支付……");
}
}
类:MeiTuan
package edu.moth10.Classdemo;
public class Wechat implements MeiTuan{
public Wechat() {
System.out.println("成功调用微信的构造器");
}
@Override
public void payOnline() {
System.out.println("选择用微信支付……");
}
}
Test类含main
package edu.moth10.Classdemo;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//支付宝
String str = "edu.moth10.Classdemo.ALiPay";
Class c = Class.forName(str);
c.newInstance();
}
}
五、Pattern类与Matcher类
package edu.moth10.Classes;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pattern_Matcher {
public static void main(String[] args) {
String s = "市话:76.8元,长途:167.38元,短信:12.68";
String regex = "[0123456789.]+";//匹配数字序列
Pattern p = Pattern.compile(regex);//模式对象
Matcher m = p.matcher(s);
double sum = 0;
while(m.find()){
String item = m.group();
System.out.println(item);
sum = sum + Double.parseDouble(item);
}
System.out.println("账单总价格:"+sum);
}
}
六、向量类Vector:
虽然教材中没列出,但可参考数据结构案例做:A∪B,(A-B) ∪(B-A);两有序表的合并…
package edu.moth11;
import java.util.Vector;
public class Test {
public static void main(String args[])
{
Vector v=new Vector();
v.addElement("one");
System.out.println(v);
v.addElement("two");
System.out.println(v);
v.addElement("three");
System.out.println(v);
v.insertElementAt("zero",0);
System.out.println(v);;
v.insertElementAt("oop",3);
System.out.println(v);
v.setElementAt("three",3);
System.out.println(v);
v.setElementAt("four",4);
System.out.println(v);
v.removeAllElements();
System.out.println(v);
}
}
七、日期相关类
package edu.moth09;
public class CurrentTime {
public static void main(String[] args) {
//01-01-1970距离当前时间的微秒数
long totalMilliSeconds = System.currentTimeMillis();
System.out.println("总微秒数: "+ totalMilliSeconds);
//计算01-01-1970到当前的秒数
long totalSeconds = totalMilliSeconds / 1000;
System.out.println("总秒数: "+totalSeconds);
//计算当前时间的秒数
long currentSecond = totalSeconds % 60;
System.out.println("当前秒数: "+currentSecond);
//计算01-01-1970到当前时间的分钟数
long totalMinutes = totalSeconds / 60;
System.out.println("总分钟数"+ totalMinutes);
//计算当前时间的分钟数
long currentMinutes = totalMinutes % 60;
System.out.println("当前分钟数"+ currentMinutes);
//计算总时数
long totalHours = totalMinutes / 60;
System.out.println("总小时数" + totalHours);
//计算当前时数
long currentHour = (totalHours+8) % 24;//北京时间+8hours
System.out.println("当前时数:"+currentHour);
System.out.println("当前时间是: "+ currentHour +":"+ currentMinutes +":" + currentSecond);
}
}