在Java中的初學者,我尋找的方式來返回與我的函數「readPeople()」與我的ArrayList「this.list」關聯的名稱由於用戶給出的ID。 這裏是我的代碼:如何用java中的元素返回名稱?
public class PersonneC implements PersonneDB {
this.list = new ArrayList();
People marie = new People(1, "Bower", "Marie");
list.add(marie.toString());
public PeopleWithId readPeople(int idPeople) {
PeopleWithId peoplename = null;
for(int i = 0; i < list.size(); i++) {
if(liste.get(i).equals(idPeople)) {
result = list.get(i);
}
}
return result.getPeopleName(); //ERROR INCOMPATIBLE TYPES
}
通緝的結果返回:鮑爾
我們可以讓人們類和什麼類型的結果,我認爲是人民和檢查list.add(marie.toString( )) – crammeur
'getPeopleName()'可能返回一個'String',但是你的方法聲明'PeopleWithId'爲返回類型。它不兼容。將它改爲'public String readPeople(int idPeople)' – davidxxx
,除了編譯器錯誤外,用'return list.get(i);'替換'result = list.get(i);'會更有效率'for'循環返回'null',或者甚至更好地使用'Optional'。 –