Java学习路线-24:类库使用案例StringBuffer、Rondom、ResourceBundle、regex、Comparable

简介: Java学习路线-24:类库使用案例StringBuffer、Rondom、ResourceBundle、regex、Comparable

第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}]
    }
}


相关文章
|
SQL XML Oracle
数据库中间件DBLE学习(一) 基本介绍和快速搭建
dble基本架构简介 dble是基于mysql的高可用扩展性的分布式中间件。江湖人送外号MyCat Plus。开源地址 我们首先来看架构图,外部应用通过NIO/AIO进行连接操作。
4715 106
|
存储 Cloud Native 关系型数据库
阿里云PolarDB、RDS获评信通院数据库Serverless认证最高“先进级”,AnalyticDB获“增强级”
在日前中国信通院组织的数据库产品能力评测中,阿里云PolarDB for MySQL、RDS MySQL数据库顺利完成了首批事务型数据库Serverless能力分级测试,获最高“先进级”评级;AnalyticDB MySQL和AnalyticDB PostgreSQL顺利完成了首个分析型数据库Serverless能力分级测试,获评“增强级”评级。
阿里云PolarDB、RDS获评信通院数据库Serverless认证最高“先进级”,AnalyticDB获“增强级”
|
算法
实现人脸美白算法---OpenCV-Python开发指南(59)
实现人脸美白算法---OpenCV-Python开发指南(59)
1012 0
实现人脸美白算法---OpenCV-Python开发指南(59)
|
Oracle IDE Java
Java 云原生之路:Micronaut 框架
本文是“Native Compilations Boosts Java”系列文章的一部分。你可以通过订阅RSS接收更新通知。
1104 0
Java 云原生之路:Micronaut 框架
|
计算机视觉
语义特征的理解
再讲语义特征之前,先将语义的概念讲一下。那么什么是语义呢?数据的含义就是语义(semantic)。简单来说,数据就是符号。数据本身没有任何意义,只有被赋予含义的数据才能够被使用,否则就是一堆没用的数字或载体。这时候,被赋予含义的数据就转化为了信息,而转化为信息的数据便是语义,即数据的含义就是语义。语义可以简单地看作是数据所对应的现实世界中的事物所代表的概念的含义,以及这些含义之间的关系,是数据在某个领域上的解释和逻辑表示。在计算机视觉中,大家经常会提起图像的语义信息以及图像的高层特征和底层特征。
733 0
|
分布式计算 Java Hadoop
zookeeper的安装与部署
zookeeper的安装与部署
|
人工智能 Oracle 大数据
31%和187%,神州数码找到云转型路径
31%和187%,神州数码找到云转型路径
422 0
31%和187%,神州数码找到云转型路径
|
监控 Java 数据挖掘
教你玩转友盟应用性能监控U-APM平台
友盟推出了全新的应用性能监控平台 U-APM,U-APM 可以帮助我们深入了解应用的性能和稳定性,帮助我们高效提升应用的质量。通过实时采集新版本上线后的崩溃信息,提供了多种辅助定位问题的关键信息及多维度分析报表,从而能够快速发现问题、定位问题、解决问题。
教你玩转友盟应用性能监控U-APM平台
|
JavaScript 前端开发 Shell
最适合入门的Laravel中级教程Passport OAuth认证
最适合入门的Laravel中级教程Passport OAuth认证
446 0
|
5G 调度 数据安全/隐私保护
5G NR SIB1介绍
SIB1携带UE接入小区所需的最关键的信息,例如随机接入参数。SIB1包括关于其他SIB的可用性和调度的信息,例如,其他SIB。
2123 0