Java常用类
1.1 Arrays
Arrays类能方便地操作数组,它提供的所有方法都是静态的。具有以下功能。
给数组赋值:通过fill方法。 对数组排序:通过sort方法,按升序。 比较数组:通过equals方法比较数组中元素值是否相等。
查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找法操作。
public class TestArrays {
public static void output(int[] array) {
if (array!=null) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
System.out.println();
}
public static void main(String[] args) {
int[] array = new int[5];
//填充数组
Arrays.fill(array, 5);
System.out.println("填充数组:Arrays.fill(array, 5):");
TestArrays.output(array);
//将数组的第2和第3个元素赋值为8
Arrays.fill(array, 2, 4, 8);
System.out.println("将数组的第2和第3个元素赋值为8:Arrays.fill(array, 2, 4, 8):");
TestArrays.output(array);
int[] array1 = {7,8,3,2,12,6,3,5,4};
//对数组的第2个到第6个进行排序进行排序
Arrays.sort(array1,2,7);
System.out.println("对数组的第2个到第6个元素进行排序进行排序:Arrays.sort(array,2,7):");
TestArrays.output(array1);
//对整个数组进行排序
Arrays.sort(array1);
System.out.println("对整个数组进行排序:Arrays.sort(array1):");
TestArrays.output(array1);
//比较数组元素是否相等
System.out.println("比较数组元素是否相等:Arrays.equals(array, array1):"+"\n"+Arrays.equals(array, array1));
int[] array2 = array1.clone();
System.out.println("克隆后数组元素是否相等:Arrays.equals(array1, array2):"+"\n"+Arrays.equals(array1, array2));
//使用二分搜索算法查找指定元素所在的下标(必须是排序好的,否则结果不正确)
Arrays.sort(array1);
System.out.println("元素3在array1中的位置:Arrays.binarySearch(array1, 3):"+"\n"+Arrays.binarySearch(array1, 3));
//如果不存在就返回负数
System.out.println("元素9在array1中的位置:Arrays.binarySearch(array1, 9):"+"\n"+Arrays.binarySearch(array1, 9));
}
}
1.2 正则表达式
1.正则表达式的作用
2.正则表达式的匹配模式
3.patten类和matcher类的使用
4.string对正则的支持
正则表达式可以方便的对数据进行匹配,可以整形更加复杂的字符串验证、拆分、替换的功能。
例子:要判断一个字符是否是数字组成,则可以使用下面方法:
1.不使用正则表达式 :regulartest例子
2.使用正则表达式
不使用的时候:思路就是先将字符串拆分,之后一个一个的进行比较验证,但是这样比较麻烦,现在这样只是做了一个简单那的验证是否为数字组成,如果更加复杂就使用正则表达式。
public static void main(String[] args) {
String string="1234567890";
boolean flag=true;
char c[]=string.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i]<'0'||c[i]>'9') {
flag=false;
break;
}
}
if (flag) {
System.out.println("这些数字都是数字!!!");
}else {
System.out.println("这些数字里面存在不是数字的东西!!!");
}
}
}
使用正则表达式:方式更加简单 Java1.4后引入
String str="1234567890";
// 这里就使用了正则表达式
if (Pattern.compile("[0-9]+").matcher(str).matches()) {
System.out.println("这些数字都是数字!!!");
}else {
System.out.println("这些数字里面存在不是数字的东西!!!");
}
Pattern 和matcher类 他们是在正则里边最核心的东西。他们都是Java.unit.regex包中。
如果想在程序中应用正则表达式,就要依靠Pattern 和matcher类,这两个类都是Java.unit.regex包中,
Pattern类主要的作用是进行正则规范【“0-9”】,
\d:表示数字,[0-9] \D:表示非数组,[^0-9] \w:表示字母、数字、下划线,[a- zA- Z0-9] \W: 表示[^a-
zA- Z0-9]{5-10}
这些正则,要是像用起来就得使用pattern和matcher
Pattern类主要的作用是进行正则规范【“0-9”】,matcher主要是执行规范,验证字符串是否符合规范。
在这个类中想要取得pattern类的实例,就得调用compile()方法
例子:
String str="2014-11-23";
if (Pattern.compile("\\d{4}-\\d{2}-\\d{2}").matcher(str).matches()){
System.out.println("你输入的内容符合要求");
} else {
System.out.println("你输入的内容不符合要求!!!");
}
拆分字符串
拆分字符串
String st1="a1b c3d4f5g6h7j8k9";
String p2="\\d+";//定义的规则
Pattern pat=Pattern.compile(p2);//实例化pattern对象
String a[]=pat.split(st1);//就是把数组拆分出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
还可以使用matcher里面的字符串替换
字符串替换
String st1="a1b2c3d4f5g6h7j8k9";
String p2="\\d+";//定义的规则
Pattern pat=Pattern.compile(p2);//实例化pattern对象
Matcher matcher=pat.matcher(st1);
String p3=matcher.replaceAll("***");
System.out.println(p3);
只要使用正则的验证规则,就会匹配复杂的字符串
String类对正则的支持:
从之前的操作中,很多代码都是重复的。Java从1.4后就对正则进行了扩充,string开始支持正则。
// String的正则表达式
String st1="a1s2d3f4g5h6j7".replaceAll("\\d+", "**");
boolean bemp="2014-11-23".matches("\\d{4}-\\d{2}-\\d{2}");
String a[]="1234dferts3".split("\\d+");
System.out.println(st1);
System.out.println(bemp);
for (int i = 0; i < a.length; i++) {
System.out.println("拆分:"+a[i]);
}
使用正则表达式的时候需要注意的。就像里面有出现了特殊字符和符号。使用转义字符”\\”,转义的时候两个“\“代表一个。
String st1="黄富春:98|王二小:90|小鸭子:100";
String a[]=st1.split("\\|");
for (int i = 0; i < a.length; i++) {
String st2[]=a[i].split(":");
// System.out.print(a[i]+"\t");
System.out.println(st2[0]+"\t"+st2[1]);
//
}
16.3 NumberFormat
NumberFormat 表示数字的格式化类, 即:可以按照本地的风格习惯进行数字的显示。
MessageFormat 、DateFormat 、NumberFormat 是 Format 三个常用的子类,如果要想进一步完成一个好的国际化程序,则肯定需要同时使用这样三个类完成,根据不同的国家显示贷币的形式。
此类还是在java.text 包中,所以直接导入此包即可。
public static void main(String args[]){
NumberFormat nf = null ; // 声明一个NumberFormat对象
nf = NumberFormat.getInstance() ; // 得到默认的数字格式化显示
System.out.println("格式化之后的数字:" + nf.format(10000000)) ;
}
DecimalFormat 的基本使用
是NumberFormat 类的子类,主要的作用是用来格式化数字使用,当然,在格式化数字的时候要比直接使用NumberFormat 更加方便,因为可以直接指定按用户自定义方式进行格式化操作,与之前讲的SimpleDateFormat类似,如果要想进行自定义格式化操作,则必须指定格式化操作的模板。
class FormatDemo{
public void format1(String pattern,double value){ // 此方法专门用于完成数字的格式化显示
DecimalFormat df = null ; // 声明一个DecimalFormat类的对象
df = new DecimalFormat(pattern) ; // 实例化对象,传入模板
String str = df.format(value) ; // 格式化数字
System.out.println("使用" + pattern
+ "格式化数字" + value + ":" + str) ;
}
};
public class NumberFormatDemo{
public static void main(String args[]){
FormatDemo demo = new FormatDemo() ; // 格式化对象的类
demo.format1("###,###.###",111222.34567) ;
demo.format1("000,000.000",11222.34567) ;
demo.format1("###,###.###¥",111222.34567) ;
demo.format1("000,000.000¥",11222.34567) ;
demo.format1("##.###%",0.345678) ;
demo.format1("00.###%",0.0345678) ;
demo.format1("###.###\u2030",0.345678) ;
}
};
16.4 Math和random类
Math类 表示数字操作,:平方,四舍五入 Math.PI 记录的圆周率 Math.E 记录e的常量
Math中还有一些类似的常量,都是一些工程数学常用量。 Math.abs 求绝对值 Math.sin 正弦函数;Math.asin
反正弦函数 Math.cos 余弦函数;Math.acos 反余弦函数 Math.tan 正切函数;Math.atan
反正切函数;Math.atan2 商的反正切函数 Math.toDegrees 弧度转化为角度;Math.toRadians 角度转化为弧度
Math.ceil 得到不小于某数的最大整数 Math.floor 得到不大于某数的最大整数 Math.IEEEremainder 求余
Math.max 求两数中最大 Math.min 求两数中最小 Math.sqrt 求开方 Math.pow 求某数的任意次方,
抛出ArithmeticException处理溢出异常 Math.exp 求e的任意次方 Math.log10 以10为底的对数
Math.log 自然对数 Math.rint 求距离某数最近的整数(可能比某数大,也可能比它小) Math.round
//四舍五入法最接近该参数的int/long值. Math.random 返回0,1之间的一个随机数
public class MathDemo {
public static void main(String args[]){
/**
* abs求绝对值
*/
System.out.println(Math.abs(-10.4)); //10.4
System.out.println(Math.abs(10.1)); //10.1
/**
* ceil天花板的意思,就是返回大的值,注意一些特殊值
*/
System.out.println(Math.ceil(-10.1)); //-10.0
System.out.println(Math.ceil(10.7)); //11.0
System.out.println(Math.ceil(-0.7)); //-0.0
System.out.println(Math.ceil(0.0)); //0.0
System.out.println(Math.ceil(-0.0)); //-0.0
/**
* floor地板的意思,就是返回小的值
*/
System.out.println(Math.floor(-10.1)); //-11.0
System.out.println(Math.floor(10.7)); //10.0
System.out.println(Math.floor(-0.7)); //-1.0
System.out.println(Math.floor(0.0)); //0.0
System.out.println(Math.floor(-0.0)); //-0.0
/**
* max 两个中返回大的值,min和它相反,就不举例了
*/
System.out.println(Math.max(-10.1, -10)); //-10.0
System.out.println(Math.max(10.7, 10)); //10.7
System.out.println(Math.max(0.0, -0.0)); //0.0
/**
* random 取得一个大于或者等于0.0小于不等于1.0的随机数
*/
System.out.println(Math.random()); //0.08417657924317234
System.out.println(Math.random()); //0.43527904004403717
/**
* rint 四舍五入,返回double值
* 注意.5的时候会取偶数
*/
System.out.println(Math.rint(10.1)); //10.0
System.out.println(Math.rint(10.7)); //11.0
System.out.println(Math.rint(11.5)); //12.0
System.out.println(Math.rint(10.5)); //10.0
System.out.println(Math.rint(10.51)); //11.0
System.out.println(Math.rint(-10.5)); //-10.0
System.out.println(Math.rint(-11.5)); //-12.0
System.out.println(Math.rint(-10.51)); //-11.0
System.out.println(Math.rint(-10.6)); //-11.0
System.out.println(Math.rint(-10.2)); //-10.0
/**
* round 四舍五入,float时返回int值,double时返回long值
*/
System.out.println(Math.round(10.1)); //10
System.out.println(Math.round(10.7)); //11
System.out.println(Math.round(10.5)); //11
System.out.println(Math.round(10.51)); //11
System.out.println(Math.round(-10.5)); //-10
System.out.println(Math.round(-10.51)); //-11
System.out.println(Math.round(-10.6)); //-11
System.out.println(Math.round(-10.2)); //-10
}
}
Radom随机数
调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范围是[0.0,1.0)的左闭右开区间,返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。
// 案例1
System.out.println("Math.random()=" + Math.random());// 结果是个double类型的值,区间为[0.0,1.0)
int num = (int) (Math.random() * 3); // 注意不要写成(int)Math.random()*3,这个结果为0,因为先执行了强制转换
System.out.println("num=" + num);
下面是Java.util.Random()方法摘要:
1.protected int next(int bits):生成下一个伪随机数。
2.boolean nextBoolean():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的boolean值。
3.void nextBytes(byte[] bytes):生成随机字节并将其置于用户提供的 byte 数组中。
4.double nextDouble():返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布的 double值。
5.float nextFloat():返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布float值。
6.double nextGaussian():返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的double值,其平均值是0.0标准差是1.0。
7.int nextInt():返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
8.int nextInt(int n):返回一个伪随机数,它是取自此随机数生成器序列的、在(包括和指定值(不包括)之间均匀分布的int值。
9.long nextLong():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。
10.void setSeed(long seed):使用单个 long 种子设置此随机数生成器的种子。
下面给几个例子:
1.生成[0,1.0)区间的小数:double d1 = r.nextDouble();
2.生成[0,5.0)区间的小数:double d2 = r.nextDouble() * 5;
3.生成[1,2.5)区间的小数:double d3 = r.nextDouble() * 1.5 + 1;
4.生成-231到231-1之间的整数:int n = r.nextInt();
5.生成[0,10)区间的整数:
int n2 = r.nextInt(10);//方法一
n2 = Math.abs(r.nextInt() % 10);//方法二