馬士兵java教程筆記4

File 類java

Java.io.File 類表明系統文件名安全

File類的常見構造方法:spa

Public File(String pathname)以pathname爲路徑建立File對象,若是pathname是相對路徑,則默認當前路徑在系統屬性指針

public File(String parent, String child)以parent爲父路徑,child爲子路徑建立File對象code

File的靜態屬性String separator存儲了當前系統的路徑分隔符對象

File 經常使用方法blog

經過File對象能夠訪問文件的屬性接口

public boolean canRead();element

public boolean canWrite();rem

public boolean exists();

public boolean isDirctory();

public boolean isFile();

public boolean isHidden();

public long lastMOdified();//上次修改的時間

public long length();

public String getName();

public String getPath();

經過File對象建立空文件或目錄(在該對象所指的文件或目錄不存在的狀況下)

public boolean creatNewFile() throws IOException

public boolean delete()

public booean mkdir();

public boolean mkdirs();

 

枚舉類型 enum

枚舉類型:1 只可以取特定值中的一個

2.使用enum關鍵字

3.是java.lang.Enum類型

 

Collection

java.util

Collection接口定義了存取一組對象的方法,其子接口Set和List分別定義了存儲方式

  • Set中的數據對象沒有順序且不能夠重複
  • List中的數據對象有順序且能夠重複

Map接口定義了存儲 key-value

int size();

boolean isEmpty();

void clear(); 

boolean contains(Object element);

boolean add(Object element);

boolean remove(Object element);

Iterator iterator();

boolean containAll(Collection c);

boolean addAll(Collection c);

boolean removeAll(Collection c);

boolean retainAll(Collection c);

Object [] toArray();

在remove Object的過程當中,要重寫equals

容器類對象在調用remove、contains等方法時須要比較對象是否相等,這會涉及到對象類型的equals方法和hashcode方法;對於自定義的類型,須要重寫equals 和 hashCode方法以實現自定義的對象相等規則。

public boolean equals(Object obj){

if ( obj instanceof Name ){

Name name = (Name) obj;

return (firstName.equals(name.firstName))&&(lastName.equals(name.lastName));

}

return super.equals(obj);

p.s 相等的對象應該具備相等的hashcodes

重寫equals 方法 必須重寫hashCode方法

Iterator

全部實現了Collection 接口的容器類都有一個iterator方法用以返回一個實現了Iterator接口的對象

boolean hasNext() // return true if the iteration has more elements

Object next() returns the next element in the iteration

void remove() removes from the underlying collection the last element returned by the iterator

父類(Iterator) 指向子類對象 (Collection)

Iterator 至關於一個指針 可是不一樣的collection 有不一樣的實現

Iterator對象的remove方法是在迭代過程當中刪除元素惟一的安全方法

相關文章
相關標籤/搜索