黑马程序员——基础学习(十)API中Arrays、Integer、正则表达式以及日期类的相关应用

来源:互联网 发布:常用协议端口有多少 编辑:程序博客网 时间:2024/06/10 00:02
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

API

数组高级

排序

冒泡排序:

相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处,其他元素同理即可。

public static void bubbleSort(int[] arr) {for(int x=0; x<arr.length-1; x++){for(int y=0; y<arr.length-1-x; y++){if(arr[y]>arr[y+1]){int temp = arr[y];arr[y] = arr[y+1];arr[y+1] = temp;}}}}

选择排序:

0索引处的元素依次和后面的所有元素进行比较,第一次完毕,最小值出现在了0索引处。其他元素同理即可。

public static void selectSort(int[] arr) {for(int x=0; x<arr.length-1; x++) {for(int y=x+1; y<arr.length; y++) {if(arr[y] < arr[x]) {int temp = arr[x];arr[x] = arr[y];arr[y] = temp;}}}}

查找

基本查找

 数组无序

public static int getIndex(int[] arr,int value) {int index = -1;for(int x=0; x<arr.length; x++) {if(arr[x] == value) {index = x;break;}}return index;}

二分查找

 数组有序

public static int getIndex(int[] arr,int value) {int maxIndex = arr.length-1; int minIndex = 0;int midIndex = (maxIndex+minIndex)/2;while(arr[midIndex] != value) {if(arr[midIndex] > value) {maxIndex = midIndex - 1;}else if(arr[midIndex] < value) {minIndex = midIndex + 1;}if(minIndex > maxIndex) {return -1;}midIndex = (maxIndex+minIndex)/2;}return midIndex;}

Arrays工具类

Arrays是针对数据操作的工具类。提供了排序,查找等功能。

成员方法:

A:把数组转成字符串

B:排序

C:二分查找

基本类型包装类

为了对基本类型的数据进行更多的操作,java就提供了基本类型的包装类。

对应的关系

byte   ·  Byte

short Short

int Integer

long Long

float Float

double Double

char Character

boolean Boolean

Integer的构造方法

A:Integer i = new Integer(100);

B:Integer i = new Integer("100");

注意:这里的字符串必须是数字字符组成

成员方法:

A:进制转换相关的方法(知道就行)

B:把字符串转成int类型的数据

parseInt(String s)

如何把int类型和String类型进行转换

String -- int

Integer.parseInt(String s)

int  --String

String.vlaueOf(int number)

JDK5的新特性:

自动装箱:int -- Integer

valueOf()

自动拆箱:Integer -- int

intValue()

看如下的代码,能够解释即可:

Integer i = 1;

i += 2;

做了哪些事情即可。

开发原则:

只要是对象,建议进行不为null的判断。

正则表达式

符合一定规则的字符串。

规则字符:

A:字符

x 字符 x

\\ 反斜线字符

\r 回车

\n 换行

B:字符类

[abc] a或 c

[^abc] 任何字符,除了 a或 c

[a-zA-Z] a 到 或 到 Z,两头的字母包括在内

[0-9] 匹配所有的数字字符

C:预定义字符类

任何字符

\d 数字:[0-9]

\w 单词字符:[a-zA-Z_0-9] 

D:边界匹配器

行的开头 

行的结尾 

\b 单词边界

出现的不能是单词字符

abcd hello?xixi^adsf

E:数量词

X? X,一次或一次也没有 

X* X,零次或多次 

X+ X,一次或多次 

X{n} X,恰好 次 

X{n,} X,至少 次 

X{n,m} X,至少 次,但是不超过 次 

正则表达式的功能

A:判断功能

String

  public boolean matches(String regex)

B:分割功能

String

  public String[] split(String regex)

  举例:

  18-26

  age>=18 && age<=26

C:替换功能

String

   public String replaceAll(String regex,String replacement)

D:获取功能

获取功能是由:PatternMatcher类的使用

案例:

A:校验电话号码和邮箱

import java.util.Scanner;public class RegexDemo {<span style="white-space:pre"></span>public static void main(String[] args) {//获取电话号码字符串<span style="white-space:pre"></span>Scanner sc = new Scanner(System.in);<span style="white-space:pre"></span>System.out.println("请输入你的电话号码:");String phone = sc.nextLine();<span style="white-space:pre"></span>//public boolean matches(String regex)//定义规则/* * 13838383838 * 13688888888 * 13212345678 * 13111111111 * 15812345678 * 15712121212 * 15598986767 */<span style="white-space:pre"></span>String regex = "1[35][0-9]{9}";//调用功能<span style="white-space:pre"></span>boolean flag = phone.matches(regex);System.out.println(flag);}}

B:去掉论坛中的数字字符

public class RegexDemo {public static void main(String[] args) {// 定义一个字符串String s = "今天天气很好,12345,这是我的qq好,加我哦。银行卡号:123456789"; String result = s.replaceAll("\\d+", "*");System.out.println(result);}}

C:获取一个字符串中由三个字符组成的单词

import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegexDemo {public static void main(String[] args) {String s = "wo men dou shi hao hai zi,suo yi ming tian bu fang jia";// 定义规则String regex = "\\b[a-z]{3}\\b";// 把正则表达式给编译生成模式对象Pattern p = Pattern.compile(regex);Matcher m = p.matcher(s);while (m.find()) {System.out.println(m.group());}}}

Date

表示日期类,精确到毫秒

构造方法

A:Date d = new Date();

B:Date d = new Date(long time);

成员方法

A:获取毫秒值

B:设置日期的毫秒值

DateFormat/SimpleDateFormat

对日期进行格式化,或者把一个字符串解析成日期的类

成员方法

A:格式化

format(Date d)

B:解析

parse(String s)

注意模式问题:

yyyy-MM-dd HH:mm:ss

Calendar

日历类,把日期的数据进行了拆分,分成了很多个日历字段,需要哪些数据,就自己拼接哪些数据

成员方法

A:通过日历字段获取对应的值

B:设置年,月,日

C:根据给定的int值,来把给定的日历字段添加或者减去对应的值


0 0
原创粉丝点击