您好,欢迎访问一九零五行业门户网

JavaSE常用类及方法的介绍(附代码)

本篇文章给大家带来的内容是关于javase常用类及方法的介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、基本数据类型比较用:==
2、引用数据类型比较用:equals方法
如果引用数据类型使用==比较的话,比较的是地址值
tostring类
对象调用tostring()需要重写本方法: 在封装类中,否则输出的是地址
equals方法
'对象' 调用equals()需要重写本方法: 在封装类中重写,否则进行比较时比较的是地址
string类
string有一个切割split,按一个字符串进行切割,返回切割之后的字符串数组
string[] split(string regex)
public int length () :返回此字符串的长度。
public string concat (string str) :将指定的字符串连接到该字符串的末尾。
public char charat (int index) :返回指定索引处的 char值。
public int indexof (string str) :返回指定子字符串第一次出现在该字符串内的索引。
public int indexof(string str, int fromindex) :返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public string substring (int beginindex) :返回一个子字符串,从beginindex开始截取字符串到字符串结尾。
public string substring (int beginindex, int endindex) :返回一个子字符串,从beginindex到endindex截取字符串。含beginindex,不含endindex。
public string replace (charsequence target, charsequence replacement) :将与target匹配的字符串使用replacement字符串替换。 
stringbuilder类
string builder在内存中相当于一个缓冲容器,会随着内存的关闭而消失,在地址内存中进行字符拼接时 不创建所添加字符的内存地址 节省了内存空间
stringbuilder() 构造一个没有字符的字符串构建器,初始容量为16个字符。
stringbuilder(string str) 构造一个初始化为指定字符串内容的字符串构建器
stringbuilder sb = new stringbuilder();
public stringbuilder append(任意类型):添加数据,并返回对象本身(支持链式调用)。
public stringbuilder reverse():字符序列进行反转
public string tostring():返回此序列中数据的字符串表示形式。 转为string
append方法弊端:它可以拼接任意类型,但是拼接完毕,都变成字符串
arrays类
public static string tostring(int[] a):把数组转成字符串
public static void sort(int[] a):对数组进行升序排序
包装类与string类互相转换
int 类型直接拼接字符串可转为string类型
int->string
1+
string.valueof()方法可以将基本类型数据转为string类型
string.valueof(数据);
包装类.parsexxx方法可以将基本类型转为string类型  注意基本类型必须转为相对应的包装类,以下是int转string例子
int->string(重点)
integer.parseint(100)
date类
在java,有一个java.util.date,表示日期时间的,精确到毫秒值
date类的构造方法:
date() 无参构造方法:以当前的系统时间来创建出一个date对象
date(long date):根据指定的毫秒值,创建出一个date对象。 指定的毫秒值,从1970年1月1日(计算机的基准时间)起经过的毫秒值
常用方法:
public long gettime() 把日期对象转换成对应的时间毫秒值。
void settime(long time) 将此 date对象设置为1970年1月1日00:00:00 起经过的毫秒值
//请打印出1970年1月2号的时间的对象date date2 = new date(24 * 60 * 60 * 1000);system.out.println(date2);//获取当前时间的毫秒值date date = new date();system.out.println(date.gettime());//将date,改成1970年1,月1号 date.settime(0);system.out.println(date);
simpledateformat类
可以使用dateformat类,但是它是一个抽象类,所以我们要用它的子类 simpledateformat构造方法
simpledateformat(string pattern) 使用给定模式构建一个 simpledateformat ,默认日期格式符号为默认的 format区域设置。
参数pattern就是模式
字母模式:y表示面 m表示月 d表示日 h表示时 m表示分 s表示秒 s表示毫秒
中国式时间: 2019年3月11日 11点 09分 33秒 333毫秒
代码的字母模式: yyyy年mm月dd日 hh点mm分ss秒 sss毫秒
成员方法 :
格式化(日期 -> 文本): date -- string
public final string format(date date)
解析(文本 -> 日期): string -- date
public date parse(string source)
//根据系统时间创建date对象 date date = new date(); system.out.println(date); //date不好看,格式化为中国式时间 //simpledateformat sdf = new simpledateformat("yyyy年mm月dd日 hh点mm分ss秒 sss毫秒"); simpledateformat sdf = new simpledateformat("yyyy年mm-dd hh:mm:ss"); //将date格式化为string string time = sdf.format(date); system.out.println(time); //注意,我们一般人不会记忆毫秒值,能不能根据具体的时间(2019-03-11 11:16:02)解析成毫秒值 //parseexception: unparseable date: "2018年03-11 11:18:57",注意,模式必须与之前一致 date date1 = sdf.parse("2018年03-11 11:18:57"); system.out.println(date1); system.out.println(date1.gettime());
calendar类
calendar为抽象类,由于语言敏感性,calendar类在创建对象时并非直接创建,而是通过静态方法创建,返回子类对象
根据calendar类的api文档,常用方法有:
public int get(int field):返回给定日历字段的值。
public void set(int field, int value):将给定的日历字段设置为给定值。
public abstract void add(int field, int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量。
public date gettime():返回一个表示此calendar时间值(从历元到现在的毫秒偏移量)的date对象。
calendar类中提供很多成员常量,代表给定的日历字段:
字段值
含义
year

month
月(从0开始,可以+1使用)
day_of_month
月中的天(几号)
hour
时(12小时制)
hour_of_day
时(24小时制)
minute

second

day_of_week
周中的天(周几,周日为1,可以-1使用)
import java.util.calendar;public class calendarutil { public static void main(string[] args) { //get方法 // 创建calendar对象 calendar cal = calendar.getinstance(); // 设置年 int year = cal.get(calendar.year); // 设置月 int month = cal.get(calendar.month) + 1; // 设置日 int dayofmonth = cal.get(calendar.day_of_month); //set方法 calendar cal = calendar.getinstance(); cal.set(calendar.year, 2020); //add方法 cal.add(calendar.day_of_month, 2); // 加2天 cal.add(calendar.year, -3); // 减3年 //gettime方法 date date = cal.gettime(); } }
system类
public static long currenttimemillis():返回以毫秒为单位的当前时间。
public static void arraycopy(object src, int srcpos, object dest, int destpos, int length):将数组中指定的数据拷贝到另一个数组中。
currenttimemillis方法
import java.util.date;public class systemdemo { public static void main(string[] args) { //获取当前时间毫秒值 system.out.println(system.currenttimemillis()); // 1516090531144 }}
arraycopy方法
参数序号
参数名称
参数类型
参数含义
1
src
object
源数组
2
srcpos
int
源数组索引起始位置
3
dest
object
目标数组
4
destpos
int
目标数组索引起始位置
5
length
int
复制元素个数
import java.util.arrays;public class demo11systemarraycopy { public static void main(string[] args) { int[] src = new int[]{1,2,3,4,5}; int[] dest = new int[]{6,7,8,9,10}; system.arraycopy( src, 0, dest, 0, 3); /*代码运行后:两个数组中的元素发生了变化 src数组元素[1,2,3,4,5] dest数组元素[1,2,3,9,10] */ }}
random类
构造方法:
random() 创建一个新的随机数生成器。
成员方法 :
int nextint() 从这个随机数生成器的序列返回下一个伪随机数,均匀分布的 int值。
int nextint(int bound) ,均匀分布 返回值介于0(含)和指定值bound(不包括),从该随机数生成器的序列绘制
random random = new random(); /*for (int i = 0; i < 10; i++) { system.out.println(random.nextint()); }*/ /*for (int i = 0; i < 10; i++) { int j = random.nextint(10); system.out.println(j); }*/ //来一个随机值,这个数据的范围必须是1~100,33~66 54~78 //random.nextint(100);//0~99 +1 -> 1~100 /*33~66 - 33 -> 0~33 for (int i = 0; i < 10; i++) { system.out.println(random.nextint(34) + 33); }*/ //54~78 - 54 -> 0~24 for (int i = 0; i < 10; i++) { system.out.println(random.nextint(25) + 54); }
比较器comparable<t> 和 comparator<t>
java.lang comparable<t> : 该接口对实现它的每个类的对象强加一个整体排序。 这个排序被称为类的自然排序 ,类的compareto方法被称为其自然比较方法 。
java中规定 某个类只要实现了comparable 接口之后,才能通过重写compareto()具备比较的功能。
抽象方法:
int compareto(t o) 将此对象(this)与 指定( o )的对象进行比较以进行排序。
this > o : 返回正数
this = o : 返回0
this < o : 返回负数
' this - o : 表示按照升序排序。 o - this : 表示按照降序排序。
' 小结 : 如果java中的对象需要比较大小,那么对象所属的类要实现comparable接口,然后重写compareto(t o)实现比较的方式。
public class student implements comparable<student>{ .... @override public int compareto(student o) { return this.age-o.age;//升序 }}
java.util comparator<t> : 比较器接口。
抽象方法:
int compare( t o1, t o2 ) 比较其两个参数的大小顺序。
比较器接口的使用场景:
1. arrays.sort() : static <t> void sort( t[] a, comparator c)
2. collections 集合工具类 : void sort(list<t> list, comparator<> c) 根据指定的比较器给集合中的元素进行排序。
3. treeset 集合 : 构造方法 treeset( comparator c )
补充 : 在后面我还会介绍jdk1.8 的新特性(lambda 函数式代码优化) 进行优化此类接口
arraylist<string> list = new arraylist<string>(); list.add("cba"); list.add("aba"); list.add("sba"); list.add("nba"); //排序方法 按照第一个单词的降序 collections.sort(list, new comparator<string>() { @override public int compare(string o1, string o2) { int rs = o2.getcj() - o1.getcj(); return rs==0 ? o1.getage()-o2.getage():rs;// return o2.charat(0) - o1.charat(0); } }); system.out.println(list);
comparable 和 comparator 区别:
comparable : 对实现了它的类进行整体排序。
comparator : 对传递了此比较器接口的集合或数组中的元素进行指定方式的排序。
以上就是javase常用类及方法的介绍(附代码)的详细内容。
其它类似信息

推荐信息