什麼是最佳實踐和解決方案檢查「索引超出界限」下面的解決方案的工作,但感覺很hacky。有沒有更好的選擇?接下來在數組和索引超出範圍
public void nextPerson(int index){ //Index is the current location in the arraylist
try{
System.out.println(thePeople.get(index++));
}
catch (IndexOutOfBoundsException e){
System.out.println("At the end");
}
}
有了您的解決方案,使用previousPerson方法的正確方法是什麼? – Melky
完全一樣,但有索引 - '...我建議你發佈一個更大的代碼摘錄,看看是否有可能重新設計一下 –
我的微不足道的想法是使用'index - '但它沒有工作。 '私人ArrayList thePeople;'我只有一個基本的構造函數,我將它們添加到數組中。 –
Melky