Java常用API-高级

---恢复内容开始---

Object类是Java语言中的根类,即所有类的父类。它中描述的所有方法子类都可以使用。所有类在创建对象的时候,最终找的父类就是Object。

* String toString()  : 返回该对象的字符串表示

 *  return getClass().getName() + "@" + Integer.toHexString(hashCode());

 *  getClass():返回一个字节码对象

 *  Integer.toHexString():返回指定参数的十六进制字符串形式

 *  hashCode():返回该对象的哈希码值(内部地址)

 

 

重写equals()方法

 

 1 @Override
 2     public boolean equals(Object obj) {
 3         //提高效率
 4         if (this == obj)
 5             return true;
 6        
 7         if (obj == null)
 8             return false;
 9         //提高健壮性
10         if (getClass() != obj.getClass())
11             return false;
12        
13         //向下转型
14         Person other = (Person) obj;
15        
16         if (age != other.age)
17             return false;
18         if (name == null) {
19             if (other.name != null)
20                 return false;
21         } else if (!name.equals(other.name))
22             return false;
23         return true;
24     }

 

 

返回两个数中间的随机数:

1         int number = (int) (Math.random() * (end - start + 1)) + start;

2         return number;

 

* Random:产生随机数的类

* 构造方法:
* public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
* public Random(long seed):给出指定的种子
*
* 给定种子后,每次得到的随机数是相同的。
*
* 成员方法:
* public int nextInt():返回的是int范围内的随机数
* public int nextInt(int n):返回的是[0,n)范围的内随机数

1         // 创建对象
2         // Random r = new Random();
3         Random r = new Random(1111);
4
5         for (int x = 0; x < 10; x++) {
6             // int num = r.nextInt();
7             int num = r.nextInt(100) + 1;
8             System.out.println(num);
9         }

 

 

* System:包含一些有用的类字段和方法。它不能被实例化  

* static void arraycopy(Object srcint srcPos, Object destint destPos, int length)  

 * 复制数组

 * 参数1:源数组

 * 参数2:源数组的起始索引位置

 * 参数3:目标数组

 * 参数4:目标数组的起始索引位置

 * 参数5:指定接受的元素个数

1         // 定义数组
2         int[] arr = { 11, 22, 33, 44, 55 };
3         int[] arr2 = { 6, 7, 8, 9, 10 };
4
5         // 请大家看这个代码的意思
6         System.arraycopy(arr, 1, arr2, 2, 2);
7
8         System.out.println(Arrays.toString(arr));
9         System.out.println(Arrays.toString(arr2));

 

 

*  static long currentTimeMillis() :以毫秒值返回当前系统时间

*  这个毫秒的时间是相对时间,相对于1970-1-1 00:00:00 : 0

 

1   long start = System.currentTimeMillis();
2         for (int i = 0; i < 100000; i++) {
3             System.out.println(i);
4         }
5         long end = System.currentTimeMillis();
6         System.out.println(end - start);

 

 

static void exit(int status)

 终止当前正在运行的 Java 虚拟机

  static void gc()  

运行垃圾回收器

 

 * BigInteger:可以让超过Integer范围内的数据进行运算

* public BigInteger add(BigInteger val):加
* public BigInteger subtract(BigInteger val):减
* public BigInteger multiply(BigInteger val):乘
* public BigInteger divide(BigInteger val):除
* public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组

 

* 因为float类型的数据存储和整数不一样导致的。它们大部分的时候,都是带有有效数字位。由于在运算的时候,float类型和double很容易丢失精度,演示案例。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal

* BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。

* 构造方法:
* public BigDecimal(String val)

* public BigDecimal add(BigDecimal augend)
* public BigDecimal subtract(BigDecimal subtrahend)
* public BigDecimal multiply(BigDecimal multiplicand)
* public BigDecimal divide(BigDecimal divisor)
* public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取

System.out.println("divide:" + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));

 

* Date: 表示特定的瞬间,精确到毫秒,他可以通过方法来设定自己所表示的时间,可以表示任意的时间

 * System.currentTimeMillis():返回的是当前系统时间,1970-1-1至今的毫秒数

 *

 * 构造方法:

 * Date() :创建的是一个表示当前系统时间的Date对象

Date(long date) :根据"指定时间"创建Date对象



* public long getTime():获取时间,以毫秒为单位
* public void setTime(long time):设置时间

* 从Date得到一个毫秒值
* getTime()
* 把一个毫秒值转换为Date
* 构造方法
* setTime(long time)

 

 1 Date d = new Date();//默认当前系统时间
 2         //d.setTime(1000 * 60 * 60 * 24 * 2);
 3         System.out.println(d.toLocaleString());
 4         System.out.println(d.getTime());//172800000
 5        
 6        
 7         d.setTime(172800000L);
 8         System.out.println(d.toLocaleString());

 

 

* Date -- String(格式化)
* public final String format(Date date)

* String -- Date(解析)
* public Date parse(String source)

* DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。

