DAY11
一、正则表达式:
案例展示
package cn.edu360;
import java.util.Scanner;
public class ClassDemo {
public static void main(String[] args) throws Exception {
/*
* 校验QQ号码:
* 1:要求必须是5~15的数字
* 2:不能以数字0开头
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入QQ号码:");
String qq = sc.nextLine();
//调用一个方法专门来校验QQ的合法
boolean result = CheckQQ(qq);
System.out.println(result);
}
private static boolean CheckQQ(String qq) {
if(!qq.startsWith("0")){
if(qq.length() >= 5 && qq.length() <= 15){
for (int i = 0; i < qq.length(); i++) {
char c = qq.charAt(i);
if(!Character.isDigit(c)){
return false;
}
}
return true;
}
}
return false;
}
}
正则表达式:
是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则,有自己特殊的应用
package cn.edu360;
import java.util.ArrayList;
import java.util.Collection;
public class ClassDemo {
public static void main(String[] args) throws Exception {
Collection c = new ArrayList();
c.add("javaee");
c.add("javase");
c.add("hadoop");
System.out.println(c);//[javaee, javase, hadoop]
c.remove("hadoop");
System.out.println(c);//[javaee, javase]
//c.clear();
System.out.println(c);//[]
System.out.println(c.contains("javase"));//true
System.out.println(c.size());//2
}
}
package cn.edu360;
import java.util.ArrayList;
import java.util.Collection;
public class ClassDemo {
public static void main(String[] args) throws Exception {
Collection c = new ArrayList();
c.add("javaee");
c.add("javase");
c.add("hadoop");
c.add(123);
Object[] array = c.toArray();
for (int i = 0; i < array.length; i++) {
Object val = array[i];
if(val.equals("hadoop")){
c.remove(val);
continue;
}
System.out.println(val);
}
System.out.println(c);//[javaee, javase, 123]
}
}
package cn.edu360;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class ClassDemo {
public static void main(String[] args) throws Exception {
Collection c = new ArrayList();
c.add("javaee");
c.add("javase");
c.add("hadoop");
c.add(123);
Iterator iterator = c.iterator();
while(iterator.hasNext()){
Object val = iterator.next();
if(val.equals("javase")){
//c.remove(val);//报错为:java.util.ConcurrentModificationException
iterator.remove();
continue;
}
System.out.println(val);
}
}
}
package cn.edu360;
import java.util.ArrayList;
import java.util.List;
public class ClassDemo {
public static void main(String[] args) throws Exception {
List c = new ArrayList();
c.add("javaee");
c.add("javase");
System.out.println(c);//[javaee, javase]
c.add(1, "helloWorld");
System.out.println(c);//[javaee, helloWorld, javase]
c.remove(1);
System.out.println(c);//[javaee, javase]
System.out.println(c.get(1));//javase
c.set(1, "Spark");
System.out.println(c);//[javaee, Spark]
}
}
package cn.edu360;
import java.util.ArrayList;
import java.util.Iterator;
public class ClassDemo {
public static void main(String[] args) throws Exception {
//遍历字符串
//test();
//遍历自定义数组
test2();
}
private static void test2() {
ArrayList list = new ArrayList();
list.add(new Person("张三", 18, "西安市"));
list.add(new Person("李四", 18, "天津市"));
list.add(new Person("王五", 18, "天水市"));
list.add(new Person("赵六", 18, "武汉市"));
System.out.println(list);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
private static void test() {
ArrayList c = new ArrayList();
c.add("javaee");
c.add("javase");
c.add("hadoop");
Iterator iterator = c.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next()+" ");
}
}
}
相应的类:
package cn.edu360;
public class Person {
private String name;
private int age;
private String address;
public Person() {
}
public Person(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ "]";
}
}
注意事项:LinkedList和ArrayList相同,在此不加赘述
三、泛型
四、增强for循环和注意事项:
package cn.edu360;
import java.util.ArrayList;
public class ClassDemo {
public static void main(String[] args) throws Exception {
ArrayList list = new ArrayList();
list.add("hadoop");
list.add("小花猫");
list.add("javaee");
for(String value: list){
System.out.print(value+", ");
}
}
}
注意事项:
五、可变参数:
就是当不知道方法的参数定义几个的时候,可以使用可变参数
格式如下:
代码实现:
package cn.edu360;
import java.util.Arrays;
public class ClassDemo {
public static void main(String[] args) throws Exception {
test(1,2);
}
private static void test(int... i) {
System.out.println(Arrays.toString(i));
}
}
注意事项:
package cn.edu360;
import java.util.Arrays;
import java.util.List;
public class ClassDemo {
public static void main(String[] args) throws Exception {
//将数组转换成集合
Integer[] arr = {1, 2, 3, 4, 5};
List list = Arrays.asList(arr);
//将集合转换成数组
Object[] array = list.toArray();
}
}
注意事项:
代码实现:
package cn.edu360;
import java.util.HashMap;
import java.util.Map;
public class ClassDemo {
public static void main(String[] args) {
Map map = new HashMap();
map.put(110, "报警电话");
map.put(120, "急救电话");
map.put(119, "火警电话");
System.out.println(map);//{119=火警电话, 120=急救电话, 110=报警电话}
map.remove(110);
System.out.println(map);//{119=火警电话, 120=急救电话}
System.out.println(map.containsKey(120));//true
System.out.println(map.containsValue("哈哈"));//false
System.out.println(map.isEmpty());//false
System.out.println(map.size());//2
}
}
代码实现:
package cn.edu360;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class ClassDemo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put(1, "哈哈");
map.put(1, "呵呵");
map.put(2, "嘻嘻");
map.put(3, "哈哈");
map.put(4, "嘻嘻");
map.put(5, "呵呵");
//System.out.println(map);
Set keys = map.keySet();
//System.out.println(keys);
Collection values = map.values();
//System.out.println(values);
for(Integer key: keys){
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
(2)第二种遍历方式
获取键值对对象集合,然后遍历键值对集合,然后通过键值对对象获取键和值
代码实现:
package cn.edu360;
import java.util.HashMap;
import java.util.Map.Entry;
public class ClassDemo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put(1, "哈哈");
map.put(1, "呵呵");
map.put(2, "嘻嘻");
map.put(3, "哈哈");
map.put(4, "嘻嘻");
map.put(5, "呵呵");
// Set> entrySet = map.entrySet();
// for(Entry entry : entrySet){
// Integer key = entry.getKey();
// String value = entry.getValue();
// System.out.println(key+"="+value);
// }
//上述代码的变体,开发中常用下面这种形式
for(Entry entry : map.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
注意事项:
案例展示:
package cn.edu360;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* * @author Zhang
*要求输出结果:
* 三国演义
* 吕布
* 周瑜
* 笑傲江湖
* 令狐冲
* 林平之
* 神雕侠侣
* 郭靖
* 杨过
*/
public class ClassDemo {
public static void main(String[] args) {
HashMap> map = new HashMap>();
ArrayList list = new ArrayList();
list.add("吕布");
list.add("周瑜");
map.put("三国演义", list);
ArrayList list2 = new ArrayList();
list2.add("令狐冲");
list2.add("林平之");
map.put("笑傲江湖", list2);
ArrayList list3 = new ArrayList();
list3.add("郭靖");
list3.add("杨过");
map.put("神雕侠侣", list3);
for(Map.Entry> entry : map.entrySet()){
String key = entry.getKey();
System.out.println(key);
ArrayList value = entry.getValue();
for(String val : value){
System.out.println("\t"+val);
}
}
}
}
package cn.edu360;
import java.util.HashMap;
import java.util.Scanner;
/**
* @author Zhang
* 要求:有一个锦囊设置了三层防护,每一层都必须通过密码来获取,请想怎么设计这个程序
*/
public class ClassDemo {
public static void main(String[] args) {
//1、创建第一层防护
HashMap map = new HashMap();
map.put(110, "让孙夫人摆平东吴的追兵,她是孙权妹妹,东吴将领惧她三分");
//2、创建第二层防护
HashMap> map2 = new HashMap>();
map2.put(120, map);
//3、创建第三层防护
HashMap>> map3 = new HashMap>>();
map3.put(119, map2);
//4、拆解锦囊
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入最外层密码:");
int pwd = sc.nextInt();
HashMap> map4 = map3.get(pwd);
if(null != map4){
//解开第二层的防护
while(true){
System.out.println("请输入第二层密码:");
int pwd2 = sc.nextInt();
HashMap map5 = map2.get(pwd2);
if(null != map5){
//解开第三层防护
System.out.println("请输入最后一层密码:");
int pwd3 = sc.nextInt();
String result = map.get(pwd3);
if(null != result){
//如果代码进入这里,说明最后一层防护已经被破解
System.out.println(result);
break;
}
//第二层防护已经进来,要结束掉本层循环
break;
}
System.out.println("第二层密码输入错误,请重新输入");
}
//密码输入正确,就要结束掉这层循环
break;
}
System.out.println("密码输入错误,请重新输入");
}
}
}
注意事项:上述代码可以应用到省市区的联动中