1.Calendar类java
建立Calendar对象:编程
Calendar calendar=Calendar.getInstace();数组
calendar对象的方法:dom
public final void set(int year,int month,int date,int hour,int minute,int second)ide
将日历翻到任何一个时间,当参数year取负数时表示公元前。对象
calendar.get(Calendar.MONTH)接口
返回一个整数(0表示当前日历是在一月)three
calendar.get(Calendar.DAY_OF_WEEK)-1ip
返回一个整数(0表示礼拜日)rem
例1:计算1931年9月18日和1945年8月15日之间相隔的天数。
import java.util.*;
public class Example{
public static void main(String args[ ]){
Calendar calendar=Calendar.getInstance(); //建立一个日历对象
calendar.setTime(new Date()); //用当前时间初始化日历时间
String 年=String.valueOf(calendar.get(Calendar.YEAR)),
月=String.valueOf(calendar.get(Calendar.MONTH)+1),
日=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),
星期=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1);
int hour=calendar.get(Calendar.HOUR_OF_DAY),
minute=calendar.get(Calendar.MINUTE),
second=calendar.get(Calendar.SECOND);
System.out.println("如今的时间是:");
System.out.print(""+年+"年"+月+"月"+日+"日 "+ "星期"+星期);
System.out.println(" "+hour+"时"+minute+"分"+second+"秒");
calendar.set(1931,8,18); //将日历翻到1931年九月十八日,8表示九月
long timeOne=calendar.getTimeInMillis();
calendar.set(1945,7,15); //将日历翻到1945年八月十五日,7表示八月
long timeTwo=calendar.getTimeInMillis();
long 相隔天数=(timeTwo-timeOne)/(1000*60*60*24);
System.out.println("1945年8月15日和1931年9月18日相隔"+相隔天数+"天");
}
}
例2.输出1931.9.18日历
import java.util.*;
public class Example{
public static void main(String args[ ]){
Calendar 日历=Calendar.getInstance();
日历.set(1931,8,1); //8表明九月
int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
String a[]=new String[星期几+30]; //存放号码的一维数组
for(int i=0;i<星期几;i++){
a[i]="";
}
for(int i=星期几,n=1;i<星期几+30;i++){
a[i]=String.valueOf(n) ;
n++;
}
int year=日历.get(Calendar.YEAR),
month=日历.get(Calendar.MONTH)+1;
System.out.println(" "+year+"年"+month+"月"+"18日,日本发动侵华战争");
System.out.printf("%4c%4c%4c%4c%4c%4c%4c\n",'日','一','二', '三','四','五','六');
for(int i=0;i<a.length;i++){
if(i%7==0&&i!=0)
System.out.printf("\n");
System.out.printf("%5s",a[i]);
}
}
}
2.Math类
public static long abs(double a) 返回a的绝对值。
public static double max(double a,double b) 返回a、b的最大值。
public static double min(double a,double b) 返回a、b的最小值。
public static double random()产生一个0到1之间的随机数(不包括0和1)。
public static double pow(double a,double b) 返回a的b次幂。
public static double sqrt(double a) 返回a的平方根。
public static double log(double a) 返回a的对数。
public static double sin(double a) 返回正弦值。
public static double asin(double a) 返回反正弦值
两个静态常量E和PI,它们的值分别是2.7182828284590452354和3.14159265358979323846。
3.BigInteger类
public BigInteger(String val)
经常使用方法:
public BigInteger add(BigInteger val) 返回当前大整数对象与参数指定的大整数对象的和。
public BigInteger subtract(BigInteger val) 返回当前大整数对象与参数指定的大整数对象的差。
public BigInteger multiply(BigInteger val) 返回当前大整数对象与参数指定的大整数对象的积。
public BigInteger divide(BigInteger val) 返回当前大整数对象与参数指定的大整数对象的商。
public BigInteger remainder(BigInteger val) 返回当前大整数对象与参数指定的大整数对象的余。
public int compareTo(BigInteger val) 返回当前大整数对象与参数指定的大整数的比较结果,返回值是一、-1或0,分别表示当前大整数对象大于、小于或等于参数指定的大整数。
public BigInteger abs()返回当前大整数对象的绝对值。
public BigInteger pow(int exponent) 返回当前大整数对象的exponent次幂。
public String toString()返回当前大整数对象十进制的字符串表示。
public String toString(int p) 返回当前大整数对象p进制的字符串表示。
例:计算两个大整数的和、差、积、商,并计算一个大整数的因子个数。
import java.math.*;
public class Example{
public static void main(String args[]){
BigInteger n1=new BigInteger("987654321987654321987654321"),
n2=new BigInteger("123456789123456789123456789"),
result=null;
result=n1.add(n2);
System.out.println(n1+"+"+n2+"=");
System.out.println(result);
result=n1.subtract(n2);
System.out.println(n1+"-"+n2+"=");
System.out.println(result);
result=n1.multiply(n2);
System.out.println(n1+"*"+n2+"=");
System.out.println(result);
result=n1.divide(n2);
System.out.println(n1+"/"+n2+"=");
System.out.println(result);
BigInteger m=new BigInteger("77889988"),
COUNT=new BigInteger("0"),
ONE=new BigInteger("1"),
TWO=new BigInteger("2");
for(BigInteger i=TWO;i.compareTo(m)<0;i=i.add(ONE)){
if((n1.remainder(i).compareTo(BigInteger.ZERO))==0){
COUNT=COUNT.add(ONE);
System.out.println(m+"的因子:"+i);
}
}
System.out.println(m+"一共有"+COUNT+"个因子");
}
}
4.LinkedList<E>泛型类
使用该泛型类能够建立链表结构的数据对象。
构造:
LinkedList<String> mylist=new LinkedList<String>();
遍历链表:
能够借助泛型类Iterator<E>实现便利链表。
Iterator<Student> iter=mylist.iterator();
while(iter.hasNext()){
Student te=iter.next();
System.out.println(te.name+" "+te.number+" "+te.score);
}
LinkedList<E>泛型类实现的接口:
LinkedList<E>泛型类实现了泛型接口List<E>,而List<E>接口是Collection<E>接口的子接口。
编程时,能够使用接口回调技术,即把LinkedList<E>对象的引用赋值给Collection<E>接口变量或List<E>接口变量,则接口就能够实现调用类实现的接口方法。
5.HashSet<E>泛型类
HashSet<E>泛型类建立的对象称为集合。
构造方法:HashSet<String>set=HashSet<String>();
使用方法:
public boolean add(E o) 向集合添加参数指定的元素。
public void clear() 清空集合,使集合不含有任何元素。
public boolean contains(Object o) 判断参数指定的数据是否属于集合。
public boolean isEmpty() 判断集合是否为空。
public boolean remove(Object o) 集合删除参数指定的元素。
public int size() 返回集合中元素的个数。
Object[] toArray() 将集合元素存放到数组中,并返回这个数组。
boolean containsAll(HanshSet set) 判断当前集合是否包含参数指定的集合。
public Object clone() 获得当前集合的一个克隆对象,该对象中元素的改变不会影响到当前集合中元素,反之亦然。
集合的交并差运算:
集合对象调用boolean addAll(HashSet set)方法能够和参数指定的集合求并运算,使得当前集合成为两个集合的并。
集合对象调用boolean boolean retainAll (HashSet set)方法能够和参数指定的集合求交运算,使得当前集合成为两个集合的交。
集合对象调用boolean boolean boolean removeAll (HashSet set)方法能够和参数指定的集合求差运算,使得当前集合成为两个集合的差 参数指定的集合必须和当前集合是同种类型的集合,不然上述方法返回false。
例:求两个集合的对称差集合
import java.util.*;
public class Example{
public static void main(String args[]){
Integer one=new Integer(1),
two=new Integer(2),
three=new Integer(3),
four=new Integer(4),
five=new Integer(5),
six=new Integer(6);
HashSet<Integer> A=new HashSet<Integer>(),
B=new HashSet<Integer>(),
tempSet=new HashSet<Integer>();
A.add(one);
A.add(two);
A.add(three);
A.add(four);
B.add(one);
B.add(two);
B.add(five);
B.add(six);
tempSet=(HashSet<Integer>)A.clone();
A.removeAll(B); //A变成调用该方法以前的A集合与B集合的差集
B.removeAll(tempSet); //B变成调用该方法以前的B集合与tempSet集合的差集
B.addAll(A); //B就是最初的A与B的对称差
int number=B.size();
System.out.println("A和B的对称差集合有"+number+"个元素:");
Iterator<Integer> iter=B.iterator();
while(iter.hasNext()){
Integer te=iter.next();
System.out.printf("%d,",te.intValue());
}
}
}
HashSet<E>泛型类实现的接口:
HashSet<E>泛型类实现了泛型接口Set<E>,而Set<E>接口是Collection<E>接口的子接口。
6.HashMap<K,V>泛型类
HashMap<K,V>泛型类建立的对象称为散列映射。
构造:
HashMap<String,Student>hashtable=HashMap<String,Student>();
可经过调用:
public v put(K key,V value)将键/值对数据存放到散列映射中,该方法同时返回键所对应的值。
遍历散列映射:
Collection<Book> collection=table.values();
Iterator<Book> iter=collection.iterator();
while(iter.hasNext()){
Book te=iter.next();
System.out.printf("书名:%s,ISBN:%s\n",te.name,te.ISBN);
HashMap<E>泛型类实现的接口:
HashMap<E>泛型类实现了泛型接口Map<E>,HashMap<E>类中的绝大部分方法都是Map<E>接口方法的实现。编程时,能够使用接口回调技术,即把HashMap<E>对象的引用赋值给Map<E>接口变量,那么接口就能够调用类实现的接口方法。