* SimpleDateFormat的构造方法:
* SimpleDateFormat():默认模式
* SimpleDateFormat(String pattern):给定的模式
* 这个模式字符串该如何写呢?
* 通过查看API,我们就找到了对应的模式
* 年 y
* 月 M
* 日 d
* 时 H
* 分 m
* 秒 s

* 2014年12月12日 12:12:12

 1         // Date -- String
 2         // 创建日期对象
 3         Date d = new Date();
 4         // 创建格式化对象
 5         // SimpleDateFormat sdf = new SimpleDateFormat();
 6         // 给定模式
 7         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
 8         // public final String format(Date date)
 9         String s = sdf.format(d);
10         System.out.println(s);
1         //String -- Date
2         String str = "2008-08-08 12:12:12";
3         //在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
4         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5         Date dd = sdf2.parse(str);
6         System.out.println(dd);

 

 

Calener类用法:

* Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
*
* public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。

 

 1         // 其日历字段已由当前日期和时间初始化:
 2         Calendar rightNow = Calendar.getInstance(); // 子类对象
 3 
 4         // 获取年
 5         int year = rightNow.get(Calendar.YEAR);
 6         // 获取月
 7         int month = rightNow.get(Calendar.MONTH);
 8         // 获取日
 9         int date = rightNow.get(Calendar.DATE);
10 
11         System.out.println(year + "年" + (month + 1) + "月" + date + "日");

 

 

 1         // 获取当前的日历时间
 2         Calendar c = Calendar.getInstance();
 3 
 4         // 获取年
 5         int year = c.get(Calendar.YEAR);
 6         // 获取月
 7         int month = c.get(Calendar.MONTH);
 8         // 获取日
 9         int date = c.get(Calendar.DATE);
10         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
11 
12         
13         // 5年后的10天前
14         c.add(Calendar.YEAR, 5);
15         c.add(Calendar.DATE, -10);
16         // 获取年
17         year = c.get(Calendar.YEAR);
18         // 获取月
19         month = c.get(Calendar.MONTH);
20         // 获取日
21         date = c.get(Calendar.DATE);
22         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
23         System.out.println("--------------");
24 
25         c.set(2011, 11, 11);
26         // 获取年
27         year = c.get(Calendar.YEAR);
28         // 获取月
29         month = c.get(Calendar.MONTH);
30         // 获取日
31         date = c.get(Calendar.DATE);
32         System.out.println(year + "年" + (month + 1) + "月" + date + "日");

 

 * 获取任意一年的二月有多少天

 1         // 键盘录入任意的年份
 2         Scanner sc = new Scanner(System.in);
 3         System.out.println("请输入年份:");
 4         int year = sc.nextInt();
 5 
 6         // 设置日历对象的年月日
 7         Calendar c = Calendar.getInstance();
 8         c.set(year, 2, 1); // 其实是这一年的3月1日
 9         // 把时间往前推一天,就是2月的最后一天
10         c.add(Calendar.DATE, -1);
11 
12         // 获取这一天输出即可
13         System.out.println(c.get(Calendar.DATE));

 

 

 

* 需求:判断一个数是否符合int类型的范围

 * 由于基本数据类型只能做一些简单的操作和运算,所以Java为我们封装了基本数据类型,为每种基本数据类型提供了包装类

 * 包装类就是封装了基本数据类型的类,为我们提供了更多复杂的方法和一些变量

 1 //Integer(String s)
 2         Integer i = new Integer("10");
 3         System.out.println(i);
 4        
 5         //int intValue() 
 6         int a = i.intValue();
 7         System.out.println(a + 10 );
 8        
 9         //static int parseInt(String s)
10         int b = Integer.parseInt("20");
11         System.out.println(b + 30);
12        
13        
14         //Integer(int value)
15         Integer i2 = new Integer(40);
16         String s = i2.toString();
17         System.out.println(s);
18        
19         //static String toString(int i) 
20         String s2 = Integer.toString(50);
21         //System.out.println(s2);

 

 

* JDK1.5特性:自动装箱和拆箱

 

 1         //自动装箱
 2         //相当于: Integer i = new Integer(10);
 3         //Integer i = 10;
 4        
 5         //自动拆箱
 6         //相当于 int a = i.intValue();
 7         //Integer i = 10;
 8         //int a = i;
 9        
10         Integer i = 10;
11         Integer i2 = 20;
12         Integer i3 = i + i2;
13         /*
14          * Integer i3 = new Integer(i.intValue() + i2.intValue());
15          *
16          */
17         ArrayList list = new ArrayList();
18         list.add(1);//自动装箱,list.add(new Integer(1));

 

 

 

正则表达式:就是一套规则,可以用于匹配字符串

boolean matches(String regex) :判断当前字符串是否匹配指定的正则表达式,如果匹配则返回true,否则返回false

 

判断字符串是不是都由数字组成:

1         //必须都是数字
2         for (int i = 0; i < length; i++) {
3             //得到参数的每一个字符
4             char c = qq.charAt(i);
5             if(c < '0' || c > '9') {
6                 return false;
7             }
8         }   

 

 或者:

