java.util —— 工具类
2016-03-09 16:16
615 查看
工具类的存在,是为了平衡,Java 彻底的面向对象(everything is object. 的极端;),也即并非所有的东西都可抽象为类的,但又要有类的形式,工具类便由此诞生。
工具类不做实例化,工具类中可调用的成员函数均为static,即通过工具类的类名即可访问;
工具类的使用,避免了 new 操作,以及大量的实例化对象;
rand 作为一个随机 index;(47 是该随机数的种子值)
在实现 Stack 类时,每执行一次 push 的操作,需要首先判断当前所分配存储空间是否已经使用完备:
asList
其中 Enumeration 是 java 内置接口:
工具类不做实例化,工具类中可调用的成员函数均为static,即通过工具类的类名即可访问;
工具类的使用,避免了 new 操作,以及大量的实例化对象;
0. Random
private static Random rand = new Random(47);rand 作为一个随机 index;(47 是该随机数的种子值)
1. java.util.Arrays
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; }
相关文章推荐
- Java枚举类型
- Spring 静态资源配置
- Java - Hash - hashCode()与equal()
- Java 排序Comparator的实践
- spring mvc 避免IE执行AJAX时,返回JSON出现下载文件
- JAVA的String 类【转】
- spring和jdbc结合的一个小例子
- spring和jdbc结合的一个小例子
- Spring线程池的使用demo
- Java 读取控制台输入
- 如何在Eclipse中连接Android手机?
- 什么是java序列化,如何实现java序列化?或者请解释Serializable接口的作用。
- 使用JDK中JAXBContext对JavaBean和xml相互转换
- Java多线程学习(八)
- 深入浅出RxJava三--响应式的好处
- 踩坑事件:windows操作系统下的eclipse中编写SparkSQL不能从本地读取或者保存parquet文件
- 深入浅出RxJava(二:操作符)
- Java中String类的方法及说明
- org.w3c.dom(java dom)解析XML文档
- 自己学Java历程