本章目标
java.util包简介
import java.util.*;
Date类
Date类的构造方法
构 造 方 法 | 说 明 |
Date() | 使用系统当前时间创建日期对象 |
Date(long date) | 使用自1970年1月1日以后的指定毫秒数创建日期对象 |
Date(int year, int month, int date) | 创建指定年、月、日的日期对象 |
Date(int year, int month, int date, int hrs, int min, int sec) | 创建指定年、月、日、时、分、秒的日期对象 |
Date类的常用方法
方 法 原 型 | 说 明 |
boolean after(Date when) | 如果当前日期对象在when指定的日期对象之后,返回true,否则返回false |
boolean before(Date when) | 如果当前日期对象在when指定的日期对象之前,返回true,否则返回false |
void setTime(long time) | 设置日期对象,以表示自1970年1月1日起的指定毫秒数 |
boolean equals(Object obj) | 如果两个日期对象完全相同,返回true,否则返回false |
String toString() | 返回日期的格式化字符串,包括星期几 |
Date类示例
public class DateDemo
{
public static void main(String[] args)
{
Date date = new Date(); //获得当前的系统日期和时间
System.out.println("今天的日期是:" + date);
long time = date.getTime(); //获得毫秒数
System.out.println("自1970年1月1日起以毫秒为单位的时间(GMT):" + time);
//截取字符串中表示时间的部分
String strDate = date.toString();
String strTime = strDate.substring(11, (strDate.length() - 4));
System.out.println(strTime);
strTime = strTime.substring(0, 8);
System.out.println(strTime);
}
}
Calendar类
Calendar类的常用方法
方 法 原 型 | 说 明 |
Calendar getInstance() | 返回默认地区和时区的Calendar对象 |
int get(int fields) | 返回调用对象中fields指定部分的值 |
void set(int fields, int value) | 将value中指定的值设置到fields指定的部分 |
void add(int fields, int amount) | 将amount值添加到fields指定的时间或日期部分 |
Date getTime() | 返回与调用对象具有相同时间的Date对象 |
Object clone() | 返回调用对象的副本 |
void clear() | 清除当前对象中所有的时间组成部分 |
boolean after(Object obj) | 如果调用对象时间在obj之后,返回true |
boolean before(Object obj) | 如果调用对象时间在obj之前,返回true |
boolean equals(Object obj) | 判断调用对象与obj是否相等 |
Calendar类示例
public class CalendarDemo
{
public static void main(String[] args)
{
//创建包含有当前系统时间的Calendar对象
Calendar cal = Calendar.getInstance();
//打印Calendar对象的各个组成部分的值
System.out.print("当前系统时间:");
System.out.print(cal.get(Calendar.YEAR) + "年");
System.out.print((cal.get(Calendar.MONTH) + 1) + "月");
System.out.print(cal.get(Calendar.DATE) + "日 ");
System.out.print(cal.get(Calendar.HOUR) + ":");
System.out.print(cal.get(Calendar.MINUTE) + ":");
System.out.println(cal.get(Calendar.SECOND));
//将当前时间添加30分钟,然后显示日期和时间
cal.add(Calendar.MINUTE, 30);
Date date = cal.getTime();
System.out.println("将当前时间添加30分钟后的时间:" + date);
}
}
Arrays类
Random类
构 造 方 法 | 说 明 |
Random() | 直接创建一个Random类对象 |
Random(long seed) | 使用seed作为随机种子创建一个Random类对象 |
Random类的常用方法
方 法 原 型 | 说 明 |
int nextInt() | 从随机数生成器返回下一个整型值 |
long nextLong() | 从随机数生成器返回下一个长整型值 |
float nextFloat() | 从随机数生成器返回0.0到1.0之间的下一个浮点值 |
double nextDouble() | 从随机数生成器返回0.0到1.0之间的下一个双精度值 |
double nextGaussian() | 从随机数生成器返回下一个高斯分布的双精度值。中间值为0.0,而标准差为1.0 |
Random类示例
public class RandomDemo
{
public static void main(String[] args)
{
//创建一个Random类对象
Random rand = new Random();
//随机生成20个随机整数,并将其显示出来
for (int i = 0; i < 20; i++)
{
int num = rand.nextInt();
System.out.println("第" + (i + 1) + "个随机数是:" + num);
}
}
}
•问题:要生成10以内的随机数怎么做?
提示:r.nextInt(10);
Timer类
案例
public class Test2 {
public static void main(String[] args) {
Timer t = new Timer();
t.schedule(new MyTask(t), 1000);
}
}
class MyTask extends TimerTask {
private Timer t;
public MyTask(Timer t) {
this.t = t;
}
public void run() {
System.out.println("写入日志");
t.cancel();
}
}
Locale类
Locale 对象表示了特定的地理、政治和文化地区
案例
String [] cs=Locale.getISOCountries();
for(String s:cs){
System.out.print(s+"\t");
}
System.out.println();
String [] ls=Locale.getISOLanguages();
for(String s:ls){
System.out.print(s+"\t");
}
System.out.println();
NumberFormat nf=NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(nf.format(33.3));
集合
集合框架的优点
java.util包中的集合类
所以当有原始数据类型需要存放时,只能将其转换成相应的包装类对象。
ArrayList类
ArrayList类的构造方法
•ArrayList类的构造方法有3种重载方式
构 造 方 法 | 说 明 |
ArrayList() | 创建一个空的ArrayList对象 |
ArrayList(Collection c) | 根据指定的集合创建ArrayList对象 |
ArrayList(int initialCapacity ) | 使用给定的大小创建ArrayList对象 |
ArrayList类的常用方法
方 法 原 型 | 说 明 |
int size() | 返回ArrayList对象的大小,即元素的数量 |
boolean isEmpty() | 判断ArrayList对象是否为空,为空返回true,否则返回false |
void clear() | 清空ArrayList对象中的所有元素 |
boolean add(Object element) | 向ArrayList对象中添加一个元素,该元素可以是任何类的对象 |
Object remove(int index) | 从ArrayList对象中删除指定索引位置的元素 |
Object get(int index) | 返回指定索引位置的元素 |
Object set(int index, Object elem) | 将元素elem存放到由index指定的索引位置上 |
int indexOf(Object element) | 判断element在ArrayList对象中是否存在,存在返回对应的索引,否则返回-1 |
ArrayList类示例1
public class ArrayListDemo1 {
public static void main(String[] args) {
ArrayList al = new ArrayList(); //创建一个空ArrayList对象
for (int i = 0; i < 10; i++) {
Integer num = new Integer(i); //创建整型包装类对象
al.add(num); //将该对象存放到ArrayList中
}
System.out.println("数组中的元素:");
for (int i = 0; i < al.size(); i++) {
Integer temp = (Integer)(al.get(i)); //获得ArrayList中索引为i的元素
System.out.println(temp);
}
System.out.println("*********************************");
al.clear(); //清空
System.out.println("数组被清空后的情况:");
System.out.println("数组长度为:" + al.size());
if (al.isEmpty()) { //判断是否为空
System.out.println("数组现在为空。");
}
else {
System.out.println("数组现在不为空。");
}
}
}
ArrayList类示例2
public class ArrayListDemo2 {
public static void main(String[] args) {
ArrayList al = new ArrayList(); //创建一个空的ArrayList对象
//往动态数组中添加元素
al.add("苹果"); al.add("梨子"); al.add("香蕉");
al.add("西瓜"); al.add("榴莲");
System.out.println("目前数组的长度:" + al.size());
for (int i = 0; i < al.size(); i++) {
System.out.println((String)(al.get(i)));
}
String str = new String("西瓜");
int index = al.indexOf(str); //判断某个元素是否存在
if (index < 0) {
System.out.println(str + "在数组中不存在。");
} else {
System.out.println(str + "存在,索引为:" + index);
}
al.remove(3); //删除某个索引位置的元素
System.out.println("删除索引为3的元素后的情况:");
for (int i = 0; i < al.size(); i++) {
System.out.println((String)(al.get(i)));
}
}
}
LinkedList类
LinkedList类的构造方法
构 造 方 法 | 说 明 |
LinkedList() | 创建一个空链表 |
LinkedList(Collection c) | 根据指定的集合创建链表 |
LinkedList类的常用方法
方 法 原 型 | 说 明 |
int size() | 返回链表的大小,即元素的数量 |
boolean isEmpty() | 判断链表是否为空,为空返回true,否则返回false |
void clear() | 清空链表中的所有元素,使其成为空链表 |
boolean add(Object element) | 向链表中添加一个元素,该元素可以是任何类的对象 |
Object remove(int index) | 从链表中删除指定索引位置的元素 |
Object get(int index) | 返回指定索引位置的元素 |
Object set(int index, Object elem) | 将元素elem存放到由index指定的索引位置上 |
int indexOf(Object element) | 判断element在链表中是否存在,存在返回对应的索引,否则返回-1 |
LinkedList类的常用方法(续)
方 法 原 型 | 说 明 |
void addFirst(Object element) | 将指定元素添加到链表的开始处 |
void addLast(Object element) | 将指定元素添加到链表的结尾处 |
Object removeFirst() | 删除链表中的第一个元素 |
Object removeLast() | 删除链表中的最后一个元素 |
Object getFirst() | 返回链表中的第一个元素 |
Object getLast() | 返回链表中的最后一个元素 |
LinkedList类示例
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList ll = new LinkedList(); //创建空的链表
for (int i = 1; i <= 10; i++) {
Double temp = new Double(Math.sqrt(i)); //创建包装类对象
ll.add(temp); //将包装类对象添加到链表中
}
System.out.println("链表中的元素:");
//循环打印链表中的元素
for (int i = 0; i < ll.size(); i++) {
System.out.println(ll.get(i));
}
System.out.println("*********************************");
ll.removeFirst(); //删除第一个元素
ll.removeLast(); //删除最后一个元素
System.out.println("删除第一个元素和最后一个元素后的链表:");
for (int i = 0; i < ll.size(); i++) {
System.out.println(ll.get(i));
}
}
}
Vector类
Vector类的构造方法
构 造 方 法 | 说 明 |
Vector() | 创建一个空的Vector对象。初始容量为10,容量增量为0 |
Vector(Collection c) | 根据指定的集合创建Vector对象 |
Vector(int initialCapacity) | 创建一个Vector对象,初始容量由initialCapacity指定,容量增量为0 |
Vector(int initialCapacity, int capacityIncrement) | 创建一个Vector对象,初始容量由initialCapacity指定,容量增量由capacityIncrement指定 |
Vector类的常用方法
方 法 原 型 | 说 明 |
int size() | 返回Vector对象的大小,即元素的数量 |
boolean isEmpty() | 判断Vector对象是否为空,为空返回true,否则返回false |
void clear() | 清空Vector对象中的所有元素 |
boolean add(Object element) | 向Vector对象中添加一个元素,该元素可以是任何类的对象 |
Object remove(int index) | 从Vector对象中删除指定索引位置的元素 |
Object get(int index) | 返回指定索引位置的元素 |
Object set(int index, Object elem) | 将元素elem存放到由index指定的索引位置上 |
int indexOf(Object element) | 判断element在Vector对象中是否存在,存在返回对应的索引,否则返回-1 |
Vector类的常用方法(续)
方 法 原 型 | 说 明 |
int capacity() | 返回Vector对象的容量,即可以存放元素的个数 |
void addElement(Object element) | 将指定元素插入到Vector对象的末尾处 |
void insertElementAt(Object elem, int index) | 将指定元素插入到指定索引位置 |
void setElementAt(Object elem, int index) | 将指定对象替换位于指定索引处的对象 |
Object ElementAt(int index) | 检索位于指定索引处的元素 |
boolean contains(Object elem) | 如果Vector对象包含指定元素,返回true |
Object firstElement() | 返回Vector对象中的第一个元素 |
Object lastElement() | 返回Vector对象中的最后一个元素 |
void removeAllElements() | 删除Vector对象中的所有元素 |
void copyInto(Object[] anArray) | 将Vector对象中的元素复制到指定数组中 |
void setSize(int newSize) | 根据newSize的值设置Vector对象的容量 |
Vector类示例
public class VectorDemo
{
public static void main(String[] args)
{
Vector vec = new Vector(); //创建空的Vector
//往Vector中添加元素
vec.addElement("Java");
vec.addElement("C#");
vec.addElement("Oracle");
vec.addElement("C++");
vec.addElement("HTML");
System.out.println(vec.toString()); //打印Vector中的元素
vec.removeElement("C++"); //删除其中的元素
System.out.println(vec.toString());
}
}
Set接口, TreeSet, HashSet
public int compareTo(Object o){
return o==null?1:(this.id-((Student)o).id);
}
HashMap类
HashMap类的构造方法
构 造 方 法 | 说 明 |
HashMap() | 创建一个空的HashMap对象 |
HashMap(Map m) | 根据指定的Map集合创建HashMap对象 |
HashMap(int initialCapacity) | 创建一个指定容量和默认负载系数的HashMap对象 |
HashMap(int initialCapacity, float loadFactor) | 创建一个指定容量和指定负载系数的HashMap对象 |
HashMap类的常用方法
方 法 原 型 | 说 明 |
int size() | 返回HashMap对象的大小,即元素的个数 |
boolean isEmpty() | 判断HashMap对象是否为空,为空返回true,否则返回false |
void clear() | 清空HashMap对象中的所有元素 |
Object put(Object key, Object value) | 向HashMap对象中添加一个元素,必须指定该元素的键和值 |
Object remove(Object key) | 通过键对象删除相对应的值对象 |
Object get(Object key) | 通过键对象查找相对应的值对象 |
boolean containsKey(Object key) | 查找指定的键对象在HashMap对象中是否存在 |
boolean containsValue(Object value) | 查找指定的值对象在HashMap对象中是否存在 |
HashMap示例
public class HashDemo {
public static void main(String[] args) {
HashMap hm = new HashMap(); //创建空的HashMap
//往HashMap中添加元素
hm.put("93-07", "张三");
hm.put("84-12", "李四");
hm.put("102-20", "王五");
hm.put("91-04", "郑六");
hm.put("111-17", "田七");
//打印出HashMap中的元素
System.out.println(hm.toString());
//通过键对象查找值对象
System.out.println("学号91-04的学生是" + hm.get("91-04"));
System.out.println("学号84-12的学生是" + hm.get("84-12"));
hm.remove("93-07"); //通过键对象删除元素
System.out.println(hm.toString());
}
}
HashTable
Properties
Properties p=System.getProperties();
Enumeration e= p.propertyNames();
while(e.hasMoreElements()){
String key=(String) e.nextElement();
System.out.println(key+"\t"+p.getProperty(key));
}
补充Collection与Collections
总结
作业
1.
2.
3.
4.