2013-11-25 95 views
0

的返回類型爲查找(ID)方法,爲什麼系統始終堅持認爲需要教練的返回類型,我想我已經聲明, 「入口」是指導教師。對於查找(ID)方法,爲什麼系統,總是堅持要求教師

// 1. TO DO 
/** findID(String) is passed an id and returns the 
* Instructor object in the Instructor arraylist having that 
* id or null if not found 
* @return 
*/ 

public Instructor findID (String id) { 
    for (Instructor entry:instList) { 
     if (entry.getId().equals(id) == true) { 
      return entry; 
     } else return null; 
    } 
} 
+0

是否'instList'集合有一個通用的類型? – Roman

回答

0

我相信,你instList列表聲明爲原料類型,即:

List instList = new ArrayList(); 

所以,讓你的代碼編譯的一種方法,是用一個類型來聲明instList

List<Instructor> instList = new ArrayList<Instructor>(); 

如果你不能做到這一點(即你從遠程源獲得這個集合),你可以將它轉換爲所需的類型:

for (Instructor entry: (List<Instructor>)instList) { 
    ... 
} 

最後,你可以施放檢索到的對象,而不是鑄造整個集合:

for (Object entry : instList) { 
    Object instructor = (Instructor)entry; 
    ... 
}