第14 章 : 类库使用案例分析
59 StringBuffer使用
使用StringBuffer追加26个小写字母。逆序输出,并删除前5个字符
StringBuffer允许修改 String不允许修改
StringBuffer buff = new StringBuffer(); for(int i = 'a'; i<='z'; i++){ buff.append((char)i); } System.out.println(buff.reverse().delete(0, 5)); // utsrqponmlkjihgfedcba
60 随机数组
Rondom 产生5个[1, 30]之间随机数
import java.util.Arrays; import java.util.Random; class NumberFactory{ private static Random random = new Random(); public static int[] getRandomList(int num){ int[] list = new int[num]; int foot = 0; while (foot < num) { int value = random.nextInt(31); if (value !=0 ){ list[foot++] = value; } } return list; } } class Demo{ public static void main(String[] args) { int[] list = NumberFactory.getRandomList(5); System.out.println(Arrays.toString(list)); // [27, 3, 9, 4, 12] } }
61 Email验证
class Validator{ public static boolean isEmail(String email){ if(email == null || "".equals(email)){ return false; } String regex = "\\w+@\\w+\\.\\w+"; return email.matches(regex); } } class Demo{ public static void main(String[] args) { System.out.println(Validator.isEmail("ooxx@qq.com")); // true } }
62 扔硬币
0-1随机数模拟投掷硬币 1000次
import java.util.Random; class Coin{ private int front; private int back; private Random random = new Random(); public void throwCoin(int num){ for (int i = 0; i < num; i++) { int value = random.nextInt(2); if (value == 0){ this.front ++; } else{ this.back ++; } } } public int getFront() { return this.front; } public int getBack() { return this.back; } } class Demo{ public static void main(String[] args) { Coin coin = new Coin(); coin.throwCoin(1000); System.out.println("正面: " + coin.getFront()); System.out.println("背面: " + coin.getBack()); // 正面: 495 // 背面: 505 } }
63 IP验证
eg: 127.0.0.1
第一位 [12]?
第二位 [0-9]{0, 2}
import java.util.Random; class Validator { public static boolean isIp(String ip) { String regex = "(\\d{1,3}\\.){3}\\d{1,3}"; if (!ip.matches(regex)) { return false; } String[] list = ip.split("\\."); for (String str : list) { int num = Integer.parseInt(str); if (num > 255 || !str.equals(Integer.toString(num))) { return false; } } return true; } } class Demo { public static void main(String[] args) { System.out.println(Validator.isIp("127.0.0")); // false System.out.println(Validator.isIp("127.0.0.1")); // true System.out.println(Validator.isIp("255.255.255.255")); // true System.out.println(Validator.isIp("255.255.255.666")); // false System.out.println(Validator.isIp("255.255.001.1")); // false } }
64 HTML拆分
<font face="Arial,Serif" size="+2" color="red"></font>
import java.util.regex.Matcher; import java.util.regex.Pattern; class Demo { public static void main(String[] args) { String html = "<font face=\"Arial,Serif\" size=\"+2\" color=\"red\"></font>"; String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\""; Matcher matcher = Pattern.compile(regex).matcher(html); while (matcher.find()){ String temp = matcher.group(0); String[] result = temp.split("="); System.out.println(result[0] + "\t" + result[1].replaceAll("\"", "")); /** * face Arial,Serif * size +2 * color red */ } } }
65 国家代码
实现国际化应用
输入国家代号,调用资源文件
3个资源文件
# message.properties info=默认资源 # message_en_US.properties info=英文资源 # message_zh_CN.properties info=中文资源
import java.io.UnsupportedEncodingException; import java.util.Locale; import java.util.ResourceBundle; class MessageUtil { // 将固定的内容定义为常量 private static final String CHINA = "cn"; private static final String ENGLISH = "en"; private static final String BASENAME = "message"; private static final String KEY = "info"; public static String getMessage(String country) throws UnsupportedEncodingException { Locale locale = getLocale(country); if (locale == null) { return null; } else { ResourceBundle bundle = ResourceBundle.getBundle(BASENAME, locale); return new String(bundle.getString(KEY).getBytes("ISO-8859-1"), "utf-8"); } } private static Locale getLocale(String country) { switch (country) { case CHINA: return new Locale("zh", "CN"); case ENGLISH: return new Locale("en", "US"); default: return null; } } } class Demo { public static void main(String[] args) throws UnsupportedEncodingException { if (args.length < 1) { System.out.println("请输入:cn 或者 en"); System.exit(1); } System.out.println(MessageUtil.getMessage(args[0])); // 中文资源 } }
66 学生信息比较
先用成绩比较,如果相同按年龄比较
数据结构
姓名:年龄:成绩|姓名:年龄:成绩 eg: 张三:21:98|李四:23:96|王五:24:94
结构化的字符串处理
import java.io.UnsupportedEncodingException; import java.util.Arrays; class Student implements Comparable<Student>{ private String name; private int age; private double score; public Student(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } @Override public int compareTo(Student other) { // 先用成绩比较,再用年龄比较 if(this.score > other.score){ return 1; } else if (this.score < other.score){ return -1; } else{ return this.age - other.age; } } @Override public String toString() { return "Student{" + name + ',' + age + ", " + score + "}"; } } class Demo { public static void main(String[] args) throws UnsupportedEncodingException { String data = "张三:21:98|李四:23:96|王五:24:94"; String[] list = data.split("\\|"); Student[] students = new Student[list.length]; for (int i = 0; i < list.length; i++) { String[] temp = list[i].split(":"); students[i] = new Student(temp[0], Integer.parseInt(temp[1]), Double.parseDouble(temp[2])); } Arrays.sort(students); System.out.println(Arrays.toString(students)); // [Student{王五,24, 94.0}, Student{李四,23, 96.0}, Student{张三,21, 98.0}] } }