第7章 常用实用类

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
2016/6/4 2
7.1 Date类
2.格式化时间 Date对象表示时间的默认顺序是:星期、月、日、小时、 分、秒、年,如 Sat Apr 28 21:59:38 CST 2001 可以使用DateFormat的子类SimpleDateFormat来实现日 期的格式化。SimpleDateFormat有一个常用构造方法: public SimpleDateFormat(String pattern) 该构造方法可以用参数pattern指定的格式创建一个对 象sdf,sdf调用 public String format(Date date) 方 法格式化时间参数date指定的时间对象,format方法将 根据创建sdf对象时所使用的参数pattern返回一个字符 串对象 formatTime: String formatTime=sdf.format(new Date());
2016/6/4 12
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
Queue<Customer> expressLane = new CircularArrayQueue<Customer>(100); expressLane.add(new Customer("Harry")); Queue<Customer> expressLane = new LinkedListQueue<Customer>(); expressLane.add(new Customer("Harry"));
class LinkedListQueue <E> implements Queue<E>{ LinkedListQueue() { . . . } public void add(E element) { . . . } public E remove() { . . . } public int size() { . . . } private E[] elements; private Link head; private Link tail; }
2016/6/4 9
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
2016/6/4
10
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation Each implementation can be expressed by a class that implements the Queue interface:
2016/6/4 6
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
A queue interface specifies that you can add elements at the tail end of the queue, remove them at the head, and find out how many elements are in the queue. You use a queue when you need to collect objects and retrieve them in a "first in, first out" fashion.
2016/6/4 4
7.3
public public 大值。 public 小值。 public
1)。
Math类
Math类的常用方法:
static long abs(double a) 返回a的绝对值。 static double max(double a,double b) 返回 a 、 b 的最 static double min(double a,double b) 返回 a 、 b 的最
将日历翻到任何一个时间,当参数year取负数时表示公元前。 ◆ public int get(int field) 可以获取有关年份、月份、小时、星期等信息,参数 field 的有 效 值 由 Calendar 的 静 态 常 量 指 定 。 例 如 , calendar.get(Calendar.MONTH); 返回一个整数( 0 表示当前日历是在一月, 1 表示当前日历是在 二月等)。
2016/6/4
14
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
A circular array is somewhat more efficient than a linked list, so it is generally preferable. However, as usual, there is a price to pay. The circular array is a bounded collection, it has a finite capacity. If you don't have an upper limit on the number of objects that your program will collect, you may be better off with a linked list implementation after all.
2016/6/4
13
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
Why would you choose one implementation over another? The interface says nothing about the efficiency of the implementation.
2016/6/4 15
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
When you study the API documentation, you will find another set of classes whose name begins with Abstract, such as AbstractQueue. These classes are intended for library implementors.
static double random() 产生一个 0 到 1 之间的随机数(不包括 0 和
static static static static static double double double double double pow(double a,double b) 返回 a 的 b 次幂。 sqrt(double a) 返回a的平方根。 log(double a) 返回a的对数。 sin(double a) 返回正弦值。 asin(double a) 返回反正弦值
2016/6/4 3
7.2
Calendar类
Calendar 类 在 java.util 包 中 。 Calendar 类 的 static 方 法 getInstance()可以初始化一个日历对象,如 Calendar calendar= Calendar.getInstance(); calendar对象设置时间方法:
class CircularArrayQueue<E> implements Queue<E>{ CircularArrayQueue(int capacity) { . . . } public void add(E element) { . . . } public E remove() { . . . } public int size() { . . . } private E[] elements; private int head; private int tail; }
1.Date对象
◆Date类在java.util包中。使用Date类的无参数构造方法创建 的对象可以获取本地当前时间。 ◆ 用Date的构造方法Date(long time)创建的Date对象表示相对 1970年1月1日0点(GMT)的时间,如参数time取值60×60×1000 秒,表示Thu Jan 01 01:00:00 GMT 1970 。 ◆ System类的静态方法 public long currentTimeMillis()可 以获取系统当前时间,这个时间是从1970年1月1日0点(GMT)到 目前时刻所走过的毫秒数(这是一个不小的数)。 可以根据currentTimeMillis()方法得到的数字,用Date的构 造方法Date(long time)来创建一个本地日期的Date对象。
2016/6/4 7
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
2016/6/4
8
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
2016/6/4 11
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation Each implementation can be expressed by a class that implements the Queue interface:
A minimal form of a queue interface might look like this:
interface Queue<E> { void add(E element); E remove(); int size(); }
The interface tells you nothing about how the queue is implemented.
◆public final void set(int year,int month,int date) ◆public final void set(int year,int month,int date,int hour,int minute) ◆public final void set(int year,int month,int date,int hour,int minute,int second)
5
public public public public public
20Hale Waihona Puke Baidu6/6/4
7.4 Collection-JAVA集合专题
Separating Collection Interfaces and Implementation
As is common for modern data structure libraries, the Java collection library separates interfaces and implementations. Let us look at that separation with a familiar data structure, the queue.
第7章 常用实用类
Date类和Calendar类 Math类与BigInteger类 LinkedList<E>泛型类 HashSet<E>泛型类 TreeSet<E>泛型类 HashMap<K,V>泛型类 TreeMap<K,V>泛型类 Stack<E>泛型类
2016/6/4
1
7.1 Date类
相关文档
最新文档