1                 char[] chs = qq.toCharArray();
2                 for (int x = 0; x < chs.length; x++) {
3                     char ch = chs[x];
4                     if (!Character.isDigit(ch)) {
5                         flag = false;
6                         break;
7                     }
8                 }

 

 

 

* 校验qq号码

* 要求必须是5-15位

* 0不能开头

* 必须都是数字

boolean flag = qq.matches("[1-9][0-9]{4,14}");

 

//定义邮箱的规则
//String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";

 

* 分割功能
* String类的public String[] split(String regex)
* 根据给定正则表达式的匹配拆分此字符串。

 1         //定义一个年龄搜索范围
 2         String ages = "18-24";
 3        
 4         //定义规则
 5         String regex = "-";
 6        
 7         //调用方法
 8         String[] strArray = ages.split(regex);
 9        
10 //        //遍历
11 //        for(int x=0; x<strArray.length; x++){
12 //            System.out.println(strArray[x]);
13 //        }
14        
15         //如何得到int类型的呢?
16         int startAge = Integer.parseInt(strArray[0]);
17         int endAge = Integer.parseInt(strArray[1]);

 

 

//硬盘上的路径,我们应该用\\替代\

1 String s4 = "E:\\JavaSE\\day14\\avi";
2 String[] str4Array = s4.split("\\\\");
3 for (int x = 0; x < str4Array.length; x++) {
4 System.out.println(str4Array[x]);
5 }

 

 

* 我有如下一个字符串:"91 27 46 38 50"
* 请写代码实现最终输出结果是:"27 38 46 50 91"

 

 1         // 定义一个字符串
 2         String s = "91 27 46 38 50";
 3
 4         // 把字符串进行分割,得到一个字符串数组
 5         String[] strArray = s.split(" ");
 6
 7         // 把字符串数组变换成int数组
 8         int[] arr = new int[strArray.length];
 9
10         for (int x = 0; x < arr.length; x++) {
11             arr[x] = Integer.parseInt(strArray[x]);
12         }
13
14         // 对int数组排序
15         Arrays.sort(arr);
16
17         // 把排序后的int数组在组装成一个字符串
18         StringBuilder sb = new StringBuilder();
19         for (int x = 0; x < arr.length; x++) {
20             sb.append(arr[x]).append(" ");
21         }
22         //转化为字符串
23         String result = sb.toString().trim();
24        
25         //输出字符串
26         System.out.println("result:"+result);

 

 

* 替换功能
* String类的public String replaceAll(String regex,String replacement)
* 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

 

 1         // 定义一个字符串
 2         String s = "helloqq12345worldkh622112345678java";
 3
 4         // 我要去除所有的数字,用*给替换掉
 5         // String regex = "\\d+";
 6         // String regex = "\\d";
 7         //String ss = "*";
 8        
 9         // 直接把数字干掉
10         String regex = "\\d+";
11         String ss = "";
12
13         String result = s.replaceAll(regex, ss);
14         System.out.println(result);

 

 

* 获取功能
* Pattern和Matcher类的使用
*
* 模式和匹配器的基本使用顺序

1         // 模式和匹配器的典型调用顺序
2         // 把正则表达式编译成模式对象
3         Pattern p = Pattern.compile("a*b");
4         // 通过模式对象得到匹配器对象,这个时候需要的是被匹配的字符串
5         Matcher m = p.matcher("aaaaab");
6         // 调用匹配器对象的功能
7         boolean b = m.matches();
8         System.out.println(b);

 

 

* 获取功能:
* 获取下面这个字符串中由三个字符组成的单词
* da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?

 

 1         // 定义字符串
 2         String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
 3         // 规则
 4         String regex = "\\b\\w{3}\\b";
 5
 6         // 把规则编译成模式对象
 7         Pattern p = Pattern.compile(regex);
 8         // 通过模式对象得到匹配器对象
 9         Matcher m = p.matcher(s);
10         // 调用匹配器对象的功能
11         // 通过find方法就是查找有没有满足条件的子串
12         // public boolean find()
13        
14
15         while (m.find()) {
16             System.out.println(m.group());
17         }
18
19         // 注意:一定要先find(),然后才能group()
20         // IllegalStateException: No match found
21         // String ss = m.group();
22         // System.out.println(ss);

 

 

常用的正则表达式:

A:字符
x 字符 x。举例:'a'表示字符a
\\ 反斜线字符。
\n 新行(换行)符 ('\u000A') 
\r 回车符 ('\u000D')

B:字符类
[abc] a、b 或 c(简单类) 
[^abc] 任何字符,除了 a、b 或 c(否定) 
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
[0-9] 0到9的字符都包括

C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? \.
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成

D:边界匹配器
^ 行的开头 
$ 行的结尾 
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi

E:Greedy 数量词 
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次 
X{n,} X,至少 n 次 
X{n,m} X,至少 n 次,但是不超过 m 次

posted @ 2018-10-14 10:57  武士黄  阅读(642)  评论(0编辑  收藏  举报