专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

类库使用案例

StringBuffer

使用StringBuffer对象中得append()方法添加26个小写字母,要求每次只能添加一个,共添加26次,然后按照逆序方式输出,最后删除前5个数字。

public class JavaAPIDemo {
    public static void main(String[] args) {
        StringBuffer sbf = new StringBuffer();
        for (int i = 'a'; i <= 'z'; i++) {
            sbf.append((char)i);//添加字母
        }
        sbf.reverse();//数据反转
        sbf.delete(0,5);//删除
        System.out.println(sbf);
    }
}

运行结果:
utsrqponmlkjihgfedcba

Random

  • 使用Random类产生指定数量得,1~30之间(包括1和30)得随机数
import java.util.Arrays;
import java.util.Random;

public class JavaAPIDemo {
    public static void main(String[] args) {
        System.out.println("方法一:"+Arrays.toString(createRandom(5)));
        System.out.println("方法二:"+Arrays.toString(createRandom2(5)));
    }

    /**
     * Math.random()产生一个[0,1)之间的随机数
     * @param len
     * @return
     */
    public static int[] createRandom(int len){
        int[] data = new int[len];//需要得数据的个数
        for (int x = 0; x < len; x++) {
            data[x] = (int) (Math.random()*30+1);
        }
        return data;
    }

    /**
     * Random创建的对象产生一个[0,x)之间的随机数,x为要创建随机数的范围
     * @param len 传入的数据
     * @return
     */
    public static int[] createRandom2(int len){
        int[] data = new int[len];//需要得数据的个数
        Random random = new Random();
        for (int x = 0; x < len; x++) {
            data[x] = random.nextInt(30)+1;
        }
        return data;
    }
}
运行结果:
方法一:[24, 2, 30, 18, 29]
方法二:[15, 21, 1, 2, 19]

  • 编写程序,用0~1之间的随机数来模拟扔硬币实验,统计扔1000次后出现正、反面的次数并输出。
import java.util.Arrays;
import java.util.Random;

public class JavaAPIDemo {
    public static void main(String[] args) {
        //产生1000个随机数
        int[] random = createRandom(1000);//产生1000个随机数,1表示硬币正面
        int[] res = new int[2];//保存0和1的数
        for (int tem:random) {
            res[tem]++;
        }
        System.out.println(Arrays.toString(res));
    }

    /**
     * Random创建的对象产生一个[0,x)之间的随机数,x为要创建随机数的范围
     * @param len 传入的数据
     * @return
     */
    public static int[] createRandom(int len){
        int[] data = new int[len];//需要得数据的个数
        Random random = new Random();
        for (int x = 0; x < len; x++) {
            data[x] = random.nextInt(2);//只能产生0和1两个随机数
        }
        return data;
    }
}

为了提高程序的可扩展性,可以将方法重新定义到一个类当中

import java.util.Random;

public class JavaAPIDemo {
    public static void main(String[] args) {
       Coin.throwCoin(1000);
        System.out.println("正面出现的次数:"+Coin.getPositive());
        System.out.println("背面出现的次数:"+Coin.getBack());
    }
}
class Coin{
    private static int positive;//硬币正面
    private static int back;//硬币背面
    private Coin(){}

    /**
     * 扔硬币
     * @param number 扔硬币的次数
     */
    public static void throwCoin(int number){
        Random random = new Random();
        for (int x = 0; x < number; x++) {
            if (random.nextInt(2) == 0) {
                back++;
            } else {
                positive++;
            }
        }

    }

    public static int getPositive() {
        return positive;
    }

    public static int getBack() {
        return back;
    }
}

运行结果:
正面出现的次数:504
背面出现的次数:496

正则表达式

  • 验证邮箱格式
public class JavaAPIDemo {
    public static void main(String[] args) {
        String email = "2222@qq.cn1";
        System.out.println(verifyEmail.isEmail(email));

    }
}
class verifyEmail{
    private verifyEmail(){}//对象私有化
    public static boolean isEmail(String email){
        if (email ==null || "".equals(email))
            return false;
        String regex  = "[a-zA-Z0-9]\\w+@\\w+\\.(com|cn|net|gov)";
        return email.matches(regex);
    }
}

  • 验证ip地址
public class JavaAPIDemo {
    public static void main(String[] args) {
        String ip1 = "255.255.255.255";
        System.out.println(Validator.validateIP(ip1));
        String ip2 = "299.299.299.299";
        System.out.println(Validator.validateIP(ip2));
    }
}
class Validator{
   public static boolean validateIP(String ip){
       if (ip == null || "".equals(ip)){
           return false;
       }
       String regex = "([12]?[0-9]?[0-9]\\.){3}([12]?[0-9]?[0-9])";
       if (ip.matches(regex)){//验证通过
           String[] result = ip.split("\\.");//拆分数据
           for (int x = 0; x < result.length; x++) {
               int temp = Integer.parseInt(result[x]);
               if (temp>255){
                   return false;
               }
           }
           return true;
       }else {
           return false;
       }
   }
}

运行结果:

true
false

  • Html拆分处理

拆分:<font face=\"Arial,Serif\" size=\"+2\" color=\"red\">

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

public class JavaAPIDemo {
    public static void main(String[] args) {
        String str = "<font face=\"Arial,Serif\" size=\"+2\" color=\"red\">'";
        String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\"";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()){
            String temp = matcher.group(0);
            String[] res = temp.split("=");
            System.out.println(res[0]+" "+res[1].replaceAll("\"",""));
        }
    }
}
运行结果:
face Arial,Serif
size +2
color red

文章永久链接:https://tech.souyunku.com/18967

未经允许不得转载:搜云库技术团队 » 类库使用案例

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们