java.util —— 工具类

工具类的存在,是为了平衡,Java 彻底的面向对象(everything is object. 的极端;),也即并非所有的东西都可抽象为类的,但又要有类的形式,工具类便由此诞生。

  • 工具类不做实例化,工具类中可调用的成员函数均为static,即通过工具类的类名即可访问;
  • 工具类的使用,避免了 new 操作,以及大量的实例化对象;

0. Random

  • private static Random rand = new Random(47);
    • rand 作为一个随机 index;(47 是该随机数的种子值)

1. java.util.Arrays

  • fill():数组填充;

    • 重载较多;
  • copyOf

    在实现 Stack 类时,每执行一次 push 的操作,需要首先判断当前所分配存储空间是否已经使用完备:

    private void ensureCapacity() {
    	if (size == elements.length) {
    		elements = Arrays.copyOf(elements, 2*size+1);
    	}
    }
    
  • asList

    List<Shape> shapeList = Arrays.asList(new Circle(), new Square(), new Triangle());
    

2. java.util.Vector

  • Vector 类的显著特性即是支持枚举(迭代);

    Vectot<ClassName> records = new Vector<>();
    Enumeration recordEnum = records.elements();
    while (recordEnum.hasMoreElements()) {
    	ClassName each = (ClassName)recordEnum.nextElement();
    }
    

    其中 Enumeration 是 java 内置接口:

    public interface Enumeration<E> {
        boolean hasMoreElements();
        E nextElement();
    }
    

3. Calendar

public boolean isBabyBoomer() {

  Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
  Date start =  gmtCal.getTime();
  gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
  Date end = gmtCal.getTime();

  return birthDate.compareTo(start) >= 0 && 
  birthDate.compareTo(end) < 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

五道口纳什

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

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

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

打赏作者

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

抵扣说明:

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

余额充值