【Java学习笔记】正则表达式·集合和列表

来源:互联网 发布:淘宝店铺头像可以换吗 编辑:程序博客网 时间:2024/06/08 13:24

.正则表达式

1.正则表达式的校验

1)典型顺序

Pattern p = Pattern.compile("a*b") ;

Matcher m = p.matcher("aaaaaaaab") ;

boolean flag = m.matches() ;

通过Pattern模式类中compile()要获取模式对象,通过里面的matcher()方法获取匹配器对象Matcher,再去使用

2)简化版

String regex = "a*b" ;

String s = "aaaab" ;

boolean flag = s.matches(regex) ;

2.正则校验手机号

String phone = sc.nextLine();

boolean flag = phone.matches(regex) ;

3.正则校验邮箱号

String email = sc.nextLine() ;

String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+" ;

boolean flag = email.matches(regex) ;

4.替换正则表达式的子字符串

String s = "hello1234567world6891234java" ;

String regex = "[0-9]+";

String regex = "\\d+" ;

String ss = "*" ;

String result = s.replaceAll(regex, ss) ;

5. 根据给定正则表达式的匹配切割此字符串

String s = "aa,bb,cc" ;

String[] strArray = s.split(",") ;

for(int x = 0 ; x < strArray.length ; x++){

System.out.println(strArray[x]);}

6.例题

我有如下一个字符串:"91 27 46 38 50",

请写代码实现最终输出结果是:"27 38 46 50 91"

public class RegexTest {

public static void main(String[] args) {

//定义一个字符串:

String s = "91 27 46 38 50" ;

//使用切割功能

String[] strArray = s.split(" ") ;

//转换int数组

//使用动态初始化

int[] arr = new int[strArray.length] ;

//遍历

for(int x = 0 ; x < strArray.length ; x ++){

arr[x] = Integer.parseInt(strArray[x]) ;

}

//对int数组进行排序

Arrays.sort(arr) ;

//最终将排好序的数组组装成一个字符串类型

//创建一个字符串缓冲区对象

StringBuilder sb = new StringBuilder() ;

//遍历数组

for(int x = 0 ; x < arr.length ; x ++){

sb.append(arr[x]).append(" ") ;

}

//最终将StringBuilder转换成字符串类型

String result = sb.toString().trim() ;//去除两端空格

//输出

System.out.println("result:"+result);

}

}

.集合

1.定义

学习过哪些容器变量?数组,StringBuffer,但是StringBuffer始终返回的是一个字符串,可能不是我们想要的结果;数组的长度是固定的,不能满足长度变化的需求,java就提供了----集合,满足长度变化的需求!

2. 集合和数组的区别?

1)长度的区别:数组:长度是固定的  集合:长度是可变的

2)内容的区别:数组存储的是同一种类型的元素:举例:水杯中的水

集合存储的是不同类型的元素:举例:杯子:加水,加可乐;

3)存储数据类型的区别:数组:可以存储基本数据类型,也可以存储引用类型   集合:只能存储引用数据类型

3.特点

JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现,而这里子接口中又有自己的子实现类!

 

4.功能

Collection c = new ArrayList() ;   

1)boolean add(Object e):给集合中添加一个元素

c.add("hello"); c.add("wold") ; c.add("java") ;

给集合中添加一个元素::只要用集合对象调用add()方法,始终返回true

2)void clear()移除此 集合 中的所有元素

c.clear();

3)boolean remove(Object o):删除此集合中的指定的元素

c.remove("hello");

4)boolean contains(Object o):判断是集合中是否包含指定的元素

c.contains("javaee");

5)boolean isEmpty():判断集合是否为空

c.isEmpty();

5.面试题

面试题:数组中有没有length(),String类中有没有length(),集合中有没有length()方法?

数组中:length属性,

String类中:有length()方法,获取字符串的长度

集合中没有length()方法,获取集合的元素的方法:size()

6.高级功能

Collection c1 = new ArrayList();

1)boolean addAll(Collection c):

c1.addAll(c2)c1添加c2集合中所有的元素

2)boolean removeAll(Collection c) 移除一个集合中的元素,包含一个元素算是删除!

c1.removeAll(c2) c1移除和c2中相同的元素

3)boolean containsAll(Collection c) 包含一个集合中的所有元素才算包含!

c1.containsAll(c2)c1包含c2中的所有元素才为true,c1 c2 不变

4)boolean retainAll(Collection c) 如果一个A集合对B集合取交集,那么交集的元素要存储A里面,返回值类型boolean在这里表示的是A集合中的元素是否发生变化!

c1.retainAll(c2)c1和c2的交集返回到c1里面

7.其他功能

Object[] toArray():将集合转换成数组

利用这个功能就可以完成集合对元素的遍历

.list列表 和 迭代器

1. List集合是Collection集合的子接口,就直接可以使用迭代器遍历

创建List集合对象      List l = new ArrayList() ;

获取迭代器对象进行遍历 Iterator it = l.iterator() ;

2.特点

List集合:里面的存储的元素是可以重复的,并且是有序的(里面的元素有序:指的是存储和取出一致!)

3.功能

1)void add(int index,Object element):在指定位置插入一个指定的元素

list.add(1, "javaee") ;

2)Object remove(int index) 移除列表中指定位置的元素

list.remove(1);

3 )Object get(int index)返回列表中指定位置的元素

list.get(3);

4)Object set(int index,Object element)用指定元素替换列表中指定位置的元素

list.set(2, "android");

4.list的两种遍历方法

1)Iterator it = list.iterator() ;

while(it.hasNext()){

String s = (String) it.next() ;

System.out.println(s);

2)for(int x = 0 ; x < list.size() ; x++){

String s = (String) list.get(x) ;

System.out.println(s);

}

5.列表迭代器

1)定义

List集合专有的迭代遍历方式:

ListIterator listIterator():列表迭代器(单独讲)

返回的是ListIterator接口,而该接口又继承自Iterator接口,所以该列表迭代器中就有hasNext()/next()

2)特点

boolean hasNext():正向遍历

Object next()

boolean hasPrevious():逆向遍历

bject previous();

注意:果想要逆向遍历,前提必须有正向遍历,逆向遍历一般不使用,没有意义!

ListIterator lit = list.listIterator() ;

while(lit.hasNext()){

String s = (String) lit.next() ;

System.out.println(s);

}

System.out.println("-----------------------");

while(lit.hasPrevious()){

String s = (String) lit.previous() ;

System.out.println(s);

}

 

原创粉丝点击