黑马程序员_JavaSE的正则表达式和Collection

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


1:正则表达式

(1)就是符合一定规则的字符串
(2)常见规则
A:字符 x 字符 x。举例:'a'表示字符a \\ 反斜线字符 \n 新行(换行)符 ('\u000A')  \r 回车符 ('\u000D')
B:字符类 [abc] a、b 或 c [^abc] 任何字符,除了 a、b 或 c  \d 数字:[0-9][a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内  [0-9] 0到9的字符都包括
C:预定义字符类 . 任何字符 \w 单词字符:[a-zA-Z_0-9]
D:边界匹配器 ^ 行的开头  $ 行的结尾  \b 单词边界
E:Greedy 数量词  X? X,一次或一次也没有 X* X,零次或多次 X+ X,一次或多次 X{n} X,恰好 n 次 
X{n,} X,至少 n 次  X{n,m} X,至少 n 次,但是不超过 m 次 
(3)常见功能:
A:判断功能
String类的public boolean matches(String regex)  是否匹配给定的正则表达式
B:分割功能
String类的public String[] split(String regex): 根据给定正则表达式的匹配拆分字符串
C:替换功能
String类的public String replaceAll(String regex,String replacement)   使用给定的replacement,替换此字符串中所有匹配给定的正则表达式的字符串
D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");   把正则表达式编译成模式对象
Matcher m = p.matcher("aaab");    通过模式对象得到匹配器对象

find():查找存不存在

group():获取刚才查找过的数据

2:Math

(1)针对数学运算进行操作的类
(2)常见方法
A:绝对值     public static int abs(int a);
B:向上取整   public static double ceil(double a);
C:向下取整   public static double floor(double a);
D:两个数据中的大值   public static int max(int a , int b);
E:a的b次幂   public static double pow(double a, double b);
F:随机数   public static double random();
G:四舍五入   public static long round(double a);
H:正平方根    public static double sqrt(double a);
3:Random
(1)用于产生随机数的类
(2)构造方法: A:Random() 默认种子,每次产生的随机数不同 B:Random(long seed) 指定种子,每次种子相同,随机数就相同
(3)成员方法: A:int nextInt() 返回int范围内的随机数 B:int nextInt(int n) 返回[0,n)范围内的随机数
4:System
(1)系统类,提供了一些有用的字段和方法
(2)成员方法
A:运行垃圾回收器  publicstatic void gc()
B:退出jvm     public  static void exit(int status)
C:获取当前时间的毫秒值    public static void long currentTimeMillis()
D:数组复制 public static void array(Object src,int srcPos,Object dest,int destPos,int length)
5:BigInteger
(1)针对大整数的运算

(2)构造方法 BigInteger(String s)

(3)成员方法

A:加    public BigInteger add(BigInteger val)

B:减 public BigIntegersubstract(BigInteger val)

C:乘 public BigIntegermultiply(BigInteger val)

D:除 public BigIntegerdevide (BigInteger val)
E:商和余数 public BigInteger[] devideAndRemainder (BigInteger val)
6:BigDecimal
(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。
(2)构造方法 BigDecimal(String s)
(3)成员方法:
A:加 public BigDecimal add(BigDecimal val)

B:减 public BigDecimal substract(BigDecimal val)

C:乘 public BigDecimal multiply(BigDecimal val)

D:除 public BigDecimal devide (BigDecimal val)
E:自己保留小数几位 public BigDecimal divide (BigDecimaldivisor,intscale,int roundingMode )
7:Date/DateFormat
(1)Date是日期类,可以精确到毫秒。
A:构造方法 Date() Date(long time)
B:成员方法 getTime() setTime(long time)
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
A:SimpleDateFormat(String pattern) 给定模式 yyyy-MM-dd HH:mm:ss
B:日期和字符串的转换 a:Date -- String format()

b:String -- Date

parse()

8:集合(Collection)
(1)集合的继承体系结构
由于需求不同,Java就提供了不同的集合类。这多个集合类的数据结构不同,但是它们都是要提供存储和遍历功能的,
我们把它们的共性不断的向上提取,最终就形成了集合的继承体系结构图。
Collection
|--List
|--ArrayList
|--Vector
|--LinkedList
|--Set
|--HashSet
|--TreeSet
(2)Collection的功能概述
A:添加功能  boolean add(E e)
B:删除功能 void clear()
C:判断功能 boolean contains(Object o)
D:获取功能   Iterator<E> iterator()
E:长度功能 int size()
F:把集合转数组    object[] toArray()

(3)迭代器

A:是集合的获取元素的方式。
B:是依赖于集合而存在的。
(4)Collection集合的案例(遍历方式 迭代器)
集合的操作步骤:
A:创建集合对象
B:创建元素对象
C:把元素添加到集合
D:遍历集合

A:存储字符串并遍历
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();

//创建并添加元素
c.add("hello");
c.add("world");
c.add("javaSE");

//遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
String s =(String) it.next();
System.out.println(s);
}
}
}

B:存储自定义对象并遍历
public class Student {
private String name;
private int age;

public Student(){}

public Student(String name,int age) {
this.name = name;
this.age = age;
}
}

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();

//创建学生对象
Student s1 = new Student("张三",27);
Student s2 = new Student("李四",30);
Student s3 = new Student("王五",30);
Student s4 = new Student("孙六",25);
Student s5 = new Student("赵七",16);

//添加元素
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);

//遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值