JAVA笔记 ------ 类库使用案例分析

类库使用案例分析

  • 我们先来看看这个StringBuffer类的案例:
  • 我们通过append方法向里面添加26个字母,要求每次添加一次,一共添加26次,最后要求删除前五个字符。
  • 因为我们的StringBuffer类,是允许字符被修改的,所以这个还是很简单的。
    public static void main(String[] args) {
        StringBuffer ans = new StringBuffer();
        for(int i=0;i<26;i++){
            ans.append((char)('A'+i));
        }
        ans.delete(0,5);
        System.out.println(ans);
    }

随机数组

  • 利用random类产生5个1-30之间的随机整数。要去零哦。
public class 类库使用案例分析 {
    public static void main(String[] args) {
        Random ans = new Random();
        int [] a = new int[5];
        for(int i=0;i<5;i++){
            a[i] = ans.nextInt(30)+1;
        }
        System.out.println(Arrays.toString(a));
    }
}
  • 咳咳,依旧很简单哈!!

E-mail地址认证

  • 利用正则表达式进行验证
public class 类库使用案例分析 {
    public static void main(String[] args) {
        String regex = "\\w+@\\w+.\\w+";
        Scanner ans = new Scanner(System.in);
        String a = ans.nextLine();
        System.out.println(a.matches(regex));
    }
}

翻硬币

  • 将一个硬币反复扔出1000次,看看有多少次正面,有多少次反面;
public class 类库使用案例分析 {
    public static void main(String[] args) {
        Random ans = new Random();
        int z = 0;
        int f = 0;
        for(int i=0;i<1000;i++){
            if(ans.nextInt(2) == 1){
                z++;
            }else{
                f++;
            }
        }
        System.out.println("正面有:" + z + "次、反面有" + f + "次!!!");
    }
}

IP地址判断

  • 给出一个ip地址,判断这个ip地址是否合法
class A{
    public static boolean isIp(String str){
        String regex = "\\d+{1,3}\\.\\d+{1,3}\\.\\d+{1,3}\\.\\d+{1,3}";
        if(!str.matches(regex)){
            return false;
        }
        String [] ans = str.split("\\.");
        for(int i=0;i<ans.length;i++){
            int x = Integer.parseInt(ans[i]);
            if(x>255)return false;
        }
        return true;
    }
}
public class 类库使用案例分析 {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String ans = sc.nextLine();
        System.out.println(A.isIp(ans));
    }
}

学生信息比较

  • 按照姓名、年龄、成绩的格式定义字符串张三:21:98|李四:22:89|王五:20:70,存在Student对象数组里面,然后将其进行排序。
  • 首先就是我们需要用正则表达式来将这些字符串进行拆分,然后通过实例化加入到对象数组里面去。然后通过实现Comparable接口的比较方法,然后进行对象数组排序。
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 o) {
        if(this.score > o.score){
            return 1;
        }else if(this.score < o.score){
            return -1;
        }else {
            return 0;
        }
    }

    @Override
    public String toString(){
        return "【个人信息】姓名:" + this.name + "、年龄:" + this.age + "、成绩:" + this.score + "\n";
    }

}
public class 类库使用案例分析 {
    public static void main(String[] args) {
        String input = "张三:21:98|李四:22:89|王五:20:70";
        String [] result = input.split("\\|");
        Student [] a = new Student[result.length];
        for(int i=0;i<result.length;i++){
            String [] ans = result[i].split(":");
            a[i] = new Student(ans[0],Integer.parseInt(ans[1]),Double.parseDouble(ans[2]));
        }
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
    }
}
  • 这样既可以实现字符串的拆分,还能进行有效的对象数组排序了,要想对一个对象数组进行排序,首先的方法应该就是Comparable接口里面的compareTo方法了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木木不会

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值