写在开头:今天继续给大家分享关于JAVA的一些基础实用类。
学习内容安排
JAVA基础课程学习:数据类型(一)、运算符表达式和程序结构控制(二)、面向对象基础:类与对象和接口(三)、面向对象基础:继承抽象多态封装(四)、异常类和常用实用类(五)、组件和事件处理(六)、IO和JDBC(七)、泛型和集合函数(八)。
五、异常类和常用实用类
今天主要分享的类别有StringBuffer、Date、Calendar、Math、BigInteger、Random,下面我们结合例子分别对每个类别的日常用法进行分享。
4.其他常用类
4.1StringBuffer
经常我们需要对String字符串进行增删改的时候都会感觉会挺麻烦的,不太方便,但如果使用StringBuffer类的话,就可以直接调用方法进行操作。一般的操作有append增加字符串,insert插入、reverse转置、delete相关删除、repalce替换。我们会在程序中解释具体的类的使用。
先来展示append和单字符提取charAt的方法,
package test4;
public class 2_1 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("明曦君");
//StringBuffer append(String s)\(int n)\(Object o)...
System.out.println("1.append添加字符串");
s.append("加油!");
System.out.println(s);
//public chat charAt(int n); public voud setCharAt(int n, char ch)
System.out.println(" ");
System.out.println("2.charAt提取某个位子上得单个字符并替换该字符");
char c = s.charAt(0);
System.out.println(c);
s.setCharAt(0, '晨'); //char类型需要使用单引号
System.out.println(s);
}
}
run:
1.append添加字符串
明曦君加油!
2.charAt提取某个位子上得单个字符并替换该字符
明
晨曦君加油!
然后我们来介绍一下insert、reverse以及delete和replace方法,
package test4;
public class 2_1 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("明曦君");
System.out.println("3.插入字符串");
s.insert(2, "GoGo");
System.out.println(s);
//public StringBuffer reverse()
System.out.println(" ");
System.out.println("4.将字符串进行翻转");
s.reverse();
System.out.println(s);
//StringBuffer delete(int startIndex, int endIndex)
System.out.println(" ");
System.out.println("5.对字符串进行删除");
s.delete(1, 5); //不包括右边界
System.out.println(s);
s.deleteCharAt(0);//删除第一个字符
System.out.println(s);
//StringBuffer replace(int startIndex, int endIndex, String str)
System.out.println(" ");
System.out.println("6.对字符串进行替换");
s.replace(0, 2, "明曦");
System.out.println(s);
}
}
run:
3.插入字符串
明曦GoGo君
4.将字符串进行翻转
君oGoG曦明
5.对字符串进行删除
君曦明
曦明
6.对字符串进行替换
明曦
当然如果有时候就是想用String类型,但又想进行操作,我们可以先对String转换为StringBuffer然后再转换回String。
package test4;
public class 2_1 {
public static void main(String[] args) {
String ss = "明曦君";
StringBuffer sss = new StringBuffer(ss);
sss.reverse();
ss = sss.toString();
System.out.println(ss);
}
}
run:
君曦明
以上就是对StringBuffer的一些方法的展示,下面我们来介绍一些其他类别。
4.2Date与Calendar类
Date()的方法可以获取本地当前的时间,返回的就是一串时间,而当我们想去了解这个时间关于日历的内容,比如今天是第几周,第几个月这种信息,那我们就需要结合Calendar类进行操作,
package test4;
import java.util.Calendar;
import java.util.Date;
public class 2_1 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
Date d = new Date();
System.out.println(d);
c.setTime(d);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int dayYear = c.get(Calendar.DAY_OF_YEAR);
System.out.println("现在是多少年? "+year);
System.out.println("现在是第几个月? "+month);
System.out.println("现在是一年的第几天? "+dayYear);
}
}
Sun Mar 15 11:22:29 CST 2020
现在是多少年? 2020
现在是第几个月? 2
现在是一年的第几天? 75
这里需要注意的是对于月份的输出,虽然现在是3月但输出的是2,这是因为这里使用的是罗马教皇日历是从0到11的,所以我们在输出时需要加1。
4.3Math类
Math类在java.lang包中,其中包括很多科学计算的类方法,可以直接通过类名进行调用,还有两个静态常量一个是E一个是PI,我们对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)之间的随机数 |
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) | 返回反正弦值 |
下面我们来展示一下各个函数的使用例子,
package test4;
public class 2_1 {
public static void main(String[] args) {
System.out.println(Math.E); //返回E
System.out.println(Math.PI); //返回pi
int a = -1, b = 2;
System.out.println(Math.abs(a)); //返回a的绝对值
System.out.println(Math.max(a, b)); //返回a和b的最大值
System.out.println(Math.min(a, b)); //返回a和b的最小值
double random = Math.random();
System.out.println(random); //返回0-1的随机数
System.out.println(Math.pow(a, b)); //返回a的b次方
System.out.println(Math.sqrt(b)); //返回b的平方根
System.out.println(Math.log(b)); //返回b的对数
System.out.println(Math.sin(Math.PI/2));//返回pi/2的sin值"+dayYear);
}
}
run:
2.718281828459045
3.141592653589793
1
2
-1
0.5653616285177047
1.0
1.4142135623730951
0.6931471805599453
1.0
4.4BigInteger类
当遇到很多维的数值时,我们的基础类型long这些都不管用,甚至是Long类也不管用的时候就可以调用BigInteger,这个是java.math里面的类提供任意精度的整数运算。方然运用BigInteger类了之后就不能直接使用加减乘除来对进行运算了,需要使用方法来进行操作。使用public BigInreger(String val)来创建一个大整数,
类名 | 返回 |
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 BigInteger pow(int a) | 返回当前大整数的a次幂 |
public int compareTo(BigInteger val) | 比较大小,返回1,-1,0 |
public String toString() | 十进制字符串表示 |
public String toString(int p) | p进制字符串表示 |
我们通过计算一个实例来展示我们的代码,我们计算6的二次方除以3然后再加上5的结果与18比大小,
package test4;
import java.math.BigInteger;
public class 2_1 {
public static void main(String[] args) {
BigInteger k = new BigInteger("6");
BigInteger c = new BigInteger("3");
BigInteger d = new BigInteger("5");
BigInteger f = new BigInteger("18");
System.out.println("计算k的2次方除以c加d并与f比大小");
System.out.println(k.pow(2).divide(c).add(d).compareTo(f));
}
}
run:
计算k的2次方除以c加d并与f比大小
-1
4.5Random类
在Math类中我们已经讲了可以使用math.random()的方法来调取(0,1)的随机数,那么JAVA提供了一个更加灵活的获得随机数的办法就是Random类,我们可以使用public Random()或public Radnom(long seed)来创建Random对象,然后调用nextInt()或者其他方法来生成随机数,下面给几个例子。
这里需要注意的是我们可以使用nextBytes来使得byte数组中的值填充随机数,这种方法可以快速构造一个小的随机数组。
package test4;
import java.util.Random;
public class 2_1 {
public static void main(String[] args) {
Random dd = new Random();
int x = dd.nextInt(100);//0到100
System.out.println(x);
Boolean f = dd.nextBoolean();
System.out.println(f);//随机生成布尔值
byte c[] = new byte[10];//使用byte就是可以随机替换byte数组里的值
dd.nextBytes(c);
for(byte b: c){
System.out.print(b);
}
}
}
run:
88
true
76-60-1920-3875-28883-75
结语
以上就是JAVA对于字符串处理的常见类,已经一些比较实用类的介绍通过这些类的使用可以大大提高我们编程的效率。
谢谢阅读。