java学习材料

来源:互联网 发布:怎样玩游戏挣钱知乎 编辑:程序博客网 时间:2024/04/29 19:54

public class ContactInfo {
 public String firstName = "";
 public String endName = "";
 public String number = "";
 
 public ContactInfo(String f,String e,String n){
  this.firstName = f;
  this.endName = e;
  this.number = n;
 }
}

 

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestMain {
 static ArrayList<ContactInfo> all = new ArrayList<ContactInfo>();
 
 public static void main(String[] args) {
  
  System.out.println('A'+0);
  
  ContactInfo c1 = new ContactInfo("xu","heng","13786765433");
  ContactInfo c2 = new ContactInfo("aZ","xuli","18616155153");
  ContactInfo c3 = new ContactInfo("bea","gtet","18616155153");
  all.add(c1);
  all.add(c2);
  all.add(c3);
  
  //String numKeyStr = "15555555555";
  String numKeyStr = "18";
  Pattern p = Pattern.compile("[0-9]{1,11}"); 
  Matcher m = p.matcher(numKeyStr);
  if(!m.matches()){
   System.out.println("号码关键字错误!");
   return;
  }
  ArrayList<ContactInfo> numKeyList = getContactByNumber(numKeyStr);
  System.out.println("电话包含15的联系人:");
  printString(numKeyList);
  
  String nameKeyStr = "aZ";
  p = Pattern.compile("^[a-zA-Z]*"); 
  m = p.matcher(nameKeyStr);
  if(!m.matches()){
   System.out.println("名字关键字错误!");
   return;
  }
  numKeyList = getContactByNameKey(nameKeyStr);
  System.out.println("名字包含xu的联系人:");
  printString(numKeyList);
  nameKeyStr = "b";
  numKeyList = getContactByNameKey(nameKeyStr);
  System.out.println("名字包含b的联系人:");
  printString(numKeyList);
  
  String startKey = "02";
  if (startKey.length()!=2){
   System.out.println("打头关键字错误!");
   return;
  }
  System.out.println("头为"+startKey+"的联系人");
  numKeyList = getContactByStartKey(startKey);
  printString(numKeyList);
  
 }
 
 public static boolean isMobileNO(String mobiles){ 
  Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); 
  Matcher m = p.matcher(mobiles);
  return m.matches();
 } 
 
 public static ArrayList<ContactInfo> getContactByStartKey(String startKey){
  int key1 = Integer.parseInt(startKey.substring(0, 1));
  int key2 = Integer.parseInt(startKey.substring(1, 2));
  ArrayList<ContactInfo> tmp = new ArrayList<ContactInfo>();
  ContactInfo ci;
  for(int i=0; i<all.size();i++){
   ci = all.get(i);
   for(int j=0;j<3;j++){
    if (ci.firstName.startsWith(String.valueOf((char)('a'+key1*3+j)))){
     for(int k=0;k<3;k++){
      if (ci.endName.startsWith(String.valueOf((char)('a'+key2*3+k)))){
       tmp.add(ci);
      }
     }
    }
    else if(ci.endName.startsWith(String.valueOf((char)('a'+key1*3+j)))){
     for(int k=0;k<3;k++){
      if (ci.firstName.startsWith(String.valueOf((char)('a'+key2*3+k)))){
       tmp.add(ci);
      }
     }
    }
   }
  }
  return tmp;
 }
 
 
 public static ArrayList<ContactInfo> getContactByNameKey(String nameKey){
  ArrayList<ContactInfo> tmp = new ArrayList<ContactInfo>();
  ContactInfo ci;
  for(int i=0; i<all.size();i++){
   ci = all.get(i);
   if (ci.firstName.startsWith(nameKey)||ci.endName.startsWith(nameKey)){
    tmp.add(ci);
   }
  }
  return tmp;
 }
 
 public static ArrayList<ContactInfo> getContactByNumber(String numKey){
  ArrayList<ContactInfo> tmp = new ArrayList<ContactInfo>();
  ContactInfo ci;
  for(int i=0; i<all.size();i++){
   ci = all.get(i);
   //System.out.println(isMobileNO(ci.number));
   if (ci.number.startsWith(numKey)){
    tmp.add(ci);
   }
  }
  return tmp;
 }
 
 public static void printString(ArrayList<ContactInfo> ciList){
  ContactInfo ci;
  for(int i=0; i<ciList.size();i++){
   ci = ciList.get(i);
   System.out.println("First:"+ci.firstName+" End:"+ci.endName+" Number:"+ci.number);
  }
 }

}

/*
 正则表达式--验证手机号码:13[0-9]{9}
实现手机号前带86或是+86的情况:^((\+86)|(86))?(13)\d{9}$
电话号码与手机号码同时验证:(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9}) 
提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F)  *=  *('|")?(\w|\\|\/|\.)+('|"|  *|>)?  
提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*  
提取信息中的图片链接:(s|S)(r|R)(c|C)  *=  *('|")?(\w|\\|\/|\.)+('|"|  *|>)?
提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)    
提取信息中的中国手机号码:(86)*0*13\d{9}    
提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}    
提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}    
提取信息中的中国邮政编码:[1-9]{1}(\d+){5}    
提取信息中的中国身份证号码:\d{18}|\d{15}    
提取信息中的整数:\d+    
提取信息中的浮点数(即小数):(-?\d*)\.?\d+    
提取信息中的任何数字  :(-?\d*)(\.\d+)?  
提取信息中的中文字符串:[\u4e00-\u9fa5]*    
提取信息中的双字节字符串  (汉字):[^\x00-\xff]*
*/

原创粉丝点击