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>接口變量,那麼接口就能夠調用類實現的接口方法。