当前位置: 移动技术网 > IT编程>开发语言>Java > JavaSE 常用类与其方法

JavaSE 常用类与其方法

2019年04月15日  | 移动技术网IT编程  | 我要评论
1.基本数据类型比较用:== 2.引用数据类型比较用:equals方法 如果引用数据类型使用==比较的话,比较的是地址值 toString类 对象调用toString()需要重写本方法: 在封装类中,否则输出的是地址 equals方法 '对象' 调用equals()需要重写本方法: 在封装类中重写, ...

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 起经过的毫秒值

 1   //请打印出1970年1月2号的时间的对象
 2 
 3     date date2 = new date(24 * 60 * 60 * 1000);
 4 
 5     system.out.println(date2);
 6 
 7   //获取当前时间的毫秒值
 8     date date = new date();
 9     system.out.println(date.gettime());
10 
11   //将date,改成1970年1,月1号   
12 
13     date.settime(0);
14 
15     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)

 1      //根据系统时间创建date对象
 2         date date = new date();
 3         system.out.println(date);
 4 
 5         //date不好看,格式化为中国式时间
 6      //simpledateformat sdf = new simpledateformat("yyyy年mm月dd日  hh点mm分ss秒 sss毫秒");
 7         simpledateformat sdf = new simpledateformat("yyyy年mm-dd  hh:mm:ss");
 8         //将date格式化为string
 9         string time = sdf.format(date);
10         system.out.println(time);
11 
12         //注意,我们一般人不会记忆毫秒值,能不能根据具体的时间(2019-03-11  11:16:02)解析成毫秒值
13         //parseexception: unparseable date: "2018年03-11  11:18:57",注意,模式必须与之前一致
14         date date1 = sdf.parse("2018年03-11  11:18:57");
15         system.out.println(date1);
16         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使用)

 

 1 import java.util.calendar;
 2 
 3 public class calendarutil {
 4     public static void main(string[] args) {
 5         //get方法
 6         // 创建calendar对象
 7         calendar cal = calendar.getinstance();
 8         // 设置年 
 9         int year = cal.get(calendar.year);
10         // 设置月
11         int month = cal.get(calendar.month) + 1;
12         // 设置日
13         int dayofmonth = cal.get(calendar.day_of_month);
14         //set方法
15         calendar cal = calendar.getinstance();
16         cal.set(calendar.year, 2020);
17         //add方法
18         cal.add(calendar.day_of_month, 2); // 加2天
19         cal.add(calendar.year, -3); // 减3年
20         //gettime方法
21         date date = cal.gettime();
22     }    
23 }

 

 

system

  • public static long currenttimemillis():返回以毫秒为单位的当前时间。
  • public static void arraycopy(object src, int srcpos, object dest, int destpos, int length):将数组中指定的数据拷贝到另一个数组中。

 

  currenttimemillis方法

1 import java.util.date;
2 
3 public class systemdemo {
4     public static void main(string[] args) {
5            //获取当前时间毫秒值
6         system.out.println(system.currenttimemillis()); // 1516090531144
7     }
8 }

 

  arraycopy方法

参数序号

参数名称

参数类型

参数含义

1

src

object

源数组

2

srcpos

int

源数组索引起始位置

3

dest

object

目标数组

4

destpos

int

目标数组索引起始位置

5

length

int

复制元素个数

 1 import java.util.arrays;
 2 
 3 public class demo11systemarraycopy {
 4     public static void main(string[] args) {
 5         int[] src = new int[]{1,2,3,4,5};
 6         int[] dest = new int[]{6,7,8,9,10};
 7         system.arraycopy( src, 0, dest, 0, 3);
 8         /*代码运行后:两个数组中的元素发生了变化
 9          src数组元素[1,2,3,4,5]
10          dest数组元素[1,2,3,9,10]
11         */
12     }
13 }

 

 

random

  构造方法:

  random() 创建一个新的随机数生成器。

  成员方法 :

    int nextint() 从这个随机数生成器的序列返回下一个伪随机数,均匀分布的 int值。

    int nextint(int bound) ,均匀分布 返回值介于0(含)和指定值bound(不包括),从该随机数生成器的序列绘制

 1         random random = new random();
 2         /*for (int i = 0; i < 10; i++) {
 3             system.out.println(random.nextint());
 4         }*/
 5 
 6         /*for (int i = 0; i < 10; i++) {
 7             int j = random.nextint(10);
 8             system.out.println(j);
 9         }*/
10 
11         //来一个随机值,这个数据的范围必须是1~100,33~66 54~78
12         //random.nextint(100);//0~99 +1 -> 1~100
13         /*33~66 - 33 -> 0~33
14         for (int i = 0; i < 10; i++) {
15             system.out.println(random.nextint(34) + 33);
16         }*/
17 
18         //54~78 - 54 -> 0~24
19         for (int i = 0; i < 10; i++) {
20             system.out.println(random.nextint(25) + 54);
21         }

 

 

比较器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)实现比较的方式。

1 public class student implements comparable<student>{
2     ....
3     @override
4     public int compareto(student o) {
5         return this.age-o.age;//升序
6     }
7 }

 

  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  函数式代码优化)  进行优化此类接口 

 1 arraylist<string> list = new arraylist<string>();
 2         list.add("cba");
 3         list.add("aba");
 4         list.add("sba");
 5         list.add("nba");
 6         //排序方法  按照第一个单词的降序
 7         collections.sort(list, new comparator<string>() {
 8             @override
 9             public int compare(string o1, string o2) {
10                 int rs = o2.getcj() - o1.getcj();
11                 return rs==0 ? o1.getage()-o2.getage():rs;
12 //                return o2.charat(0) - o1.charat(0);
13             }
14         });
15         system.out.println(list);

  comparable 和 comparator 区别:

    comparable : 对实现了它的类进行整体排序。

    comparator : 对传递了此比较器接口的集合或数组中的元素进行指定方式的排序。

 

 

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网