關於Java基礎的複習總結(七)淺談java API
鳥隨鸞鳳飛騰遠,人伴賢良品自高
API(Application Programming Interface,應用程式程式設計介面)是一些預先定義的函式,目的是提供應用程式與開發人員基於某軟體或硬體得以訪問一組例程的能力,而又無需訪問原始碼,或理解內部工作機制的細節。
一:集合框架和泛型
△為什麼使用集合框架?
陣列長度固定,而集合是“動態陣列”,
如果不知道儲存多少物件,可以使用集合框架
△java集合框架提供了一套效能優良,使用方便的介面和類,位於java.util包中
父介面Collection
子介面List:儲存不唯一,有序的物件
①實現類ArrayList
實現了長度可變的陣列,遍歷元素和隨機訪問元素的效率高
add(Object)
add(index,Object)
remove(Object)
remove(index)
get(index)
clear()
isEmpty()
contains(Object)
toArray()
size()
②實現類LinkedList
採用連結串列的儲存方式,插入和刪除的效率比較高
addFirst(Object)
addLast(Object)
getFirst()
getLast()
removeFirst()
removeLast()
③子介面Set:
儲存唯一,無序的物件
實現類HashSet
實現類TreeSet
④Map介面:
儲存鍵值對,鍵不可重複,值可以重複
實現類HashMap
put()
remove()
containsKey()
containsvalue()
Set ketSet()
Collection values()
實現類TreeMap
△list遍歷方式有3種
普通for迴圈
增強for
迭代器
set遍歷方式2種
增強for
迭代器
△泛型
Hashmap<Integer,Student> map = new Hashmap();
△collections:操作集合的類
sort()
binarySearch()
max() min()
fill()
reverse()
第二章:包裝類
1、列舉型別
如果想限制性別隻能為男女,可通過兩種方式:
方式一:條件判斷
方式二:列舉
列舉是由一組固定的常量組成的型別,其優點:型別安全,易於輸入,程式碼清晰
2、包裝類java.lang
//把字串轉換成相應得基本型別
boolean flag=Boolean.parseBoolean("true"); //true
int num=Integer.parseInt("25");
//裝箱:將基本型別轉換為包裝型別
Integer n=5;
//拆箱:將包裝型別轉換為基本型別
int s=n;
3、生成0-10之間隨機數兩種方式
1、Random r=new Random();
r.nextInt(10);
2、int s=(int)(Math.random()*10);
4、String:不可變的字串
長度:length()
比較:equals() ==
大寫:toUpperCase()
小寫:toLowerCase()
忽略大小寫:equalsIngorecase()
字串連線:concat()=>+
字元第一次出現的下標:indexof()
擷取:substring(bengin,end)
拆分:split()
根據下標或字元:Char charAt(index)
去空格 trim()
5、StringBuffer:可變的字串,自動的伸縮
追加:append()
轉換:String:toString()
插入:insert(index,String str)
6、String Stringbuffer StringBuilder區別
String:字串常量
不可變物件,所以經常改變字串內容最好不要用String,
因為每次生成物件都會對系統性能產生影響
StringBuffer:字串變數
可變字串,每次改變都是對物件本身操作,而不是生成新的物件
所以經常改變字串內容推薦使用StringBuffer,執行緒安全
StringBuilder:字串變數
和StringBuffer等價,執行緒不安全,效率更高
7、日期類:java.util
Date
//格式化:日期格式-》文字格式
SimpleDateFormat formater=
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s=formater.format(date);
// 解析:文字格式-》日期格式
Date d=formater.parse(s);
Date類的增強版Calendar
Calendar c=Calendar.getInstance();
System.out.println("今天是"+c.get(Calendar.YEAR)+"年"
+(c.get(Calendar.MONTH)+1)+"月"+
c.get(Calendar.DAY_OF_MONTH)+"日,星期"+
(c.get(Calendar.DAY_OF_WEEK)-1));
Date date=c.getTime();
System.out.println(date);
第三章:I/0
檔案
File
createFile()
delete()
exists()
isFile()
isDirectory()
getPath()
getName()
位元組流
FileInputStream fis=new FileInputStream(file)
FileOutputStream fos=new FileOutputStream(file,true)
字元流
FileReader fr=new FileReader(file);
FileWriter fw=new FileWriter(file,true)